473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Re: Dynamic classpath

Can you post an example of Connection.connect (String, properties)?

My java doc says java.sql.Connection is an interface and doesn't mention a
connect method.

TIA

"Thomas Kellerer" <YQ**********@spammotel.comwrote in message
news:68*************@mid.individual.net...
wizard of oz, 07.05.2008 10:41:
>I have a need to have a classpath that is determined by the set of jars a
user places into a directory.

By way of example, placing jar's into certain directories in a tomcat web
server will cause tomcat to include them into the web applications class
path. An example of this might be a charting package used by the web app
to generate charts.

In my specific example, I am building a query tool and I want to be able
to tell users to simply drop the JDBC drivers into a directory and my app
will "pick them up". Thus there would be no need to edit the classpath.

My target environment is Java 6.0

You will need to use a URLClassLoader to load the driver(s)
The only problem is then doing the connection as the DriverManager will
refuse to use a driver that was loaded by a different classloader.

You have two options to resolve this:

1) use Connection.connect(String, Properties) directly (bypassing
DriverManager) 2) create a wrapper class that pretends to be a driver but
delegates everything to the real instance (loaded by your own
classloader).
I am using option 1) without any problems.

Thomas
Jun 27 '08 #1
4 3317
Sorry, I meant Driver.connect()

Once you have loaded the driver class using a URLClassLoader, you can create a new instance and cast that to a Driver and then ask the driver to connect.

Something like this:

URLClassLoader l = new URLClassLoader(...);
Class drvClass = l.loadClass("org.postgresql.Driver");
java.sql.Driver drv = (java.sql.Driver)drvClass.newInstance();
Properties props = new Properties();
props.put("user", "postgres");
props.put("password", "password");

java.sql.Connection conn = drv.connect("jdbc:postgresql:localhost/mydb", props);

Regards
Thomas

wizard of oz, 07.05.2008 13:38:
Can you post an example of Connection.connect (String, properties)?

My java doc says java.sql.Connection is an interface and doesn't mention a
connect method.

TIA

"Thomas Kellerer" <YQ**********@spammotel.comwrote in message
news:68*************@mid.individual.net...
>wizard of oz, 07.05.2008 10:41:
>>I have a need to have a classpath that is determined by the set of
jars a user places into a directory.

By way of example, placing jar's into certain directories in a tomcat
web server will cause tomcat to include them into the web
applications class path. An example of this might be a charting
package used by the web app to generate charts.

In my specific example, I am building a query tool and I want to be
able to tell users to simply drop the JDBC drivers into a directory
and my app will "pick them up". Thus there would be no need to edit
the classpath.

My target environment is Java 6.0

You will need to use a URLClassLoader to load the driver(s)
The only problem is then doing the connection as the DriverManager
will refuse to use a driver that was loaded by a different classloader.

You have two options to resolve this:

1) use Connection.connect(String, Properties) directly (bypassing
DriverManager) 2) create a wrapper class that pretends to be a driver
but delegates everything to the real instance (loaded by your own
classloader).
I am using option 1) without any problems.

Thomas
Jun 27 '08 #2
Cool, thanks.
The only problem is then doing the connection as the DriverManager will refuse to use a driver that was loaded by a different classloader.
While I was awaiting your reply, I thought I would have a go at it myself. For what it is worth, the following also works. So the "standard method" of connecting to a database (via DriverManager) seems to work now (at least for my JDBC driver).
As mentioned I'm working in Java 6.0.
// For some reason my driver is in three parts
URL jdbc1 = new URL ("file:/classpath/jars/JDBC/tdgssconfig.jar");
URL jdbc2 = new URL ("file:/classpath/jars/JDBC/tdgssjava.jar");
URL jdbc3 = new URL ("file:/classpath/jars/JDBC/terajdbc4.jar");

// Either of the following seem to work.
// ClassLoader loader = new URLClassLoader (new URL [] { path, jdbc1, jdbc2, jdbc3 }, this.getClass().getClassLoader());
ClassLoader loader = new URLClassLoader (new URL [] { path, jdbc1, jdbc2, jdbc3 });

Class jdbcClass = loader.loadClass ("com.ncr.teradata.TeraDriver");
// The following is required otherwise I get a "No suitable driver" SQLException.
Object jdbcDriver = jdbcClass.newInstance ();

Connection c = DriverManager.getConnection("jdbc:teradata://dbc/", "uid", "pass");
Statement s = c.createStatement();
ResultSet r = s.executeQuery("select * from t1;");
while (r.next()) {
System.out.println (r.getString(1) + ", " + r.getString (2));
}
r.close ();
s.close ();
c.close ();

Thanks again for your post Thomas, it really helped point me in the right direction.

Glenn Mc
"Thomas Kellerer" <YQ**********@spammotel.comwrote in message news:68*************@mid.individual.net...
Sorry, I meant Driver.connect()

Once you have loaded the driver class using a URLClassLoader, you can create a new instance and cast that to a Driver and then ask the driver to connect.

Something like this:

URLClassLoader l = new URLClassLoader(...);
Class drvClass = l.loadClass("org.postgresql.Driver");
java.sql.Driver drv = (java.sql.Driver)drvClass.newInstance();
Properties props = new Properties();
props.put("user", "postgres");
props.put("password", "password");

java.sql.Connection conn = drv.connect("jdbc:postgresql:localhost/mydb", props);

Regards
Thomas

Jun 27 '08 #3
Lew
wizard of oz wrote:
Cool, thanks.
Please post to Usenet in plain text, not HTML.

--
Lew
Jun 27 '08 #4
Spoke too soon. The following code *does* work in a test project, but not my
main project. Both projects are built using NetBeans 6.0.

The only difference I can pick is that the main project is a Swing project,
whereas the test project doesn't use Swing. Thomas's use of Driver.connect
does however work as he advertised.

Thanks again for all of the replies.

"wizard of oz" <no****@gtajb.comwrote in message
news:AF*****************@news-server.bigpond.net.au...
Cool, thanks.
The only problem is then doing the connection as the DriverManager will
refuse to use a driver that was loaded by a different classloader.

While I was awaiting your reply, I thought I would have a go at it myself.
For what it is worth, the following also works. So the "standard method"
of connecting to a database (via DriverManager) seems to work now (at
least for my JDBC driver).
As mentioned I'm working in Java 6.0.
// For some reason my driver is in three parts
URL jdbc1 = new URL
("file:/classpath/jars/JDBC/tdgssconfig.jar");
URL jdbc2 = new URL
("file:/classpath/jars/JDBC/tdgssjava.jar");
URL jdbc3 = new URL
("file:/classpath/jars/JDBC/terajdbc4.jar");

// Either of the following seem to work.
// ClassLoader loader = new URLClassLoader (new URL [] { path,
jdbc1, jdbc2, jdbc3 }, this.getClass().getClassLoader());
ClassLoader loader = new URLClassLoader (new URL [] { path,
jdbc1, jdbc2, jdbc3 });

Class jdbcClass = loader.loadClass
("com.ncr.teradata.TeraDriver");
// The following is required otherwise I get
a "No suitable driver" SQLException.
Object jdbcDriver = jdbcClass.newInstance ();

Connection c =
DriverManager.getConnection("jdbc:teradata://dbc/", "uid", "pass");
Statement s = c.createStatement();
ResultSet r = s.executeQuery("select * from t1;");
while (r.next()) {
System.out.println (r.getString(1) + ", " + r.getString
(2));
}
r.close ();
s.close ();
c.close ();

Thanks again for your post Thomas, it really helped point me in the right
direction.

Glenn Mc
"Thomas Kellerer" <YQ**********@spammotel.comwrote in message
news:68*************@mid.individual.net...
Sorry, I meant Driver.connect()

Once you have loaded the driver class using a URLClassLoader, you can
create a new instance and cast that to a Driver and then ask the driver
to connect.

Something like this:

URLClassLoader l = new URLClassLoader(...);
Class drvClass = l.loadClass("org.postgresql.Driver");
java.sql.Driver drv = (java.sql.Driver)drvClass.newInstance();
Properties props = new Properties();
props.put("user", "postgres");
props.put("password", "password");

java.sql.Connection conn = drv.connect("jdbc:postgresql:localhost/mydb",
props);

Regards
Thomas

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Herman | last post by:
Hi everyone, I recently installed the Sun J2SE SDK on my machine, and I am having trouble running the java.exe interpreter for my Java compiled code. I remember that I had to set my environment...
1
by: Christopher V. Kimball | last post by:
How do I put many jar files into the classpath without having to name each one? This is in Windows 200, in which setting any environmental variable is tedious. Chris Kimball
2
by: David Cook | last post by:
Windows (XP and NT) allows one to specify environmental variable (such as PATH and CLASSPATH) at both the SYSTEM-wide level as well as at the per-USER level. And the behavior has always been...
4
by: Abdelhalim MIMOUNI | last post by:
hi, i'm new to java, and using sun last JDK, and i'm trying to understand how work this feature when we want to compile at command line ? i'm trying to set the classpath to the directory of the...
1
by: Dave Keays | last post by:
I am setting-up an experimental web service using Apache Axis but I'm having problems. AXIS can't find a library that I've verified exists on my computer and that CLASSPATH points to it. I'm using...
3
by: jeremiah johnson | last post by:
Okay this is going to sound really dumb. Skeet, you can poke fun of me in your own special way if you see fit. Is there a way to reproduce the CLASSPATH functionality of Java within C#? Would...
7
by: dlarsson | last post by:
Okay folks, I thought I was doing something very, very simple, but I cannot seem to get this to work at all. Can anyone identify what I am doing wrong here-? _________________________________ ...
9
by: KevinRobinson | last post by:
Hi, Can anyone please tell me how to add or change a Java classpath in SUSE Linux 9.3. I have set up a Tomcat server but my Java Classes will not run although they do on a windows box. ...
1
Ganon11
by: Ganon11 | last post by:
Hey all, I'm setting up my computer to run Java for a class I'm taking, and I've gotten into a bit of a snag. I've set the CLASSPATH system variable to include the base folder of my homeworks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.