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

Problem loading jdbc driver in servlet, but not in program

From: Keith/Suzanne Barron <keith(at)barron(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Problem loading jdbc driver in servlet, but not in program
Date: 1999-05-15 16:24:56
Message-ID: 373D9FD8.198A35D5@barron.com (view raw or flat)
Thread:
Lists: pgsql-interfaces
I have two simple programs, one is a regular java program, the other
is a java servlet.
When I try to run the servlet, I get "Could not load database driver".
Any ideas on why this
happens?

-Keith

Here are the programs:

1. regular java program:

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

public class DBPhoneLookup {

    public static void main (String args[]) throws
ClassNotFoundException, FileNotFoundException,  IOException,
SQLException {

        try {
            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;
            String url = "jdbc:postgresql:school";
            String usr = "postgres";
            String pwd = "postgres";

            // Load (and therefore register) the Postgres driver
            Class.forName("postgresql.Driver");

            // Get a connection to the database
            con = DriverManager.getConnection(url, usr, pwd);

            // Create a Statement object
            stmt = con.createStatement();

            // Execute an SQL query, get a ResultSet
            rs = stmt.executeQuery("Select first_name from students");

            // Display the result set as a list
            while (rs.next()) {
                System.out.println(rs.getString("first_name"));
            }

            con.close();
        }

        catch(ClassNotFoundException e) {
            System.out.println("Could not load database driver: " +
e.getMessage());
        }
    }
}


2. servlet program:

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

public class DBPhoneLookup
    extends HttpServlet { public void doGet (HttpServletRequest req,
HttpServletResponse res) throws
    ServletException, IOException {

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        try {
            String url = "jdbc:postgresql:school";
            String usr = "postgres";
            String pwd = "postgres";

            // Load (and therefore register) the Postgres driver
            Class.forName("postgresql.Driver");

            // Get a connection to the database
            con = DriverManager.getConnection(url, usr, pwd);

            // Create a Statement object
            stmt = con.createStatement();

            // Execute an SQL query, get a ResultSet
            rs = stmt.executeQuery("Select first_name from students");

            // Display the result set as a list
            out.println("");
            out.println("");
            out.println("");
            while (rs.next()) {
                out.println("    " + rs.getString("first_name"));
            }
            out.println(" ");
        }

        catch(ClassNotFoundException e) {
            out.println("Could not load database driver: " +
e.getMessage()); }

        catch(SQLException e) {
            out.println("SQLException caught: " + e.getMessage()); }

        finally {
            // Always close the database connection
            try { if (con != null) con.close(); }
            catch (SQLException ignored) { }
        }
    }
}


Responses

pgsql-interfaces by date

Next:From: Benoit FoucherDate: 1999-05-15 18:44:50
Subject: Re: [INTERFACES] Problem loading jdbc driver in servlet, but not in program
Previous:From: Michael J DavisDate: 1999-05-15 16:23:58
Subject: RE: [INTERFACES] MSAccess and primary keys

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