Java Reference
In-Depth Information
This URL specifies that JDBC should connect to the database named “mydb” stored
on a MySQL database server running on the host dbserver.mydomain.com and lis-
tening for connections on port 1234.
If you are running the database server on the same host your Java program is run-
ning on, you can omit the host name portion of the URL. If your database server is
listening on its default port (which it usually does), you can omit the port number.
Here's another jdbc: URL that works for PosgreSQL server:
jdbc:postgresql://dbserver.mydomain.com/mydb
Or, if you are connecting to a server on the local host:
jdbc:postgresql:mydb
DriverManager.getConnection() returns an object that implements the Connec-
tion interface. This object represents the connection to the database; you use it to
interact with the database. The createStatement() method of the Connection
object creates an object that implements the Statement interface, which is what
you use to send SQL queries and updates to the database. The executeQuery()
and executeUpdate() methods of the Statement object send queries and updates,
respectively, while the general-purpose execute() method sends a statement that
can be either a query or an update.
After you send a query to the database, use the getResultSet() method of State-
ment to retrieve an object that implements the ResultSet interface. This object rep-
resents the values returned by the SQL query; it is organized into columns and
rows like a table. A ResultSet offers its data one row at a time; you use next() to
move from the current row to the next row. ResultSet provides numerous get X ()
methods that allow you to retrieve the data from each column of the current row
as a number of different types.
The JDBC API was updated in Java 1.2 to JDBC 2.0. In JDBC 2.0, result sets can be
configured to be scrollable, which means that in addition to the next() method,
you can also use the previous() method to move to the previous row, first()
and last() to move to the first and last rows, respectively, and absolute() and
relative() to move to an arbitrary row specified with an absolute or relative row
number. If your database server supports scrollable result sets, and if you're using
a JDBC 2.0-compliant driver, you can specify scrollable result sets when you create
your Statement object. To do this, use the two-argument version of the createS-
tatement() method, with code like this:
Statement s = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
There are other possible values for the two arguments to createStatement() , but
a discussion of them is beyond the scope of this chapter. See Java Enterprise in a
Nutshell for further information.
Now that you understand the basic techniques used in a JDBC program, let's go
on to Example 17-1. The ExecuteSQL program uses all the techniques just dis-
cussed to connect to a database, execute SQL statements, and display the results.
The program parses its command-line arguments to determine the class name of
the JDBC driver, the URL of the database, and other parameters necessary to
Search WWH ::




Custom Search