From: | "Nick Fankhauser" <nickf(at)ontko(dot)com> |
---|---|
To: | "Michael Hanna" <zen(at)hwcn(dot)org>, <pgsql-jdbc(at)postgresql(dot)org> |
Subject: | Re: Why class not found? |
Date: | 2002-08-24 18:18:27 |
Message-ID: | NEBBLAAHGLEEPCGOBHDGGEDHFKAA.nickf@ontko.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-jdbc |
Michael-
A common problem in moving from code executed from the command line to
servlets is that the servlets are all executed by the tomcat user (often
www-data or apache by default). This confuses people because the classpath
looks fine from the command line, but that isn't the environment your code
runs from.
To make sure that Tomcat sees the jdbc driver, either add it to the class
path in the .profile for that users, or better, place the driver (or a link
to it) in /usr/share/tomcat/lib. (I'm assuming you are on linux here.)
Hope this helps-
-Nick
--------------------------------------------------------------------------
Nick Fankhauser nickf(at)ontko(dot)com Phone 1.765.935.4283 Fax 1.765.962.9788
Ray Ontko & Co. Software Consulting Services http://www.ontko.com/
> -----Original Message-----
> From: pgsql-jdbc-owner(at)postgresql(dot)org
> [mailto:pgsql-jdbc-owner(at)postgresql(dot)org]On Behalf Of Michael Hanna
> Sent: Saturday, August 24, 2002 7:16 AM
> To: pgsql-jdbc(at)postgresql(dot)org
> Subject: [JDBC] Why class not found?
>
>
> 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/pg
> jdbc2.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;
> }
> }
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
From | Date | Subject | |
---|---|---|---|
Next Message | Larry Riedel | 2002-08-24 18:55:49 | 7.3 Cursors/ResultSets/CallableStatements |
Previous Message | Michael Hanna | 2002-08-24 12:15:41 | Why class not found? |