Solved

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

Posted on 2004-04-21
6
946 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
how to see all occupied ports on windows 10 laptop 15 94
swing controls 2 37
Java ArrayList and if statement 2 55
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

734 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