Selecting and deregistering JDBC drivers
During the course of your application you may need to select or remove specific drivers from the DriverManager list. These methods are useful if, for example, you implement a Factory design pattern to support multiple databases. Within your Factory class you could use DriverManager to pre-register the JDBC drivers you need for connecting to the supported databases. When a request for a Connection object occurs, your factory selects the proper driver from DriverManager and then creates the...
Using JDBC Statements
The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enable you to send commands and receive data from your database. They also define methods that help bridge data type differences between Java and SQL data types used in a database. This is not a trivial task. As an example of the data type differences consider the Java int primitive data type, which cannot represent a NULL. Yet databases use NULL values extensively to represent empty...
Standard SQL data types
Unlike with the setXXX and updateXXX methods, you can specify which Java type you want to cast the JDBC type to with the getXXX method. The following code snippet demonstrates converting an SQL INTEGER to a Java double Create a ResultSet that retrieve the SQL INTEGER ssn String SQL SELECT Ssn FROM Employees WHERE Name 'ToddT' ResultSet rs stmt.executeQuery SQL Retrieve as a double double ssn rs.getDouble 1 Most JDBC-SQL data types can map to numerous Java data types. However, JDBC provides...
Result set types
JDBC provides different types of ResultSet objects that enables you to interact with the database in a variety of ways. The first, which is the default, has minimal functionality and enables you only to move forward through the result it does not enable you to update data. The second type enables you to move forward, backward, or to any row within the result set. The third enables you to update the contents in the result set. JDBC 3.0 Prior to JDBC 3.0, the Connection.commit method could close...
Result set cursors
Despite the number of rows in a result set, you can only access one row at a time. A ResultSet object points to this active row using a cursor. If you want to reference another row in the result set you must explicitly move the cursor with one of the ResultSet object's cursor-movement methods. Figure 6-1 illustrates a result set cursor and how it moves through the data set. Notice that the cursor points to the third row of a seven-row result set. If you issue the ResultSet.next method, the...
Advanced SQL data types
As computer technology progressed through the 1990s, a need for richer, more advanced data type support in databases arose for two reasons. First, developers were becoming skilled at modeling complex engineering and business processes using object-oriented programming techniques. However, they had trouble storing data from these object models in relational databases. Only the attributes of the objects could be stored in the database. The developers needed databases to support the user-defined...


