JDBC isn't running

From: Marcelo Pereira <gandalf(at)sum(dot)desktop(dot)com(dot)br>
To: pgsql-general(at)postgresql(dot)org, pgsql-jdbc(at)postgresql(dot)org
Subject: JDBC isn't running
Date: 2003-01-14 19:41:47
Message-ID: Pine.LNX.4.20.0301141733070.28883-100000@ni.hmmg.sp.gov.br
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-jdbc

Hi again,

I have configured the variable:
CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar:.

and compiled (without errors) the following source code:

// -- begin
/**
* A demo program to show how jdbc works with postgresql
* Nick Fankhauser 10/25/01
* nickf(at)ontko(dot)com or nick(at)fankhausers(dot)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

$ javac HelloPostgresql.java
$ java HelloPostgresql.class
Exception in thread "main" java.lang.NoClassDefFoundError: HelloPostgresql/class

Where am I going wrong?? I have Java (J2SDK) and Ant
(Jakarta-Ant) configured properly, and postgresql (7.3.1) was configured
with ``--with-java'' option.

I have created a table on postgresql, with some fields, to test this code,
but when I run it crashes instantly with the message above.

The driver (postgresql.jar) was compiled and I have set CLASSPATH as
above. Why doesn't it code run?

Thanks in advance and
Best Regards,

Marcelo Pereira

-- Remember that only God and ^[:w saves.
__
(_.\ © Marcelo Pereira |
/ / ___ marcelo(at)pereira(dot)com |
/ (_/ _ \__ [Math|99]-IMECC |
_______\____/_\___)___Unicamp_______________/

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2003-01-14 20:02:54 Re: varchar and spaces problem..
Previous Message Barry Lind 2003-01-14 19:32:56 Re: [JDBC] JDBC isn't running

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2003-01-15 12:55:54 Re: [JDBC] JDBC
Previous Message Barry Lind 2003-01-14 19:32:56 Re: [JDBC] JDBC isn't running