package dbpackage.jdbc; import java.sql.*; import java.util.Properties; import java.io.FileInputStream; import java.io.IOException; /** This example uses a java.sql.PreparedStatement to substitute a value into a query. This example uses the elder.jdbc.ConnectionFactory to simplify the details of acquiring a java.sql.Connection. @author Michael D Elder @version 31-Oct-2001 */ public class PreparedStatementExample { public static void main(String[] args) { ConnectionFactory factory = null; System.out.println("This example uses the elder.jdbc.ConnectionFactory \nand assume Oracle as the database."); if(args.length == 0) { System.out.println("Usage: java elder.jdbc.PreparedStatementExample ... where in [1,4]"); return; } try { System.out.println("Looking for oracle.properties"); factory = new ConnectionFactory("oracle.properties"); } catch(Exception e) { System.out.println("Could not create Factory."); e.printStackTrace(); } String queryArgument = args[0]; Connection dbConn = null; try { dbConn = factory.createConnection(); int argument = Integer.parseInt(queryArgument.trim()); PreparedStatement pStmt = dbConn.prepareStatement("select * from STUDENT where STUNUM = ?"); pStmt.setInt(1, argument); ResultSet rs = pStmt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); /* the spec defines the first call of next() to aim the scroller at the first row */ /* print header */ for(int indx=1; indx