wood burning stoves 2.0*
The moose likes JDBC and the fly likes jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "jdbc" Watch "jdbc" New topic
Author

jdbc

selvi parthu
Greenhorn

Joined: Feb 02, 2006
Posts: 9
When we are using jdbc program we are using Class.forName("sun.jdbc.odbc.JDbcOdbcDriver");
What is the meaning of the statement?
Then we are using Connection,ResultSet,Statement interfaces,where they have been implemented?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

The Vendor implements all the interfaces i.e. Driver,Statement etc. For Oracle take a look at classes.zip.

Loading a class means a new Driver(for the first time) is created and gets registered with Driver manager. When you use the DriverManager.getConnection() it locates the appropriate driver from the available registered driver and creates the connection.


Groovy
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
Originally posted by chellam selvi:
When we are using jdbc program we are using Class.forName("sun.jdbc.odbc.JDbcOdbcDriver");
What is the meaning of the statement?
Then we are using Connection,ResultSet,Statement interfaces,where they have been implemented?


That loads the specified class into the JVM; that class (and all other JDBC-conformant drivers) in turn has a static initialization block that will register the driver with the DriverManager, thereyby making it available for use.

That driver is generally not a "production-capable" driver; database vendors have their own versions of a JDBC driver, some of which have ODBC capability. However, there are a few cases where sun.jdbc.odbc.JDbcOdbcDriver is the only free driver available for that type of data store; there are usually better but non-free drivers in those situations. Also however, in general, you should avoid using JDBC-ODBC bridges if you can, although there are times that you cannot.
selvi parthu
Greenhorn

Joined: Feb 02, 2006
Posts: 9
Then what is the return type for Class.forName(" ") method?
can we use Class.forName("java.lang.String");?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

Originally posted by chellam selvi:
Then what is the return type for Class.forName(" ") method?
can we use Class.forName("java.lang.String");?


The return type is "Class" object. The main purpose of doing a Class.forname in JDBC is to execute the static initializer block which creates a new instance of the Driver and registers with the DriverManager.

The alternate way DriverManager.registerDriver(new Driver()); This means you dont have to load the class.
selvi parthu
Greenhorn

Joined: Feb 02, 2006
Posts: 9
thank u
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

True, but registerDriver(new Driver()) is not recommended since it may result in the Driver class being registered twice, once by the driver when it loads, and again explicitly by you. Class.forName() works just fine.

Other ways to load drivers is to use the -Djdbc.driver=com.driver.Driver or to use a DataSource.

Of them all, the DataSource is preferred then Class.forName(). I've never seen the -D option used commonly but it works too, people generally prefer to put Driver settings in a properties file rather than using -D
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

True, but registerDriver(new Driver()) is not recommended since it may result in the Driver class being registered twice, once by the driver when it loads, and again explicitly by you. Class.forName() works just fine.


It actually registers it two times atleast in Sun JDK.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jdbc
 
Similar Threads
how is close() used?
Dealing with semi colons
Could not find UTF-8 string in Database
Prepared Statement
batch updates with statement using sequence