Asher Saban
Asher Saban

Reputation: 4793

connect to DB with java without external jar - is there such a thing?

i'm connecting to a database with java with the following code:

 Class.forName(some driver name);

now, in order for it to run i have to include the relevant jar to the build path. my question is, is there a driver (maybe built in the sdk/jre) that does not require including a jar?

thank you

Upvotes: 0

Views: 245

Answers (3)

lxbndr
lxbndr

Reputation: 2208

Possible solution: include connector jar in your jar, unpack it somewhere in runtime and use URLClassLoader to load driver.

Example of completely abstract invocation of MSSQL driver:

URLClassLoader cl = ...; // create and initialize URLClassLoader
Class c = cl.loadClass("com.microsoft.sqlserver.jdbc.SQLServerDataSource").newInstance().getClass();
c.getMethod("setUser", String.class).invoke(o, connectionParameters.user);
c.getMethod("setPassword", String.class).invoke(o, connectionParameters.password);
c.getMethod("setServerName", String.class).invoke(o, connectionParameters.serverName);
c.getMethod("setDatabaseName", String.class).invoke(o, connectionParameters.databaseName);
return (Connection) c.getMethod("getConnection").invoke(o);

It uses MS-driver-specific methods, but not requires driver to be on your classpath.

Upvotes: 0

Mulesoft Developer
Mulesoft Developer

Reputation: 2824

This is not possible to connect with out any driver because driver work as a bridge to connect our application with database servers

Upvotes: 0

skaffman
skaffman

Reputation: 403481

The only JDBC driver supplied built-in to the Oracle Java 6 JRE is the JDBC-ODBC bridge. Its use is not recommended, however.

For any other DBMS, you need a proper JDBC driver JAR.

Upvotes: 4

Related Questions