// // jdbcSelectDemo3.java // // 05-05-2002 Bikle // Package Declaration // //////////////////// // nada // Imports // ///////////////// import java.io.*; import java.lang.*; import java.net.*; import java.sql.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; // //////////// public class jdbcSelectDemo3 // //////////// { // Declarations // ///////////////// // I like to declare as many objects up here as possible. // This makes it easier for me to track the state of objects with my debugger. Connection con = null; String SQLcmd = ""; PreparedStatement prepStmt = null; String alinkBindVar = ""; ResultSet rs = null; Exception anException = null; // Constructors // ///////////////// // empty Constructor public jdbcSelectDemo3 () { // emptiness } // Methods // ///////////////// // method Select() // /////////////////// public void Select(String newAlinkBindVar ) { alinkBindVar = newAlinkBindVar; // This variable shapes my WHERE clause try { // first, I connect to the DB // Class.forName ("oracle.jdbc.driver.OracleDriver"); // DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); // con = DriverManager.getConnection // ("jdbc:oracle:thin:@host1.bikle.com:1521:XSID", "scott", "tiger"); Class.forName("org.gjt.mm.mysql.Driver"); con = DriverManager.getConnection ( "jdbc:mysql://localhost/mysql?user=root" ); // I should be connected to the DB now. // (Often, I'll put the above syntax in a method and call the method.) // ////////////////////////////////////////////////////////////////////////////////// // Build a SELECT statement which will return several rows SQLcmd = ""; SQLcmd = SQLcmd + " SELECT * FROM links "; SQLcmd = SQLcmd + " WHERE alink != ? "; // Notice I use a != here // Create a prepared statement prepStmt = con.prepareStatement(SQLcmd); // Now pass in the bind variable prepStmt.setString(1,alinkBindVar ); // Fill the result set rs = prepStmt.executeQuery( ); // Get the number of columns in the result set ResultSetMetaData rsmd = rs.getMetaData(); // I make use of a handy class here. int numOfColumns = rsmd.getColumnCount(); // jdbc tells me how many columns I have. // print some HTML System.out.println ("\n"); System.out.println ("\n"); // Loop through the result set while ( rs.next() ) { System.out.println ("\n"); // I start a row here for (int k=1; k<=numOfColumns; k++ ) // Notice I start counting at 1 { System.out.println ("\n"); } System.out.println ("\n"); } System.out.println ("
\n"); // I start a column here System.out.println (rs.getString(k) + "\n"); // I put data in column here System.out.println ("
\n"); System.out.println ("\n"); } catch (SQLException aSQLException) { // I create anException to make aSQLException visible to my debugger anException = aSQLException; System.out.println (anException); } catch (Exception aException) { // I create anException to make aException visible to my debugger anException = aException; System.out.println (anException); } } // end method Select() // close() // //////////// void close () { try { if(con != null) { con.close(); con = null; } } catch (SQLException aSQLException) { // I create anException to make aSQLException visible to my debugger anException = aSQLException; System.out.println (anException); } catch (Exception aException) { // I create anException to make aException visible to my debugger anException = aException; System.out.println (anException); } } // end close() // method main() // /////////////////// public static void main (String[] args) { jdbcSelectDemo3 XjdbcSelectDemo3 = new jdbcSelectDemo3(); XjdbcSelectDemo3.Select("flowers.htm"); // I specify the value of the bind variable here XjdbcSelectDemo3.close(); } // end method main() }