Can anybody explain me these classes and methods?
DriverManager.registerDriver
(new oracle.jdbc.driver.OracleDriver());
conn = java.sql.DriverManager.getConnection(
"jdb:ocracle:thin:username/[email protected]:1234:dbSID");
Thanks
Can anybody explain me these classes and methods?
DriverManager.registerDriver
(new oracle.jdbc.driver.OracleDriver());
conn = java.sql.DriverManager.getConnection(
"jdb:ocracle:thin:username/[email protected]:1234:dbSID");
Thanks
The DriverManager
class in java handles the connections between the database and the jdbc drivers, routing db i/o to the correct jdbc driver (you can have multiple drivers active ie connections to multiple types of database).
Drivers are registered with the DriverManager so that they become part of its working set. The next step is to create a connection to your database, so you can run queries. This is achived by the
Connection conn = DriverManager.getConnection("jdb:ocracle:thin:username/[email protected]:1234:dbSID")
method. The connection String passed into the getConnection()
method is driver-specific, you need to RTFM for each driver. Note that the DriverManager
selects the driver automatically from its list of registered drivers, according to the syntax of the connection string you pass in.
The Connection object returned is your handle for preparing statements and running queries against the database
This is JDBC which is the way Java programs talk to a database, and your sample explicitly asks for the Oracle driver which requires their driver in your classpath.
Sun has a good tutorial on the matter at http://java.sun.com/docs/books/tutorial/jdbc/overview/index.html
Let's decode the lines of your code block:
1. DriverManager.registerDriver
2. (new oracle.jdbc.driver.OracleDriver());
3. conn = java.sql.DriverManager.getConnection(
4. "jdbc:oracle:thin:username/[email protected]:1234:dbSID");
Line 2:
Creates a new instance of oracle.jdbc.driver.OracleDriver
, a JDBC Driver
for the Oracle database. A JDBC driver implements the interfaces and classes defined by the JDBC API that programmers use to connect to a database and perform queries.
Line 1
Registers the instance of the oracle.jdbc.driver.OracleDriver
to the DriverManager
class which is the traditional management layer of JDBC, working between the user and the drivers. It handles establishing a connection between a database and the appropriate driver.
Line 3:
Now that the communication layer between the JDBC application and the database is ready, you can create a connection by calling getConnection()
method of the DriverManager
class.
Line 4:
This is the "connection string" or "database URL". This String
identifies the database you want to connect to. The scheme of this URL is specific to the database provider and/or the driver (here, Oracle and its "thin" driver).
Note that prior to Java 6, calling Class.forName
was the preferred way to load and register a JDBC Driver. It was the responsibility of the Driver
to call DriverManager.registerDriver
.
[...] All
Driver
classes should be written with a static section (a static initializer) that creates an instance of the class and then registers it with theDriverManager
class when it is loaded. Thus, a user would not normally callDriverManager.registerDriver
directly; it should be called automatically by aDriver
class when it is loaded.
Check the Driver Manager chapter from the JDBC documentation for more details.