Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading mechanism of Jdbc Driver

 
Mr Anil Kumar Pandey
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

is this the only way to load the Driver class.
if so why
can it not be possible by importing the package and creating the objects or any other way so on.

kindly describe in depth
 
ravisha andar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use

Driver d=new sun.jdbc.odbc.JDBCODBCDriver();
DriverManger.registerDriver(d);



 
Mr Anil Kumar Pandey
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ravisha andar wrote:You can use

Driver d=new sun.jdbc.odbc.JDBCODBCDriver();
DriverManger.registerDriver(d);





How Class.forName("any class name");
works that is with what mechanism it is loading the given class.
as we know that class occupy memory when object using new is created.
please in detail.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ravisha andar wrote:You can use

Driver d=new sun.jdbc.odbc.JDBCODBCDriver();
DriverManger.registerDriver(d);





This has the downside of meaning you need to recompile your code if you swap drivers. Not a huge issue if you are using the JDBC-ODBC bridge (because you can always change your DSN), but then you would only ever use the JDBC-ODBC bridge if you were working with Access.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call Class.forName() the class loader will load the class. JDBC driver implementations are required to have a static block that registers the driver with the DriverManager. This registration step is what binds a driver implementation with a specific URL. So later when you use DriverManager.getConnection("jdbc:foo://blah:1234/database") the DriverManager can get the right driver for that URL.
 
Mr Anil Kumar Pandey
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:When you call Class.forName() the class loader will load the class. JDBC driver implementations are required to have a static block that registers the driver with the DriverManager. This registration step is what binds a driver implementation with a specific URL. So later when you use DriverManager.getConnection("jdbc:foo://blah:1234/database") the DriverManager can get the right driver for that URL.


thanks sir

what i got, if i am right, is that there is static block that is actually instantiating the class automatically whenever it is loaded.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The DriverManager will instantiate the class when it is asked to provide a Driver for a specific URL. The code in the static block just registers the driver with the DriverManager.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic