Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: Dynamic cla...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 11 Topic 16056 of 16078
Post > Topic >>

Re: Dynamic classpath

by "wizard of oz" <nospam@[EMAIL PROTECTED] > May 8, 2008 at 01:26 AM

This is a multi-part message in MIME format.

------=_NextPart_000_0018_01C8B0FE.6D5025D0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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 =3D new URL =
("file:/classpath/jars/JDBC/tdgssconfig.jar");
            URL jdbc2 =3D new URL =
("file:/classpath/jars/JDBC/tdgssjava.jar");
            URL jdbc3 =3D new URL =
("file:/classpath/jars/JDBC/terajdbc4.jar");
           =20
                            // Either of the following seem to work.
//            ClassLoader loader =3D new URLClassLoader (new URL [] { =
path, jdbc1, jdbc2, jdbc3 }, this.getClass().getClassLoader());
            ClassLoader loader =3D new URLClassLoader (new URL [] { =
path, jdbc1, jdbc2, jdbc3 });

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

            Connection c =3D =
DriverManager.getConnection("jdbc:teradata://dbc/", "uid", "pass");
            Statement s =3D c.createStatement();
            ResultSet r =3D 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" <YQDHXVLMUBXG@[EMAIL PROTECTED]
> wrote in message =
news:68dkudF2sv349U1@[EMAIL PROTECTED]
> Sorry, I meant Driver.connect()
>=20
> 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.
>=20
> Something like this:
>=20
> URLClassLoader l =3D new URLClassLoader(...);
> Class drvClass =3D l.loadClass("org.postgresql.Driver");
> java.sql.Driver drv =3D (java.sql.Driver)drvClass.newInstance();
> Properties props =3D new Properties();
> props.put("user", "postgres");
> props.put("password", "password");
>=20
> java.sql.Connection conn =3D =
drv.connect("jdbc:postgresql:localhost/mydb", props);
>=20
> Regards
> Thomas
>=20
>=20
>=20

------=_NextPart_000_0018_01C8B0FE.6D5025D0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.6000.16643" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>Cool, thanks.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&gt; <FONT face=3D"Times New Roman" =
size=3D3>The only=20
problem is then doing the connection as the DriverManager will refuse to =
use a=20
driver that was loaded by a different =
classloader.</FONT><BR></DIV></FONT>
<DIV><FONT face=3DArial size=3D2>While I was awaiting your reply, I =
thought I would=20
have a go at it myself. For what it is worth, the following also works. =
So the=20
"standard method" of connecting to a database (via DriverManager) seems =
to work=20
now&nbsp;(at least for my JDBC driver).</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>As mentioned I'm working in Java =
6.0.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New"=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
// For some reason my driver is in three parts</FONT></DIV>
<DIV><FONT face=3D"Courier New"=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p; URL=20
jdbc1 =3D new URL=20
("file:/classpath/jars/JDBC/tdgssconfig.jar");<BR>&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
URL jdbc2 =3D new URL=20
("file:/classpath/jars/JDBC/tdgssjava.jar");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
URL jdbc3 =3D new URL=20
("file:/classpath/jars/JDBC/terajdbc4.jar");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; // Either of the following seem to=20
work.<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;=20
ClassLoader loader =3D new URLClassLoader (new URL [] { path, jdbc1, =
jdbc2, jdbc3=20
},=20
this.getClass().getClassLoader());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
ClassLoader loader =3D new URLClassLoader (new URL [] { path, jdbc1, =
jdbc2, jdbc3=20
});<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;=20
Class jdbcClass =3D loader.loadClass =
("com.ncr.teradata.TeraDriver");</FONT></DIV>
<DIV><FONT face=3D"Courier New"=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
// The following is required&nbsp;otherwise I get a&nbsp;"No suitable=20
driver"&nbsp;SQLException.</FONT><FONT face=3D"Courier New"=20
size=3D2><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;=20
Object jdbcDriver =3D jdbcClass.newInstance=20
();<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;=20
Connection c =3D DriverManager.getConnection("jdbc:teradata://dbc/", =
"uid",=20
"pass");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;=20
Statement s =3D=20
c.createStatement();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;=20
ResultSet r =3D s.executeQuery("select * from=20
t1;");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;=20
while (r.next())=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;=20
System.out.println (r.getString(1) + ", " + r.getString=20
(2));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
r.close=20
();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
=20
s.close=20
();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
=20
c.close ();<BR><BR><FONT face=3DArial>Thanks again for your post Thomas, =
it really=20
helped point me in the right direction.</FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2><FONT =
face=3DArial></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2><FONT face=3DArial>Glenn=20
Mc</FONT></DIV></FONT>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>"Thomas Kellerer" &lt;</FONT><A=20
href=3D"mailto:YQDHXVLMUBXG@[EMAIL PROTECTED]
"><FONT face=3DArial=20
size=3D2>YQDHXVLMUBXG@[EMAIL PROTECTED]
></A><FONT face=3DArial =
size=3D2>&gt; wrote=20
in message </FONT><A =
href=3D"news:68dkudF2sv349U1@[EMAIL PROTECTED]
"><FONT=20
face=3DArial =
size=3D2>news:68dkudF2sv349U1@[EMAIL PROTECTED]
></A><FONT=20
face=3DArial size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>&gt; =
Sorry, I meant=20
Driver.connect()<BR>&gt; <BR>&gt; Once you have loaded the driver class =
using a=20
URLClassLoader, you can create a new instance and cast that to a Driver =
and then=20
ask the driver to connect.<BR>&gt; <BR>&gt; Something like this:<BR>&gt; =

<BR>&gt; URLClassLoader l =3D new URLClassLoader(...);<BR>&gt; Class =
drvClass =3D=20
l.loadClass("org.postgresql.Driver");<BR>&gt; java.sql.Driver drv =3D=20
(java.sql.Driver)drvClass.newInstance();<BR>&gt; Properties props =3D =
new=20
Properties();<BR>&gt; props.put("user", "postgres");<BR>&gt;=20
props.put("password", "password");<BR>&gt; <BR>&gt; java.sql.Connection =
conn =3D=20
drv.connect("jdbc:postgresql:localhost/mydb", props);<BR>&gt; <BR>&gt;=20
Regards<BR>&gt; Thomas<BR>&gt; <BR>&gt; <BR>&gt; =
<BR></FONT></BODY></HTML>

------=_NextPart_000_0018_01C8B0FE.6D5025D0--




 11 Posts in Topic:
Dynamic classpath
"wizard of oz"   2008-05-07 08:41:27 
Re: Dynamic classpath
Thomas Kellerer <YQDHX  2008-05-07 11:08:40 
Re: Dynamic classpath
"wizard of oz"   2008-05-07 11:38:28 
Re: Dynamic classpath
Thomas Kellerer <YQDHX  2008-05-07 14:17:17 
Re: Dynamic classpath
"wizard of oz"   2008-05-08 01:26:56 
Re: Dynamic classpath
Lew <lew@[EMAIL PROTEC  2008-05-07 21:38:01 
Re: Dynamic classpath
"wizard of oz"   2008-05-08 05:15:00 
Re: Dynamic classpath
Roedy Green <see_websi  2008-05-07 13:51:08 
Re: Dynamic classpath
Ian Shef <invalid@[EMA  2008-05-15 18:51:18 
Re: Dynamic classpath
"John B. Matthews&qu  2008-05-15 20:01:43 
Re: Dynamic classpath
Casper Bang <casper@[E  2008-05-08 21:37:16 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 23:44:16 CDT 2008.