Java Reference
In-Depth Information
T ABLE 32.3
JDBC Drivers
Database
Driver Class
Source
Access
sun.jdbc.odbc.JdbcOdbcDriver
Already in JDK
MySQL
com.mysql.jdbc.Driver
mysql-connector-java-5.1.26.jar
Oracle
oracle.jdbc.driver.OracleDriver
ojdbc6.jar
Oracle drivers, you have to add mysql-connector-java-5.1.26.jar and ojdbc6.jar in the
classpath using the following DOS command on Windows:
ojdbc6.jar
set classpath=%classpath%;c:\book\lib\mysql-connector-java-5.1.26.jar;
c:\book\lib\ojdbc6.jar
If you use an IDE such as Eclipse or NetBeans, you need to add these jar files into the library
in the IDE.
Note
com.mysql.jdbc.Driver is a class in mysql-connector-java-5.1.26.jar , and
oracle.jdbc.driver.OracleDriver is a class in ojdbc6.jar. mysql-connector-
java-5.1.26.jar , and ojdbc6.jar contains many classes to support the driver. These
classes are used by JDBC but not directly by JDBC programmers. When you use a class
explicitly in the program, it is automatically loaded by the JVM. The driver classes,
however, are not used explicitly in the program, so you have to write the code to tell
the JVM to load them.
why load a driver?
Note
Java 6 supports automatic driver discovery, so you don't have to load the driver explicitly.
At the time of this writing, however, this feature is not supported for all database drivers.
To be safe, load the driver explicitly.
automatic driver discovery
2. Establishing connections.
To connect to a database, use the static method getConnection(databaseURL) in the
DriverManager class, as follows:
Connection connection = DriverManager.getConnection(databaseURL);
where databaseURL is the unique identifier of the database on the Internet. TableĀ 32.4 lists
the URL patterns for the Access, MySQL, and Oracle databases.
T ABLE 32.4
JDBC URLs
Database
URL Pattern
Access
jdbc:odbc:dataSource
MySQL
jdbc:mysql://hostname/dbname
Oracle
jdbc:oracle:thin:@hostname:port#:oracleDBSID
For an ODBC data source, the databaseURL is jdbc:odbc:dataSource . An ODBC
data source can be created using the ODBC Data Source Administrator on Windows. See
Supplement IV.D, Tutorial for Microsoft Access, on how to create an ODBC data source for
an Access database.
connect Access DB
 
 
Search WWH ::




Custom Search