How can we connect database in Java without using the Class.forname() method with our application?

Showing Answers 1 - 4 of 4 Answers

Supraja

  • Dec 1st, 2005
 

import java.sql.*; 

class JdbcTest1 { 
     
    public static void main (String[] args) { 
        try { 

            // Step 1: Load the JDBC driver. 
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

            // Step 2: Establish the connection to the database. 
            String url = "jdbc:odbc:contact_mgr"; 
            Connection conn = DriverManager.getConnection(url,"user1","password");  

        } catch (Exception e) { 
            System.err.println("Got an exception! "); 
            System.err.println(e.getMessage()); 
        } 
    } 
} 

  Was this answer useful?  Yes

Supraja

  • Dec 1st, 2005
 

sorry I wrote that code in hurry, the correct answer is as follows:

(A)

Drivers can also be specified from the command line via the jdbc.drivers system property, but this method requires the driver(s) to be in the classpath at compile time:

java -Djdbc.drivers=DriverClassName AJavaApp
(B)

As part of its initialization, the DriverManager class will attempt to load

the driver classes referenced in the "jdbc.drivers" system property.

This allows a user to customize the JDBC Drivers used by their applications.

For example in your ~/.hotjava/properties file you might specify:

 
 jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver
 

Rajesh

  • Dec 6th, 2005
 

you can connect database using DAO(Data Access Object).If u need clear info mail me to: rokkam_rajesh@rediffmail.com

  Was this answer useful?  Yes

karthik

  • Jun 12th, 2006
 

plz supraja tell me the coding without having the method Class.forname. send it to my mail id kkk_252003@yahoo.com

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions