/* * This sample shows how to list the movie titles for a given actor * from the imdb database */ // You need to import the java.sql package to use JDBCimport java.sql.*; import java.sql.*; public class Movie2 { public static void main(String[] args) { if(args.length!=1){ System.out.println("Usage: java Movie2 "); System.exit(1); } String aname = args[0]; // actor name // 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); // Create a Statement Statement stmt = conn.createStatement (); // Select data from the productions table ResultSet rset = stmt.executeQuery ("select title,year,role from imdb.productions natural join imdb.principals natural join imdb.persons where name = '"+aname+"' and type = 'movie' and (category='actor' or category='actress') order by year"); // Iterate through the result and print the movie titles int ct = 0; while (rset.next () && ct<100){ //System.out.println (rset.getString("title") + "/" + rset.getString("year") + "/" + rset.getString("role")); System.out.printf("%-40s %s\n",rset.getString("title") + " (" + rset.getString("year") + ")", rset.getString("role")); ++ct; } // close the ResultSet if(rset != null) rset.close(); // close the connection if (conn != null) conn.close(); } catch(SQLException e) { e.printStackTrace(); } catch(Exception ee){ ee.printStackTrace(); } } }