pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Problem loading jdbc driver in servlet, but not in program
2+ messages / 2 participants
[nested] [flat]

* Problem loading jdbc driver in servlet, but not in program
@ 1999-05-15 16:24  Keith/Suzanne Barron <keith@barron.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Keith/Suzanne Barron @ 1999-05-15 16:24 UTC (permalink / raw)
  To: 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) { }
        }
    }
}




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [INTERFACES] Problem loading jdbc driver in servlet, but not in program
@ 1999-05-15 18:44  Benoit Foucher <benoit@ooc.com>
  parent: Keith/Suzanne Barron <keith@barron.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Benoit Foucher @ 1999-05-15 18:44 UTC (permalink / raw)
  To: Keith/Suzanne Barron <keith@barron.com>; +Cc: pgsql-interfaces

Hi, 

I think it's a configuration problem of your servlet engine. 
I use JServ from java.apache.org and I had to add the postresql.jar
archive to the classpath environment variable in the config file of 
the servlet engine. Here's a part of my configuration file:

 # CLASSPATH environment variable passed to the JVM
 wrapper.classpath=/usr/local/lib/ApacheJServ.jar
 wrapper.classpath=/usr/local/lib/postresql.jar
 wrapper.classpath=/usr/local/lib/gnujsp.jar

(you'll probably have to restart your web server to restart
the servlet engine...)
Hope this will help you...

Cheers
Benoit Foucher


Keith/Suzanne Barron wrote:
> 
> 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) { }
>         }
>     }
> }



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~1999-05-15 18:44 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-05-15 16:24 Problem loading jdbc driver in servlet, but not in program Keith/Suzanne Barron <keith@barron.com>
1999-05-15 18:44 ` Benoit Foucher <benoit@ooc.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox