import java.sql.*; import java.io.Console; public class MoviesTogether { public static void main(String[] args) { /* // Get the user's name and password Console cons = System.console(); cons.printf("Enter username: "); String uname = cons.readLine(); cons.printf("Enter password: "); char[] pword = cons.readPassword(); String pwd = ""; for(char c : pword) pwd += c; */ if(args.length!=2){ System.out.println("Usage: java MoviesTogether "); System.exit(1); } String aname1 = args[0]; String aname2 = args[1]; System.out.println("Finding movies in which "+aname1+" and "+aname2+" appeared together.\n"); // Connect to the database // You can put a database name before the ? in the connection URL. Connection conn = null; String url = "jdbc:mysql://sql.cs.oberlin.edu/?"; String parms = "user=alice&password=tiger&useSSL=false"; try { conn = DriverManager.getConnection(url+parms); Statement stmt = conn.createStatement(); String s = "select title,year from imdb.productions "+ "where type='movie' and tcode in "+ "(select p1.tcode from imdb.principals p1 , imdb.principals p2 "+ "where p1.tcode=p2.tcode "+ "and p1.ncode in "+ " (select ncode from imdb.persons where name='"+aname1+"') "+ "and p2.ncode in "+ " (select ncode from imdb.persons where name='"+aname2+"') "+ "and p1.category!='self' "+ "and p2.category!='self') "+ "order by year"; ResultSet r = stmt.executeQuery(s); while(r.next()){ String title = r.getString("title"); String year = r.getString("year"); System.out.printf("%s (%s)\n",title,year); } } catch(SQLException e) { e.printStackTrace(); } catch(Exception ee){ ee.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }