Java Reference
In-Depth Information
The standard syntax for JDBC URLs is shown here:
jdbc:<subprotocol>:<subname>
The three parts of a JDBC URL are broken down as follows:
Jdbc — The protocol. The protocol in a JDBC URL is always jdbc.
<subprotocol> — The name of the driver or connectivity mechanism, which may be supported by
one or more drivers
<subname> — A unique identifier for the database
For example, this is the URL to access the contacts database through the
JDBC-ODBC bridge:
jdbc:odbc:contacts
The odbc subprotocol
The odbc subprotocol has the special feature of allowing any number of attribute
values to be specified after the database name, as shown here:
jdbc:odbc:<data-source-name>[;<attribute-name>=<attribute-value>]*
Attributes passed in this way may include user id and password, for example.
Having established a connection to the database, you are now in a position to
execute a SQL statement. The
next section
discusses SQL statements.
SQL Statements
Once a connection is established, it is used to pass SQL statements to the database.
Since there are no restrictions imposed on the kinds of SQL statements that may be
sent to a DBMS using JDBC, the user has a great deal of flexibility to use
database-specific statements or even non-SQL statements.
The JDBC core API provides these three classes for sending SQL statements to the
database:
Statement. A Statement object is used for sending simple SQL statements. Statements are
created by the method createStatement().
PreparedStatement. A PreparedStatement is a SQL statement that is precompiled and stored in
a PreparedStatement object. This object can then be used to execute this statement multiple
times.
CallableStatement. CallableStatement is used to execute SQL stored procedures.
CallableStatements are created by the method prepareCall().