Thread: Postgres-jdbc example!
Hi all, I've installed postgres with jdbc driver and added the postgresql.jar to the classpath (it's the only path in classpath) environment variable. I also have installed JDK from sun... I'm trying to do a database connection but I'm failing to do anything that works. Can you please give me a extremely simple example of how this stuff works or references to an example? I've tried to read tutorials and some other stuff but I failed to understand... I'm a Java and jdbc beginner so it's really not easy for me but I hope that with a simple example I can understand it. I'm not even sure that my jdbc driver is installed correctly and working so if you send me any kind of code to test it, I'd be glad! TIA, My best regards, -- Paulo J. Matos : pocm(_at_)rnl.ist.utl.pt Instituto Superior Tecnico - Lisbon Software & Computer Engineering - A.I. - > http://www.rnl.ist.utl.pt/~pocm --- Yes, God had a deadline... So, He wrote it all in Lisp!
Just for the record, here's my failed try to load the driver: (oh, and I already started postmaster with -i option) import java.sql.*; public class init { public static void main (String args[]) { Class.forName("org.postgresql.Driver"); } } I get: pdestroy@localhost:~/ist/bd/proj$ javac init.java init.java:6: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown Class.forName("org.postgresql.Driver"); ^ 1 error Best regards, Paulo pocm@rnl.ist.utl.pt (Paulo J. Matos) writes: > Hi all, > > I've installed postgres with jdbc driver and added the > postgresql.jar to the classpath (it's the only path in classpath) > environment variable. > > I also have installed JDK from sun... I'm trying to do a database > connection but I'm failing to do anything that works. > Can you please give me a extremely simple example of how this > stuff works or references to an example? > I've tried to read tutorials and some other stuff but I failed to > understand... I'm a Java and jdbc beginner so it's really not > easy for me but I hope that with a simple example I can > understand it. > I'm not even sure that my jdbc driver is installed correctly and > working so if you send me any kind of code to test it, I'd be > glad! > > TIA, > My best regards, > > -- > Paulo J. Matos : pocm(_at_)rnl.ist.utl.pt > Instituto Superior Tecnico - Lisbon > Software & Computer Engineering - A.I. > - > http://www.rnl.ist.utl.pt/~pocm > --- > Yes, God had a deadline... > So, He wrote it all in Lisp! > > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster > -- Paulo J. Matos : pocm(_at_)rnl.ist.utl.pt Instituto Superior Tecnico - Lisbon Software & Computer Engineering - A.I. - > http://www.rnl.ist.utl.pt/~pocm --- Yes, God had a deadline... So, He wrote it all in Lisp!
Paulo, Change your code to public class init { public static void main( String []args ) { try { Class.forName("org.postgresql.Driver"); }catch (Exception ex){ ex.printStackTrace(); } } } -----Original Message----- From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of Paulo J. Matos Sent: Thursday, January 17, 2002 8:31 PM To: pgsql-jdbc@postgresql.org Subject: Re: [JDBC] Postgres-jdbc example! Just for the record, here's my failed try to load the driver: (oh, and I already started postmaster with -i option) import java.sql.*; public class init { public static void main (String args[]) { Class.forName("org.postgresql.Driver"); } } I get: pdestroy@localhost:~/ist/bd/proj$ javac init.java init.java:6: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown Class.forName("org.postgresql.Driver"); ^ 1 error Best regards, Paulo pocm@rnl.ist.utl.pt (Paulo J. Matos) writes: > Hi all, > > I've installed postgres with jdbc driver and added the postgresql.jar > to the classpath (it's the only path in classpath) environment > variable. > > I also have installed JDK from sun... I'm trying to do a database > connection but I'm failing to do anything that works. Can you please > give me a extremely simple example of how this stuff works or > references to an example? I've tried to read tutorials and some other > stuff but I failed to understand... I'm a Java and jdbc beginner so > it's really not easy for me but I hope that with a simple example I > can understand it. > I'm not even sure that my jdbc driver is installed correctly and > working so if you send me any kind of code to test it, I'd be > glad! > > TIA, > My best regards, > > -- > Paulo J. Matos : pocm(_at_)rnl.ist.utl.pt > Instituto Superior Tecnico - Lisbon > Software & Computer Engineering - A.I. > - > http://www.rnl.ist.utl.pt/~pocm > --- > Yes, God had a deadline... > So, He wrote it all in Lisp! > > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster > -- Paulo J. Matos : pocm(_at_)rnl.ist.utl.pt Instituto Superior Tecnico - Lisbon Software & Computer Engineering - A.I. - > http://www.rnl.ist.utl.pt/~pocm --- Yes, God had a deadline... So, He wrote it all in Lisp! ---------------------------(end of broadcast)--------------------------- TIP 3: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to majordomo@postgresql.org so that your message can get through to the mailing list cleanly
Try looking at the documentation http://www.postgresql.org/idocs/index.php?jdbc.html If you look at the error below though, you'll see that its complaining that you are not catching the ClassNotFoundException which Class.forName() can throw. Easiest way to get going is to say that main can throw an exception. More normally, you would want to use try/catch to handle the exceptions. import java.sql.*; public class init { public static void main (String args[]) throws Exception { Class.forName("org.postgresql.Driver"); } } Cheers, Tom. On Fri, Jan 18, 2002 at 01:31:05AM +0000, Paulo J. Matos wrote: > Just for the record, here's my failed try to load the driver: > (oh, and I already started postmaster with -i option) > > import java.sql.*; > > public class init { > > public static void main (String args[]) { > Class.forName("org.postgresql.Driver"); > } > } > > I get: > pdestroy@localhost:~/ist/bd/proj$ javac init.java > init.java:6: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown > Class.forName("org.postgresql.Driver"); > ^ > 1 error > > > Best regards, > > Paulo > > > pocm@rnl.ist.utl.pt (Paulo J. Matos) writes: > > > Hi all, > > > > I've installed postgres with jdbc driver and added the > > postgresql.jar to the classpath (it's the only path in classpath) > > environment variable. > > > > I also have installed JDK from sun... I'm trying to do a database > > connection but I'm failing to do anything that works. > > Can you please give me a extremely simple example of how this > > stuff works or references to an example? > > I've tried to read tutorials and some other stuff but I failed to > > understand... I'm a Java and jdbc beginner so it's really not > > easy for me but I hope that with a simple example I can > > understand it. > > I'm not even sure that my jdbc driver is installed correctly and > > working so if you send me any kind of code to test it, I'd be > > glad! > > > > TIA, > > My best regards, > > > > -- > > Paulo J. Matos : pocm(_at_)rnl.ist.utl.pt > > Instituto Superior Tecnico - Lisbon > > Software & Computer Engineering - A.I. > > - > http://www.rnl.ist.utl.pt/~pocm > > --- > > Yes, God had a deadline... > > So, He wrote it all in Lisp! > > > > > > ---------------------------(end of broadcast)--------------------------- > > TIP 4: Don't 'kill -9' the postmaster > > > > -- > Paulo J. Matos : pocm(_at_)rnl.ist.utl.pt > Instituto Superior Tecnico - Lisbon > Software & Computer Engineering - A.I. > - > http://www.rnl.ist.utl.pt/~pocm > --- > Yes, God had a deadline... > So, He wrote it all in Lisp! > > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly -- Thomas O'Dowd. - Nooping - http://nooper.com tom@nooper.com - Testing - http://nooper.co.jp/labs
Hi Why dont you try "javap org.postgresql.Driver" from your command line with your classpath set and see if the driver is really available. If that gives a result like this *************************************************** Compiled from Driver.java public class org.postgresql.Driver extends java.lang.Object implements java.sql.Driver { static {}; public org.postgresql.Driver() throws java.sql.SQLException; public boolean acceptsURL(java.lang.String) throws java.sql.SQLException; public java.sql.Connection connect(java.lang.String, java.util.Properties) throws java.sql.SQLException; public java.lang.String database(); public int getMajorVersion(); public int getMinorVersion(); public java.sql.DriverPropertyInfo getPropertyInfo(java.lang.String, java.util.Properties)[] throws java.sql.SQLException; public static java.lang.String getVersion(); public java.lang.String host(); public boolean jdbcCompliant(); public static java.sql.SQLException notImplemented(); java.util.Properties parseURL(java.lang.String, java.util.Properties) throws java.sql.SQLException; public int port(); public java.lang.String property(java.lang.String); } *************************************************** you can access your driver. Your code seems enough to test the fact that the driver is available. Regards Rajesh --- "Paulo J. Matos" <pocm@rnl.ist.utl.pt> wrote: > Hi all, > > I've installed postgres with jdbc driver and added > the > postgresql.jar to the classpath (it's the only path > in classpath) > environment variable. > > I also have installed JDK from sun... I'm trying to > do a database > connection but I'm failing to do anything that > works. > Can you please give me a extremely simple example of > how this > stuff works or references to an example? > I've tried to read tutorials and some other stuff > but I failed to > understand... I'm a Java and jdbc beginner so it's > really not > easy for me but I hope that with a simple example I > can > understand it. > I'm not even sure that my jdbc driver is installed > correctly and > working so if you send me any kind of code to test > it, I'd be > glad! > > TIA, > My best regards, > > -- > Paulo J. Matos : pocm(_at_)rnl.ist.utl.pt > Instituto Superior Tecnico - Lisbon > Software & Computer Engineering - A.I. > - > http://www.rnl.ist.utl.pt/~pocm > --- > Yes, God had a deadline... > So, He wrote it all in Lisp! > > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster __________________________________________________ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/