Re: Connection Problem with JDBC
От | Nick Fankhauser |
---|---|
Тема | Re: Connection Problem with JDBC |
Дата | |
Msg-id | NEBBLAAHGLEEPCGOBHDGAEONEGAA.nickf@ontko.com обсуждение исходный текст |
Ответ на | Connection Problem with JDBC ("Steven Murphy" <stevenmurphy@hotmail.com>) |
Список | pgsql-jdbc |
In the place where you catch the SQL exception, you just need to add a couple more exception handlers, so it would look like this: } catch (SQLException se) { System.err.println( se.getMessage() ); } catch (ClassNotFoundException cnfe) { System.err.println( cnfe.getMessage() ); } catch (InstantiationException ie) { System.err.println( ie.getMessage() ); Then try to compile again. -NickF > -----Original Message----- > From: Steven Murphy [mailto:stevenmurphy@hotmail.com] > Sent: Monday, February 25, 2002 9:02 AM > To: nickf@ontko.com; pgsql-jdbc@postgresql.org > Subject: Re: [JDBC] Connection Problem with JDBC > > > Hi all, > > I have added the code to load the drivers using Class.forName in > main, but > when I compile the code I get the following errors :- > > ConnectDemo.java:27: unreported exception > java.lang.ClassNotFoundException; > must be caught or declared to be thrown > Class.forName("org.postgresql.Driver").newInstance(); > ^ > ConnectDemo.java:27: unreported exception > java.lang.InstantiationException; > must be caught or declared to be thrown > Class.forName("org.postgresql.Driver").newInstance(); > ^ > 2 errors > > Here is how the program looks now I have added the Class.forName bit :- > > // Import the JDBC classes. > // > import java.sql.*; > > public class ConnectDemo { > > public static void main(String[] argv) { > > if (argv.length < 3) { > usage(); > } > String url = argv[0]; > String user = argv[1]; > String pass = argv[2]; > > // Invoke getConnection() to create the > // connection object. > // > Connection conn; > try { > Driver driver = (Driver) > Class.forName("org.postgresql.Driver").newInstance(); > DriverManager.registerDriver(driver); > conn = DriverManager.getConnection(url, user, pass); > System.out.println("Connection successful."); > System.out.println("Connection as String: " + conn); > } catch (SQLException e) { > System.err.println( e.getMessage() ); > System.exit(-1); > } > > } > > static void usage() { > > System.err.println("Usage:"); > System.err.print("java -Djdbc.drivers=DRIVERCLASS PROGRAM "); > System.err.println("URL USER PASS"); > System.exit(-1); > > } > > Do I need to import some other things to get this to work? > Sorry I'm a bit of a novice with all of this. > > Cheers > Steve. > > -------------------------------------------------------------------- > > From: "Nick Fankhauser" <nickf@ontko.com> > Reply-To: <nickf@ontko.com> > To: "Steven Murphy" <stevenmurphy@hotmail.com>, > <pgsql-jdbc@postgresql.org> > Subject: Re: [JDBC] Connection Problem with JDBC > Date: Mon, 11 Feb 2002 15:44:04 -0500 > > > The command I am using to run the program is > > java -Djdbc.drivers=org.postgresql.Driver ConnectDemo > > jdbc:postgresql:javatest postgres ' ' > > I think if it was a classpath problem, you'd be getting a > "ClassNotFoundException" I believe the "No Suitable Driver" > indicates that > among the drivers you have loaded, none matches the database url > passed to > DriverManager.getConnection. ...But the command & url above look > just fine & > I can't see any opportunity in your program for the url to get mangled. > > Have you tried loading the drivers using Class.forName in main? > I'm not sure > what it would tell us if it worked, but if it didn't work that > way, I think > I'd download the driver again. > > -Nick > > -------------------------------------------------------------------------- > Nick Fankhauser nickf@ontko.com Phone 1.765.935.4283 Fax 1.765.962.9788 > Ray Ontko & Co. Software Consulting Services http://www.ontko.com/ > > ---------------------------(end of broadcast)--------------------------- > TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) > > _________________________________________________________________ > MSN Photos is the easiest way to share and print your photos: > http://photos.msn.com/support/worldwide.aspx >
В списке pgsql-jdbc по дате отправления: