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 7 of 11 Topic 16056 of 16078
Post > Topic >>

Re: Dynamic classpath

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

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" <nospam@[EMAIL PROTECTED]
> wrote in message 
> news:AFsUj.8543$ko5.6998@[EMAIL PROTECTED]
> 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" <YQDHXVLMUBXG@[EMAIL PROTECTED]
> wrote in message 
> news:68dkudF2sv349U1@[EMAIL PROTECTED]
> > 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
> >
> >
> >




 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:38:54 CDT 2008.