JDBC Prepared Statement Example (MCS-270, Spring 2001)

import java.sql.*;

public class DecadesPrep {

  public static void main(String args[]){
    try{
      Class.forName("postgresql.Driver");
      Connection c = DriverManager.getConnection
        ("jdbc:postgresql://kilpinen.mcs.gac.edu/max_movies",
         "nobody", "(password disclosed in class)");
      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);
    }
  }
}