How to make jdbc connection a protable without specific drivers

Showing Answers 1 - 2 of 2 Answers

Supraja

  • Feb 17th, 2006
 

JDBC can't do its job without a driver, and the JDBC management layer must know the location of each and every database driver available to it. There are two ways that JDBC does this. First, upon initialization, the java.sql.DriverManager class searches for the sql.drivers property in the system's properties. If it exists, the DriverManager class will load it. Second, you can call the specific driver explicitly, thus avoiding the search. Drivers may be downloaded over the network (Internet or Intranet) as an option.

import java.net.URL;
import java.sql.*;

class Select {

public static void main(String argv[]) {
try {
// Create a URL specifying an ODBC data source name.
String url = "jdbc:odbc:wombat";

// Connect to the database at that URL.
Connection con = DriverManager.getConnection(url, "kgh", "");

// Execute a SELECT statement
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c, d, key FROM Table1");

// Step through the result rows.
System.out.println("Got results:");
while (rs.next()) {
// get the values from the current row: int a = rs.getInt(1);
Numeric b = rs.getNumeric(2);
char c[] = rs.getString(3).tocharArray();
boolean d = rs.getBoolean(4);
String key = rs.getString(5);

// Now print out the results:
System.out.print(" key=" + key);
System.out.print(" a-" + a);
System.out.print(" b=" + b);
System.out.print(" c=");
for (int I = 0; I < c.lngth; I++) {
System.out.print(c[i]);
}
System.out.print(" d=" + d);
System.out.print("\n");
}

stmt.close();
con.close();

} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}

}

  Was this answer useful?  Yes

Nagaraj

  • Oct 7th, 2006
 

there is no need to specify the drivername we can keep them in a properties file and get them in run time

i have done it if code required mailto

ch.nagaraj@gmail.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