Why class not found?

From: Michael Hanna <zen(at)hwcn(dot)org>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Why class not found?
Date: 2002-08-24 12:15:41
Message-ID: 3575524A-B75B-11D6-A0A7-00039308EB2C@hwcn.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

OK, thanks to all regarding my previous Exceptions question.

New problem: I'm getting a ClassNotFoundException from this line:

Class.forName("org.postgresql.Driver"); //load the driver

However I do import this package and I'm certain it's in my classpath:

[taoki:~] michael% $CLASSPATH
/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Classes/classes.
jar:/usr/local/jakarta-
tomcat-4.0.3/common/lib/servlet.jar:/usr/local/pgsql/share/java/pgjdbc2.jar:
./: Command not found.
[taoki:~] michael%

Also that HelloPostgresql.class by Mr. Fankhauser works fine..

Any ideas...

here's the whole code:

--

Rigby.java

--

// Copyright Michael Hanna 2002
// do not use without permission of author

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*; // All we need for JDBC

public class Rigby extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse
response)throws IOException, ServletException
{
PrintWriter out;
DBThing dbt;
ResultSet results;

String database = "rigby";
String username = "postgres";
String password = "postgres";

response.setContentType("text/html");
out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Rigby</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Contents of friends:</h1>");

try {
Class.forName("org.postgresql.Driver"); //load the driver
out.println("<B>Inside try</B>");
dbt = new DBThing(database, username, password);
results = dbt.queryDB("select * from friends");

if (results != null)
{
out.println("<B>id" + "First Name" +"Surname" + "Email" +
"Phone"+"Bday</B>");
while (results.next())
{
out.println(results.getInt("id") +
results.getString("firstname") + results.getString("surname")+
results.getString("email")+results.getInt("tel")+results.getString("date") +
"\n");
}
}
else {
out.println("The friends database is empty.<br>");
}
results.close();

} catch (ClassNotFoundException cnf) {
out.println("***Exception:\n"+cnf);
cnf.printStackTrace();
} catch (SQLException se) {
out.println("***Exception:\n"+se);
se.printStackTrace();
}
out.println("hey2");
out.println("</body>");
out.println("</html>");

/*
try {
new DBThing(database, user, password);
} catch (ClassNotFoundException cnfe) {
throw new IOException("Could not instantiate driver: " +
cnfe.getMessage());
} catch (SQLException se) {
throw new IOException("Unable to connect to database or work
with it: " + se.getMessage());
}
*/

}
}

--

DBThing.java

--

// Copyright Michael Hanna 2002
// do not use without permission of author

import java.sql.*; // All we need for JDBC
import java.lang.*;

public class DBThing
{
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 DBThing(String database, String username, String password)
throws ClassNotFoundException, SQLException
{

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

}

public ResultSet queryDB(String query) throws SQLException
{
ResultSet rs;
rs = sql.executeQuery(query); // try to query DB
return rs;
}
}

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Nick Fankhauser 2002-08-24 18:18:27 Re: Why class not found?
Previous Message Barry Lind 2002-08-23 19:55:55 Re: Authentication Problems