Java Reference
In-Depth Information
The DriverManager polls all registered drivers to find the first one that can create a connection to the
URL. Variations on this command let you give the database a user name and password or pass a Java
Properties object with the URL:
getConnection(String url,String user,String password);
getConnection(String url, Properties info);
A connection represents a session with a specific database, providing the context in which our SQL
statements are executed and results are returned.
Statement
The term
Statement
refers to the Java class that passes the SQL Query to the database via the
connection rather than to the SQL Query itself. A
Statement
object is used for executing a static SQL
statement and obtaining the results it produces.
The actual SQL command you pass to the database is the command you have just created when we
were discussing the
CREATE
command. JDBC does not put any restrictions on the SQL commands
you send to the database, but you must ensure that the data source you are connecting to supports
whatever SQL you are using. JDBC allows any query string to be passed to an underlying DBMS
driver, so an application may use as much SQL functionality as desired at the risk of receiving an error
on some DBMSs. In fact, an application query need not even be SQL, or it may be a specialized
derivative of SQL. If the database engine reports a problem, a SQLException will be thrown, providing
information on the database-access error.
Listing 5-1
contains the code for creating a table using JDBC.
Listing 5-1: Creating a table using JDBC
package jdbc_bible.part2;
import java.sql.*;
import sun.jdbc.odbc.JdbcOdbcDriver;
public class TableMaker{
static String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
static String dbName = "Contacts";
static String url = "jdbc:odbc:";
static String SQLCreate =
"CREATE TABLE CONTACT_INFO ("+