Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Not loading the JDBC driver programatically

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I run my java1.4 jdbc console application without loading the jdbc driver programatically...
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ashutosh Limaye
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the javadocs for the DriverManager class, particularly the section about the "jdbc.drivers" property.
 
Ashutosh Limaye
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic