Java Reference
In-Depth Information
}
}
The first load succeeded; the second failed because I didn't have the Oracle driver installed
at the time:
$ java LoadDriver
Loaded class sun.jdbc.odbc.JdbcOdbcDriver
java.lang.ClassNotFoundException: oracle/jdbc/driver/OracleDriver
$
It is also possible to preregister a driver using the -D option to load it into the System Proper-
ties; in this case, you can skip the Class.forName() step:
$ java -Djdbc.drivers=com.acmewidgets.AcmeDriver:foo.bar.OhMyDriver MyClass
Once you have registered the driver, you are ready to connect to the database.
Connecting to a JDBC Database
Problem
You need to get a Connection to the relational database.
Solution
Use DriverManager.getConnection() .
Discussion
The static method DriverManager.getConnection() lets you connect to the database using
a URL-like syntax for the database name (for example, jdbc:dbmsnetproto://server:4567/
mydatabase ) and a login name and password. The “dbURL” that you give must begin with
jdbc :. The rest of it can be in whatever form the driver vendor's documentation requires and
is checked by the driver. The DriverManager asks each driver you have loaded (if you've
loaded any) to see if it can handle a URL of the form you provided. The first one that re-
Search WWH ::




Custom Search