wayne
wayne

Reputation: 21

Help me connecting java and oracle

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

Upvotes: 2

Views: 415

Answers (3)

Steve De Caux
Steve De Caux

Reputation: 1779

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

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570285

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.

Upvotes: 4

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

Upvotes: 0

Related Questions