• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection

 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We had Driver interface and had connect() method in it to establish connections. But why do we prefer to get connection by using DriverManager class getConnection() method? I refer so many books all of them are using DriverManager class.

Thank You
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vallabhaneni Suresh Kumar:
We had Driver interface and had connect() method in it to establish connections. But why do we prefer to get connection by using DriverManager class getConnection() method? I refer so many books all of them are using DriverManager class.


So, have you tried to get the connection from Driver.connect()?
 
Makarand Parab
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Okay here is the answer.
When u say Class.forName("oracle.driver.OracleDriver");
Class.forName gets executed. it loads the class "OracleDriver".
In class OracleDriver, there is a static block which gets executed as stated below

public class OracleDriver
{
OracleDriver orDriver = null ;

static
{
if(orDriver == null)
{
//loads and creates a new object if it does
not exist
OracleDriver orDriver = new OracleDriver ();
//then it registers with the Driver Manager using
DriverManager.registerDriver(orDriver);
// this is a static method check
the DriverManager API
}
}
}

Since ur Driver is register with DriverManager, u call DriverManager.getConnection();
If u ur using classes12.jar, check the OracleDriver.class file. Open it and see the code.

Let me know if it makes send 2 you.

Regards
Makarand Parab
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

It is better to use DataSource class that using DriverManager.

You may please refer to the following link...
http://www.oracle.com/technology/tech/java/sqlj_jdbc/htdocs/jdbc_faq.htm#01_02

They will answer your most of the questions.

Good luck...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic