JDBC Prepared Statement Example
/* You'll need to make sure that ~karl/public/270/mysql.jar is in
* your CLASSPATH for this to work.
*
*/
import java.sql.*;
public class DecadesPrep {
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection
("jdbc:mysql://mcs-mysql.gac.edu/dummy_movies",
"dummy", "(password goes here)");
PreparedStatement stmt = c.prepareStatement
("select title, year_made from movies "
+ "where year_made >= ?"
+ " and year_made < ?");
for(int decadeStart = 1920; decadeStart < 2000; decadeStart += 10){
System.out.println("==== Movies of the " + decadeStart + "s ====");
stmt.setInt(1, decadeStart);
stmt.setInt(2, decadeStart + 10);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1) + " (" + rs.getInt(2) + ")");
}
}
} catch(Exception e){
System.err.println(e);
}
}
}