Skip site navigation (1) Skip section navigation (2)

Problem with connecting to postgres using jdbc7.0-1.2.jar

From: Peter Adamek <peter(dot)adamek(at)utoronto(dot)ca>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Problem with connecting to postgres using jdbc7.0-1.2.jar
Date: 2001-12-24 05:33:41
Message-ID: 3C26BE35.FF5CDB2B@utoronto.ca (view raw or flat)
Thread:
Lists: pgsql-jdbc
Hey folks,

This may be a rookie mistake.  I'm trying to use Tomcat 4.0 stand alone
(without using Apache) along with postgres 7.0.3 and jdbc7.0-1.2.jar to
connect from a servlet to the postgres server.
Here is my servlet (it compiles without any problems).

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class TestServlet extends HttpServlet {

    Connection db = null;

    public void init (ServletConfig config) throws ServletException {
        super.init(config);

        /* Load the driver */
        try {Class.forName("org.postgresql.Driver");}
        catch (ClassNotFoundException e) {}

        /* Connect to the database */
        try {db =
DriverManager.getConnection("jdbc:postgresql:peter","padamek","");}
        catch (SQLException e) {}
    }

    public void service (HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException {
        // set content type and other response header fields first
        response.setContentType("text/html");

        // get the communication channel with the requesting client
        PrintWriter out = response.getWriter();

        /* Standard top bit */
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML
3.2//EN\">\n"
                    + "<HTML>\n"
                    + "<HEAD>\n"
                    + " <TITLE>Test</TITLE>\n"
                    + "</HEAD>\n"
                    + "<BODY BGCOLOR=\"#FFFFFF\">\n");

        try {
            Statement sql = db.createStatement();
            ResultSet rs = sql.executeQuery("select count(*) from
tblCourses");
            rs.next();
            out.println("<p>There are" +
                        rs.getInt("count")+
                        "rows in this table</p>");
        }
        catch (SQLException e) { out.println(e.toString());}

        /* Standard bottom bit */
        out.println("</BODY>\n"
                    + "</HTML>\n");
        out.close();
    }

    public void destroy() {
        try {db.close();}
        catch (SQLException e) {}
    }
}

When I compile it with the verbose javac option, here is what I get:

[parsing started TestServlet.java]
[parsing completed 116ms]
[loading
/var/jakarta-tomcat-4.0.1/common/lib/servlet.jar(javax/servlet/http/HttpServlet.class)]

[loading
/var/jakarta-tomcat-4.0.1/common/lib/servlet.jar(javax/servlet/GenericServlet.class)]

[loading /usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/lang/Object.class)]
[loading
/var/jakarta-tomcat-4.0.1/common/lib/servlet.jar(javax/servlet/Servlet.class)]

[loading
/var/jakarta-tomcat-4.0.1/common/lib/servlet.jar(javax/servlet/ServletConfig.class)]

[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/io/Serializable.class)]
[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/sql/Connection.class)]
[loading
/var/jakarta-tomcat-4.0.1/common/lib/servlet.jar(javax/servlet/ServletException.class)]

[loading
/var/jakarta-tomcat-4.0.1/common/lib/servlet.jar(javax/servlet/http/HttpServletRequest.class)]

[loading
/var/jakarta-tomcat-4.0.1/common/lib/servlet.jar(javax/servlet/http/HttpServletResponse.class)]

[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/io/IOException.class)]
[checking TestServlet]
[loading /usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/lang/Error.class)]
[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/lang/Exception.class)]
[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/lang/Throwable.class)]
[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/lang/RuntimeException.class)]
[loading /usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/lang/Class.class)]
[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/lang/ClassNotFoundException.class)]

[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/sql/DriverManager.class)]
[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/util/Properties.class)]
[loading /usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/lang/String.class)]
[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/sql/SQLException.class)]
[loading
/var/jakarta-tomcat-4.0.1/common/lib/servlet.jar(javax/servlet/ServletResponse.class)]

[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/io/PrintWriter.class)]
[loading /usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/io/Writer.class)]
[loading /usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/sql/Statement.class)]

[loading /usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/sql/ResultSet.class)]

[loading
/usr/java/jdk1.3.1_02/jre/lib/rt.jar(java/lang/StringBuffer.class)]
[wrote TestServlet.class]
[total 605ms]

My Classpath is set to
CLASSPATH=".:/usr/lib/pgsql/jdbc7.0-1.2.jar:/var/jakarta-tomcat-4.0.1/common/lib/servlet.jar"

When I try to run the servlet, I get the following error (it seems like
the database connection is not being made - db is assigned a null
value):

Apache Tomcat/4.0.1 - HTTP Status 500 - Internal Server Error
type Exception report
message Internal Server Error
description The server encountered an internal error (Internal Server
Error) that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
        at TestServlet.service(TestServlet.java:40)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)

        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at filters.ExampleFilter.doFilter(ExampleFilter.java:149)
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:213)

        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

        at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)

        at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

        at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

        at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

        at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1011)

        at
org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106)

        at java.lang.Thread.run(Thread.java:484)

Does anyone have any suggestions?  It seems strage to me that if I
change the driver name that reads
Class.forName("org.postgresql.Driver"), I get no errors when I compile.
I could change it to Class.forName("heyYou") and it would still compile
with no errors.  The other thing that seems strange is that if you look
at the verbose output of my javac call, it seems like the sql
DriverManagement and ResultSet classes are coming from the Java JDK
1.3.1 install and not the postgres 7.0-1.2 JDBC java driver that was
supplied from postgres.org.  Is this just the order of the classes in my
classpath (I only have 2 not including the current directory).

Any help would greately be appreciated.  Or at least a direction in
which to go look.  Thanks in advance.

Peter


Responses

pgsql-jdbc by date

Next:From: Rene PijlmanDate: 2001-12-24 08:06:33
Subject: Re: Problem with connecting to postgres using jdbc7.0-1.2.jar
Previous:From: Nick FankhauserDate: 2001-12-23 17:17:29
Subject: Re: HELP PLEASE: Error executing the example esql.xml on cocoon1.8.2

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group