Thread: oops - Trying to do a simple connect to 7.2.1 DB -- unix
I forgot to include the code..here it is. --- /** * A demo program to show how jdbc works with postgresql * Nick Fankhauser 10/25/01 * nickf@ontko.com or nick@fankhausers.com * This program may be freely copied and modified * Please keep this header intact on unmodified versions * The rest of the documentation that came with this demo program * may be found at http://www.fankhausers.com/postgresql/jdbc */ import java.sql.*; // All we need for JDBC import java.text.*; import java.io.*; public class HelloPostgresql { Connection db; // A connection to the database Statement sql; // Our statement to run queries with DatabaseMetaData dbmd; // This is basically info the driver delivers // about the DB it just connected to. I use // it to get the DB version to confirm the // connection in this example. public HelloPostgresql(String argv[]) throws ClassNotFoundException, SQLException { String database = argv[0]; String username = argv[1]; String password = argv[2]; Class.forName("org.postgresql.Driver"); //load the driver db = DriverManager.getConnection("jdbc:postgresql:"+database, username, password); //connect to the db dbmd = db.getMetaData(); //get MetaData to confirm connection System.out.println("Connection to "+dbmd.getDatabaseProductName()+" "+ dbmd.getDatabaseProductVersion()+" successful.\n"); sql = db.createStatement(); //create a statement that we can use later String sqlText = "create table jdbc_demo (code int, text varchar(20))"; System.out.println("Executing this command: "+sqlText+"\n"); sql.executeUpdate(sqlText); sqlText = "insert into jdbc_demo values (1,'One')"; System.out.println("Executing this command: "+sqlText+"\n"); sql.executeUpdate(sqlText); sqlText = "insert into jdbc_demo values (3,'Four')"; System.out.println("Executing this command twice: "+sqlText+"\n"); sql.executeUpdate(sqlText); sql.executeUpdate(sqlText); sqlText = "update jdbc_demo set text = 'Three' where code = 3"; System.out.println("Executing this command: "+sqlText+"\n"); sql.executeUpdate(sqlText); System.out.println (sql.getUpdateCount()+ " rows were update by this statement\n"); System.out.println("\n\nNow demostrating a prepared statement..."); sqlText = "insert into jdbc_demo values (?,?)"; System.out.println("The Statement looks like this: "+sqlText+"\n"); System.out.println("Looping three times filling in the fields...\n"); PreparedStatement ps = db.prepareStatement(sqlText); for (int i=10;i<13;i++) { System.out.println(i+"...\n"); ps.setInt(1,i); //set column one (code) to i ps.setString(2,"HiHo"); //Column two gets a string ps.executeUpdate(); } ps.close(); System.out.println("Now executing the command: "+ "select * from jdbc_demo"); ResultSet results = sql.executeQuery("select * from jdbc_demo"); if (results != null) { while (results.next()) { System.out.println("code = "+results.getInt("code")+ "; text = "+results.getString(2)+"\n"); } } results.close(); sqlText = "drop table jdbc_demo"; System.out.println("Executing this command: "+sqlText+"\n"); sql.executeUpdate(sqlText); db.close(); } public static void correctUsage() { System.out.println("\nIncorrect number of arguments.\nUsage:\n "+ "java \n"); System.exit(1); } public static void main (String args[]) { if (args.length != 3) correctUsage(); try { HelloPostgresql demo = new HelloPostgresql(args); } catch (Exception ex) { System.out.println("***Exception:\n"+ex); ex.printStackTrace(); } } }
And what's the error you get when you compile/run ? Dave On Sat, 2002-08-17 at 09:17, Michael Hanna wrote: > I forgot to include the code..here it is. > > --- > > > /** > * A demo program to show how jdbc works with postgresql > * Nick Fankhauser 10/25/01 > * nickf@ontko.com or nick@fankhausers.com > * This program may be freely copied and modified > * Please keep this header intact on unmodified versions > * The rest of the documentation that came with this demo program > * may be found at http://www.fankhausers.com/postgresql/jdbc > */ > > > > import java.sql.*; // All we need for JDBC > import java.text.*; > import java.io.*; > > public class HelloPostgresql > { > Connection db; // A connection to the database > Statement sql; // Our statement to run queries with > DatabaseMetaData dbmd; // This is basically info the driver > delivers > // about the DB it just connected to. I use > // it to get the DB version to confirm the > // connection in this example. > > public HelloPostgresql(String argv[]) > throws ClassNotFoundException, SQLException > { > String database = argv[0]; > String username = argv[1]; > String password = argv[2]; > Class.forName("org.postgresql.Driver"); //load the driver > db = DriverManager.getConnection("jdbc:postgresql:"+database, > username, > password); //connect to the db > dbmd = db.getMetaData(); //get MetaData to confirm connection > System.out.println("Connection to > "+dbmd.getDatabaseProductName()+" "+ > dbmd.getDatabaseProductVersion()+" > successful.\n"); > sql = db.createStatement(); //create a statement that we can use > later > > > String sqlText = "create table jdbc_demo (code int, text > varchar(20))"; > System.out.println("Executing this command: "+sqlText+"\n"); > sql.executeUpdate(sqlText); > > > sqlText = "insert into jdbc_demo values (1,'One')"; > System.out.println("Executing this command: "+sqlText+"\n"); > sql.executeUpdate(sqlText); > > > sqlText = "insert into jdbc_demo values (3,'Four')"; > System.out.println("Executing this command twice: "+sqlText+"\n"); > sql.executeUpdate(sqlText); > sql.executeUpdate(sqlText); > > > sqlText = "update jdbc_demo set text = 'Three' where code = 3"; > System.out.println("Executing this command: "+sqlText+"\n"); > sql.executeUpdate(sqlText); > System.out.println (sql.getUpdateCount()+ > " rows were update by this statement\n"); > > > System.out.println("\n\nNow demostrating a prepared statement..."); > sqlText = "insert into jdbc_demo values (?,?)"; > System.out.println("The Statement looks like this: "+sqlText+"\n"); > System.out.println("Looping three times filling in the fields...\n"); > PreparedStatement ps = db.prepareStatement(sqlText); > for (int i=10;i<13;i++) > { > System.out.println(i+"...\n"); > ps.setInt(1,i); //set column one (code) to i > ps.setString(2,"HiHo"); //Column two gets a string > ps.executeUpdate(); > } > ps.close(); > > > System.out.println("Now executing the command: "+ > "select * from jdbc_demo"); > ResultSet results = sql.executeQuery("select * from jdbc_demo"); > if (results != null) > { > while (results.next()) > { > System.out.println("code = "+results.getInt("code")+ > "; text = "+results.getString(2)+"\n"); > } > } > results.close(); > > > sqlText = "drop table jdbc_demo"; > System.out.println("Executing this command: "+sqlText+"\n"); > sql.executeUpdate(sqlText); > > > db.close(); > } > > public static void correctUsage() > { > System.out.println("\nIncorrect number of arguments.\nUsage:\n "+ > "java \n"); > System.exit(1); > } > > public static void main (String args[]) > { > if (args.length != 3) correctUsage(); > try > { > HelloPostgresql demo = new HelloPostgresql(args); > } > catch (Exception ex) > { > System.out.println("***Exception:\n"+ex); > ex.printStackTrace(); > } > } > } > > > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org > >
I get a NoClassDefFoundError. Michael -rw-r--r-- 1 michael staff 3795 Aug 11 07:40 HelloPostgresql.class -rw-r--r-- 1 michael staff 4017 Aug 11 07:39 HelloPostgresql.java -rwxr-xr-- 1 michael staff 115 Jul 24 17:34 start_tomcat -rwxr-xr-- 1 michael staff 116 Jul 24 17:35 stop_tomcat -rwxr-xr-x 1 michael wheel 859 Jul 30 07:44 vt.pl [x1-6-00-03-93-08-eb-2c:~/bin] michael% java HelloPostgresql Exception in thread "main" java.lang.NoClassDefFoundError: HelloPostgresql [x1-6-00-03-93-08-eb-2c:~/bin] michael% On Saturday, August 17, 2002, at 09:21 AM, Dave Cramer wrote: > And what's the error you get when you compile/run ? > > Dave > On Sat, 2002-08-17 at 09:17, Michael Hanna wrote: >> I forgot to include the code..here it is. >> >> --- >> >> >> /** >> * A demo program to show how jdbc works with postgresql >> * Nick Fankhauser 10/25/01 >> * nickf@ontko.com or nick@fankhausers.com >> * This program may be freely copied and modified >> * Please keep this header intact on unmodified versions >> * The rest of the documentation that came with this demo program >> * may be found at http://www.fankhausers.com/postgresql/jdbc >> */ >> >> >> >> import java.sql.*; // All we need for JDBC >> import java.text.*; >> import java.io.*; >> >> public class HelloPostgresql >> { >> Connection db; // A connection to the database >> Statement sql; // Our statement to run queries with >> DatabaseMetaData dbmd; // This is basically info the driver >> delivers >> // about the DB it just connected to. I >> use >> // it to get the DB version to confirm >> the >> // connection in this example. >> >> public HelloPostgresql(String argv[]) >> throws ClassNotFoundException, SQLException >> { >> String database = argv[0]; >> String username = argv[1]; >> String password = argv[2]; >> Class.forName("org.postgresql.Driver"); //load the driver >> db = DriverManager.getConnection("jdbc:postgresql:"+database, >> username, >> password); //connect to the db >> dbmd = db.getMetaData(); //get MetaData to confirm connection >> System.out.println("Connection to >> "+dbmd.getDatabaseProductName()+" "+ >> dbmd.getDatabaseProductVersion()+" >> successful.\n"); >> sql = db.createStatement(); //create a statement that we can use >> later >> >> >> String sqlText = "create table jdbc_demo (code int, text >> varchar(20))"; >> System.out.println("Executing this command: "+sqlText+"\n"); >> sql.executeUpdate(sqlText); >> >> >> sqlText = "insert into jdbc_demo values (1,'One')"; >> System.out.println("Executing this command: "+sqlText+"\n"); >> sql.executeUpdate(sqlText); >> >> >> sqlText = "insert into jdbc_demo values (3,'Four')"; >> System.out.println("Executing this command twice: "+sqlText+"\n"); >> sql.executeUpdate(sqlText); >> sql.executeUpdate(sqlText); >> >> >> sqlText = "update jdbc_demo set text = 'Three' where code = 3"; >> System.out.println("Executing this command: "+sqlText+"\n"); >> sql.executeUpdate(sqlText); >> System.out.println (sql.getUpdateCount()+ >> " rows were update by this statement\n"); >> >> >> System.out.println("\n\nNow demostrating a prepared >> statement..."); >> sqlText = "insert into jdbc_demo values (?,?)"; >> System.out.println("The Statement looks like this: >> "+sqlText+"\n"); >> System.out.println("Looping three times filling in the >> fields...\n"); >> PreparedStatement ps = db.prepareStatement(sqlText); >> for (int i=10;i<13;i++) >> { >> System.out.println(i+"...\n"); >> ps.setInt(1,i); //set column one (code) to i >> ps.setString(2,"HiHo"); //Column two gets a string >> ps.executeUpdate(); >> } >> ps.close(); >> >> >> System.out.println("Now executing the command: "+ >> "select * from jdbc_demo"); >> ResultSet results = sql.executeQuery("select * from jdbc_demo"); >> if (results != null) >> { >> while (results.next()) >> { >> System.out.println("code = "+results.getInt("code")+ >> "; text = "+results.getString(2)+"\n"); >> } >> } >> results.close(); >> >> >> sqlText = "drop table jdbc_demo"; >> System.out.println("Executing this command: "+sqlText+"\n"); >> sql.executeUpdate(sqlText); >> >> >> db.close(); >> } >> >> public static void correctUsage() >> { >> System.out.println("\nIncorrect number of arguments.\nUsage:\n "+ >> "java \n"); >> System.exit(1); >> } >> >> public static void main (String args[]) >> { >> if (args.length != 3) correctUsage(); >> try >> { >> HelloPostgresql demo = new HelloPostgresql(args); >> } >> catch (Exception ex) >> { >> System.out.println("***Exception:\n"+ex); >> ex.printStackTrace(); >> } >> } >> } >> >> >> >> ---------------------------(end of >> broadcast)--------------------------- >> TIP 6: Have you searched our list archives? >> >> http://archives.postgresql.org >> >> > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/users-lounge/docs/faq.html >
This is *definitely* not a PostgreSQL issue, but... Does it tell you anything that the class is cannot find is HelloPostgresql? That is _your_ class, not a PostgreSQL class. Try "echo $CLASSPATH". You'll need both the directory containing your class and the PostgreSQL JAR on the CLASSPATH (the current directory is *not* included by default). Alternately, you could pass them in on the command line: java -cp .:/somewhere/pgsql.jar HelloPostgresql Aaron On Sat, 17 Aug 2002, Michael Hanna wrote: > I get a NoClassDefFoundError. > > Michael > > -rw-r--r-- 1 michael staff 3795 Aug 11 07:40 HelloPostgresql.class > -rw-r--r-- 1 michael staff 4017 Aug 11 07:39 HelloPostgresql.java > -rwxr-xr-- 1 michael staff 115 Jul 24 17:34 start_tomcat > -rwxr-xr-- 1 michael staff 116 Jul 24 17:35 stop_tomcat > -rwxr-xr-x 1 michael wheel 859 Jul 30 07:44 vt.pl > [x1-6-00-03-93-08-eb-2c:~/bin] michael% java HelloPostgresql > Exception in thread "main" java.lang.NoClassDefFoundError: > HelloPostgresql > [x1-6-00-03-93-08-eb-2c:~/bin] michael%