Solved

Explanation of "Class.forName" and "Connecton con = DriveManager.getConnecton"

Posted on 2004-04-21
6
951 Views
Last Modified: 2011-10-03
Could someone explain to me the relavancy/pupose of the following two line of code, in the excerpt of code below?

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection (url);



import java.sql.*;
public class P5 {
      public static void main (String args[]) {

            String url = "jdbc:odbc:P4";

            Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection (url);
Statement stmt = con.createStatement ();
String query = "SELECT ENO FROM Employee";
ResultSet rs = stmt.executeQuery (query);
while (rs.next())
{
        String eno = rs.getString("ENO");    
        System.out.println(eno);
}
      }
}
0
Comment
Question by:aquemini001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 20 total points
ID: 10885964
Class.forName is a way to decide at runtime which class to load.
JAVA has the feature to let you refer a class, method, field ...  when your program
is already running, so you can't write the code anymore.
So, JAVA compiles a bit for you when running.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10885973
Your second question: that's the same: choosing a database connection in the nick of time.
;JOOP!
0
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 10886216
ClassForName loads the java class with the specified name (so kinda... string TO class)
DriverManager uses that class to build a connectio to the db - the url is vendor-specific though...AFAIK

You might as well use the oracleDataSource directly, no need for a driverManager :P
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 30

Expert Comment

by:Mayank S
ID: 10887539
You might want to go through the JDBC tutorial to get a complete picture of what's happening:

http://java.sun.com/docs/books/tutorial/jdbc/
0
 
LVL 1

Expert Comment

by:seanrowen
ID: 10892389
Daij-Djan is right -- Class.forName() simply causes the class to be loaded into the classloader. There is no compilation or anything, and in general this is not a mechanism for "selecting" a class.

As it happens, when JDBC driver classes load, they register themselves with JDBC's DriverManager. That way, DriverManager knows about the existence of your driver for your database and will use it when you later get a Connection.

This is the "old way" of obtaining a Connection. You ought to be using a DataSource. In a J2EE app,  you should configure the DataSource for your container and look it up using JNDI. Here is some more information, from the Tomcat documentation:
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html
0
 

Author Comment

by:aquemini001
ID: 10973081
thanks
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question