wood burning stoves*
The moose likes JDBC and the fly likes Not loading the JDBC driver programatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Not loading the JDBC driver programatically" Watch "Not loading the JDBC driver programatically" New topic
Author

Not loading the JDBC driver programatically

Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
How can I run my java1.4 jdbc console application without loading the jdbc driver programatically...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
What do you mean by "loading the jdbc driver programatically"? It needs to be loaded somehow. You can avoid having the driver name and url in the code by passing them as properties on the command line, or keeping them in a properties file.


Ping & DNS - my free Android networking tools app
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
I have heard that there is a JVM option to load a JDBC driver so that you needn't write a code to load a driver in your program.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
Check the javadocs for the DriverManager class, particularly the section about the "jdbc.drivers" property.
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
I got it....Thanks Ulf


java -Djdbc.driver=[driver1]:[driver2]:... class file name

works excellent....

Check out this code too...I feel it makes your program pretty much independent.


import java.sql.*;
import java.util.*;
import java.io.*;
class TestSelect
{
public static void main(String[] args) throws SQLException,Exception
{
FileInputStream fis=new FileInputStream ("z:\\Labs\\javapract\\testprop.property");
Properties p1 =new Properties();
p1.load(fis);
String Database=p1.getProperty("Database");
System.setProperty("jdbc.drivers", Database);
String aURL=p1.getProperty("URL");
String username=p1.getProperty("Username");
String password=p1.getProperty("Password");
Properties p=new Properties();
p.put("user",username);
p.put("password",password);
Connection c=DriverManager.getConnection(aURL,p);
System.out.println(c);
Statement smt=c.createStatement();
ResultSet rs = smt.executeQuery("Select * from Emp_Master where Emp_Salary='10000.00'");
while(rs.next())
{
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}

}
}

[ March 13, 2006: Message edited by: Ashutosh Limaye ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
I'm curious: You go to some length keeping the DB properties out of the code, but hardcode the name of the properties file where they are kept? Or is this just an example?
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
Yes the DB properties are kept in a property file called "testprop.property"

Here is the file contents:
Database=sun.jdbc.odbc.JdbcOdbcDriver
Username = dba
Password = sql
URL= Jdbc:Odbc:MyData


It has Name-Value pair...Now you can add as many databases types repeating the above four lines for each driver ...and that won't affect your application...also I actually wanted to put the System.setProperty() in a static block....but again ...i'll try that out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Not loading the JDBC driver programatically