tags:

views:

147

answers:

3

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

A: 

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

Steve De Caux
According to the javadoc: Applications no longer need to explictly load JDBC drivers using <code>Class.forName()</code>. Existing programs which currently load JDBC drivers using <code>Class.forName()</code> will continue to work without modification.
Bozho
Please forgive the poor bastards stuck in 1.4.2 because of compatibility constraints
Steve De Caux
well, "is not the recommended way" is a wrong statement.
Bozho
point taken, hard cruel world
Steve De Caux
you can edit your post, you know ;)
Bozho
Good thinking, problem gone (I hope)
Steve De Caux
and downvote gone as well ;)
Bozho
A: 

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

Thorbjørn Ravn Andersen
+4  A: 

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 the DriverManager class when it is loaded. Thus, a user would not normally call DriverManager.registerDriver directly; it should be called automatically by a Driver class when it is loaded.

Check the Driver Manager chapter from the JDBC documentation for more details.

Pascal Thivent
thanx 4 ur answeri realy appreciate thatgood luckRAQ Reportwww.raqsoft.com
wayne
You should know that the oracle.jdbc.driver package is de-supported for customer use by Oracle. From now on the oracle.jdbc package should be used.
Adam Hawkes