Java Tutorial - Java Script :
The
The JDBC-ODBC Bridge
Moving Through ResultsetsThe default behavior of resultsets permits one trip through the set using its next() method to retrieve each record. By changing how statements and prepared statements are created, you can produce resultsets that support these additional methods:· afterLast()—Moves to a place immediately after the last record in the set· beforeFirst()—Moves to a place immediately before the first record in the set· first()—Moves to the first record in the set· last()—Moves to the last record in the set· previous()—Moves to the previous record in the setThese actions are possible when the resultset’s policies have been specified as arguments to a database connection’s createStatement() and prepareStatement() methods. Normally, createStatement() takes no arguments, as in this example:Connection payday = DriverManager.getConnection( “jdbc:odbc:Payroll”, “Doc”, “1rover1”);Statement lookSee = payday.CreateStatement();For a more flexible resultset, call createStatement() with three integer arguments that set up how it can be used. Here’s a rewrite of the preceding statement:Statement lookSee = payday.CreateStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY,ResultSet.CLOSE_CURSORS_AT_COMMIT);The same three arguments can be used in the prepareStatement(String, int, int, int) method after the text of the statement. The ResultSet class includes other class variables that offer more options in how sets can be read and modified.JDBC DriversCreating a Java program that uses a JDBC driver is similar to creating one that uses the JDBC-ODBC bridge. Java 6 includes the Java DB relational database, which is built from the open source Apache Derby database, and comes with its own driver. For more sophisticated databases, more than a dozen companies, including Informix, Oracle, Symantec, IBM, and Sybase, sell drivers or package them with commercial products. A database of available JDBC drivers can be found on Sun’s JDBC site at http://developers.sun.com/product/ jdbc/driversJava DB can be found in a db subfolder of the JDK installation. To develop applications that connect to the database, you must make its driver class library accessible. One way to accomplish this is to edit your Classpath environment variable. The driver library is found in derby.jar in the db/lib subfolder. If you installed the JDK in C:\Program Files\jdk1.6.0, this library is in C:\Program Files\jdk1.6.0\ db\lib\derby.jar. Add the entire file reference, including the folder and filename, to your Classpath The steps for setting up a data source for JDBC are similar to those employed with the JDBC-ODBC bridge:· Create the database.· Associate the database with a JDBC driver.· Establish a data source, which may include selecting a database format, database server, username, and password.Listing 18.3 is a Java application that can perform two tasks:· Create a Java DB database named Presidents with a database table called contacts that contains four records.· Read the records from this database table.
This database is an Access file with contact information for U.S. presidents
Moving Through Resultsets
The default behavior of resultsets permits one trip through the set using its next() method to retrieve each record. By changing how statements and prepared statements are created, you can produce resultsets that support these additional methods:
· afterLast()—Moves to a place immediately after the last record in the set
· beforeFirst()—Moves to a place immediately before the first record in the set
· first()—Moves to the first record in the set
· last()—Moves to the last record in the set
· previous()—Moves to the previous record in the set
These actions are possible when the resultset’s policies have been specified as arguments to a database connection’s createStatement() and prepareStatement() methods. Normally, createStatement() takes no arguments, as in this example:
Connection payday = DriverManager.getConnection( “jdbc:odbc:Payroll”, “Doc”, “1rover1”);
Statement lookSee = payday.CreateStatement();
For a more flexible resultset, call createStatement() with three integer arguments that set up how it can be used. Here’s a rewrite of the preceding statement:
Statement lookSee = payday.CreateStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
The same three arguments can be used in the prepareStatement(String, int, int, int) method after the text of the statement. The ResultSet class includes other class variables that offer more options in how sets can be read and modified.
JDBC Drivers
Creating a Java program that uses a JDBC driver is similar to creating one that uses the JDBC-ODBC bridge. Java 6 includes the Java DB relational database, which is built from the open source Apache Derby database, and comes with its own driver. For more sophisticated databases, more than a dozen companies, including Informix, Oracle, Symantec, IBM, and Sybase, sell drivers or package them with commercial products. A database of available JDBC drivers can be found on Sun’s JDBC site at http://developers.sun.com/product/ jdbc/driversJava DB can be found in a db subfolder of the JDK installation. To develop applications that connect to the database, you must make its driver class library accessible. One way to accomplish this is to edit your Classpath environment variable. The driver library is found in derby.jar in the db/lib subfolder. If you installed the JDK in C:\Program Files\jdk1.6.0, this library is in C:\Program Files\jdk1.6.0\ db\lib\derby.jar. Add the entire file reference, including the folder and filename, to your Classpath The steps for setting up a data source for JDBC are similar to those employed with the JDBC-ODBC bridge:
· Create the database.
· Associate the database with a JDBC driver.
· Establish a data source, which may include selecting a database format, database server, username, and password.
Listing 18.3 is a Java application that can perform two tasks:
· Create a Java DB database named Presidents with a database table called contacts that contains four records.
· Read the records from this database table.
This database is an Access file with contact information for U.S. presidents
