Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 75637
354
  • 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.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic