Solved

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

Posted on 2004-04-21
6
926 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:mayankeagle
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now