• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ways in doing database connection?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The database connection can be done by the following days:-

1) DriverManager.getConnection
2) Class.forName

I wanna know the difference between the two and is there any other way to do the database connection and what for we are using Class.forName to do the database connection.

bhuvi
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
For getting the connection using DriverManager class, an instane of the driver is required in the registerDriver method. But Class.forName returns the Class object associated with the class or interface with the given string name, so you can even tell the driver to be used by your program using the property file.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Bhuvi,

your question is not general at all, and can therefore not be answered in an easy / obvious way.

DriverManager.getConnection() returns an object (as described in the DriverManger documentation of your database). Probably a Connection-Object ;-) That's fix atcompile time.

Class.forName() returns a reference to a class object. Dynamically determined at run time.

The latter has no specific relation to a DB-connection. But it can be used to get a db connection (and all other classes in your project).

hope that helps,
jan.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to JDBC forum...
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the java docs guide:

Class.forName

by calling the method Class.forName. This explicitly loads the driver class.
loading the class causes an instance to be created and also calls DriverManager.registerDriver with that instance as the parameter, then it is in the DriverManager's list of drivers and available for creating a connection.

DriverManager.getConnection

Once the Driver classes have been loaded and registered with the DriverManager class, they are available for establishing a connection with a database. When a request for a connection is made with a call to the DriverManager.getConnection method, the DriverManager tests each driver in turn to see if it can establish a connection.

It may sometimes be the case that more than one JDBC driver is capable of connecting to a given URL
In such cases, the order in which the drivers are tested is significant because the DriverManager will use the first driver it finds that can successfully connect to the given URL.

First the DriverManager tries to use each driver in the order it was registered. (The drivers listed in jdbc.drivers are always registered first.) It will skip any drivers that are untrusted code unless they have been loaded from the same source as the code that is trying to open the connection.


Shailesh
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic