• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName in depth

 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt regarding Class.forName().



Once the Class.forName () loads the class in JVM how DriverManager.getConnection() knows it ( like what class it should look for to create instance of the driver class. Lets assume i may be calling Class.forName to load another class also). How that DriverManger differentes/identifies which class to consider to create an object?

Appreciate your help.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The DriverManager associates a specific URL syntax with a specific type. This association is registered by drivers in a static block when the class is loader (hence the need to call Class.forName()).
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean to say in DriverManager java class they have written some code like new oracle.jdbc.driver.OracleDriver(). If it so how they know what is class name to use, how they link it to Class.forName() loaded classe. Could you please more clear on this. If possible could you please provide some mockup code.
 
Paul Clapham
Sheriff
Posts: 21586
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
murali kankanala wrote:You mean to say in DriverManager java class they have written some code like new oracle.jdbc.driver.OracleDriver().

No, it's not quite like that.
Could you please more clear on this. If possible could you please provide some mockup code.

Why mockup code? You can find the real thing in any open-source JDBC driver. I don't think that Oracle's JDBC driver is open-source, but MySQL's JDBC driver is. Check out its source code to see what it's doing.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul ,

I am also having the same question , assume that

if multiple Database Drivers are being loaded using for example


How does DriverManager knows that to what Database it need to connect ??

 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Paul said, looking at the code of a open-source JDBC driver will show how exactly it works. Take a look at the JTDS driver implementation here. See how it registers itself with the DriverManager in the static block. The DriverManager.getConnection will then look at each of the registered Driver and pass the connection URL to the acceptsURL method. Each JDBC driver is expected to implement it. Take a look at how it's implemented in the JTDS driver class that I pointed to earlier (search for acceptsURL method in that code).
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks JaiKiran , it was a nice information .

So , you mean to say that every Database has its own driverPrefix as in case of Sybase and Microsoft



Thanks .
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very nice explanation, thanks a lot.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran Va wrote:

So , you mean to say that every Database has its own driverPrefix as in case of Sybase and Microsoft





Each JDBC driver can have whatever logic it wants to, in the acceptsURL method. But typically, yes, each driver has its own driver prefix and as the javadoc of that method says:

Typically drivers will return true if they understand the subprotocol specified in the URL and false if they do not.

 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Thread hijack removed. Please start your own topic for your own questions -- and in the appropriate forum please.]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic