Solved

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

Posted on 2004-04-21
6
939 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

832 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