• 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

What does the line Class.forName("oracle.jdbc.OracleDriver"); actually do?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the line -

Class.forName("oracle.jdbc.OracleDriver");

Actually do in the program? A snippet of my code follows.

*
*
*

public static void main(String[] args) {
try{

Class.forName("oracle.jdbc.OracleDriver");

String serverName = "oramwt1";
String portNumber = "4521";
String sid = "xxxxxxxxxxxxx";
String url = "jdbc: oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "xxx";
String password = "xxxxxxxxxxxxxxxxx";

Connection con = DriverManager.getConnection(url, username, password);
*
*
*

I appreciate the help and look forward to hearing from you.

Aelf
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All JDBC Drivers are required to have a static code block that registers them with the DriverManager. Forcing the classloader to load the class will cause this code to run.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

All JDBC Drivers are required to have a static code block that registers them with the DriverManager.



You mean to say that this thing :

 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NO

so only this thing is sufficient

Class.forName("oracle.jdbc.OracleDriver");



and this piece of line of code is static method which will be responsible to establish the connection with the database. is this thing you ere refering .

I am asking because we generally use DataSource to get connections , but the above is a standard interview question . Thanks .
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that method returns the Class object associated with the class of that name. To do this the class will have to be loaded at which point the static block in the Driver will be called.
 
reply
    Bookmark Topic Watch Topic
  • New Topic