Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the purpose of Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")?

 
phooton misra
Ranch Hand
Posts: 52
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some Body please suggest me What is the purpose of

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

regards.

Phooton...
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
phooton misra wrote:Some Body please suggest me What is the purpose of

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

regards.

Phooton...

A quick search would provide you some basic information about forName method.

Basically, Class.forName simply loads the mentioned class in JVM. This is just like an import statement, but provide more facilities (e.g. you can load a class based on some condition, or you can take name of the class from environment variable or property file etc.)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes Java load the class sun.jdbc.odbc.JdbcOdbcDriver.

In the case of JDBC drivers, this will make the driver class register itself so that the JDBC API knows where to find the driver when you open a database connection. Note that for newer JDBC drivers, this line isn't necessary anymore - JDBC now uses another mechanism to automatically find drivers.

See also the API documentation of the forName() method in class java.lang.Class.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody else asked a very similar question recently, and if you look here, you will probably get the answer to your question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic