Thread: Connect problem with JDBC in javaBean and JSP

Connect problem with JDBC in javaBean and JSP

From
"gaox"
Date:
I try to write a bean to deal with all the operation of the database. Here are the codes:
 
import java.sql.*;
import java.util.*;
 
public class ConnectDB {
 String error;
 Connection con;
 
 public void connect () throws ClassNotFoundException,
                            SQLException,
                            Exception {
  try {
   Class.forName("org.postgresql.Driver");
   con = DriverManager.getConnection("jdbc:postgresql:test","postgres","");
  }
  catch (ClassNotFoundException cnfe) {
   error = "ClassNotFoundException: Could not locate DB driver.";
   throw new ClassNotFoundException (error);
  }
  catch (SQLException sqle) {
   error = "SQLException: Could not connect to database.";
   throw new Exception (error);
  }
  catch (Exception e) {
   error = "Exception: An unknown error occurred while connecting to database.";
   throw new Exception (error);
  }
 }
 
 public void disconnect () throws SQLException {
  try {
   if ( con != null ) {
    con.close();
   }
  }
  catch (SQLException sqle) {
   error = "SQLException: Unable to close the database connection.";
   throw new SQLException (error);
  }
 }
 
 public ResultSet viewDatabase () throws SQLException, Exception {
  ResultSet rs = null;
  try {
   String queryString = "Select * From test;";
   Statement stmt = con.createStatement();
   rs = stmt.executeQuery(queryString);
  }
  catch (SQLException sqle) {
   error = "SQLException : Could not execute the query.";
   throw new SQLException (error);
  }
  catch (Exception e) {
   error = "Exception: An unknown error occurred while test.";
   throw new Exception (error);
  }
  return rs;
 }
}
and the JSP is :
 
<%@ page language="java" import="java.sql.*, java.io.*, java.util.*" %>
<%@ page contentType="text/html;charset=gb2312"%>
<jsp:useBean id="test" class="ConnectDB" />
 
<html>
<head><title> test database </title></head>
 
<body>
<table width="85%" border="1">
<%
test.connect ();
 
ResultSet rs = test.viewDatabase ();
 
while (rs.next()) {
%>
  <tr>
   <td><%=rs.getInt("i")%></td>
   <td><%=rs.getString("name")%></td>
  </tr>
<%
}
 
test.disconnect ();
%>
</table>
<p>&nbsp;</p>
</body>
</html>
 
The environment is Redhat 7.2, postgreSQL 7.2, j2sdk1.4.0, Tomcat 4.0.3
and the parameter CLASSPATH contains the /usr/local/pgsql/share/pgjdbc2.jar
While I browse the page, it is said
 
javax.servlet.ServletException: ClassNotFoundException: Could not locate DB driver.
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:463)
at org.apache.jsp.list$jsp._jspService(list$jsp.java:134)
........
 
However, I can run my java application to connect the database and show the
records in it well. What's wrong? Thanks a lot for help!
 
yours Xiang Gao

Re: Connect problem with JDBC in javaBean and JSP

From
tony
Date:
On Wed, 2002-06-12 at 07:38, gaox wrote:

> The environment is Redhat 7.2, postgreSQL 7.2, j2sdk1.4.0, Tomcat 4.0.3
> and the parameter CLASSPATH contains the /usr/local/pgsql/share/pgjdbc2.jar
> While I browse the page, it is said
>
> javax.servlet.ServletException: ClassNotFoundException: Could not locate DB driver.
>     at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:463)
>     at org.apache.jsp.list$jsp._jspService(list$jsp.java:134)
> ........
>
> However, I can run my java application to connect the database and show the
> records in it well. What's wrong? Thanks a lot for help!

Tomcat needs the JDBC driver in /tomcat4/common/lib/

Cheers

Tony Grant

--
RedHat Linux on Sony Vaio C1XD/S
http://www.animaproductions.com/linux2.html
Macromedia UltraDev with PostgreSQL
http://www.animaproductions.com/ultra.html


Re: Connect problem with JDBC in javaBean and JSP

From
Nookala Satish Kumar
Date:
Hi Gaox

The problem is very simple. Copy the "pgjdbc2.jar"
file to the TOMCAT_HOME/common/lib directory. Restart
your tomcat server. That's it.

Regards,
Satish.

--- gaox <gaox@mountor.com> wrote:
> I try to write a bean to deal with all the operation
> of the database. Here are the codes:
>
> import java.sql.*;
> import java.util.*;
>
> public class ConnectDB {
>  String error;
>  Connection con;
>
>  public void connect () throws
> ClassNotFoundException,
>                             SQLException,
>                             Exception {
>   try {
>    Class.forName("org.postgresql.Driver");
>    con =
>
DriverManager.getConnection("jdbc:postgresql:test","postgres","");
>   }
>   catch (ClassNotFoundException cnfe) {
>    error = "ClassNotFoundException: Could not locate
> DB driver.";
>    throw new ClassNotFoundException (error);
>   }
>   catch (SQLException sqle) {
>    error = "SQLException: Could not connect to
> database.";
>    throw new Exception (error);
>   }
>   catch (Exception e) {
>    error = "Exception: An unknown error occurred
> while connecting to database.";
>    throw new Exception (error);
>   }
>  }
>
>  public void disconnect () throws SQLException {
>   try {
>    if ( con != null ) {
>     con.close();
>    }
>   }
>   catch (SQLException sqle) {
>    error = "SQLException: Unable to close the
> database connection.";
>    throw new SQLException (error);
>   }
>  }
>
>  public ResultSet viewDatabase () throws
> SQLException, Exception {
>   ResultSet rs = null;
>   try {
>    String queryString = "Select * From test;";
>    Statement stmt = con.createStatement();
>    rs = stmt.executeQuery(queryString);
>   }
>   catch (SQLException sqle) {
>    error = "SQLException : Could not execute the
> query.";
>    throw new SQLException (error);
>   }
>   catch (Exception e) {
>    error = "Exception: An unknown error occurred
> while test.";
>    throw new Exception (error);
>   }
>   return rs;
>  }
> }
>
> and the JSP is :
>
> <%@ page language="java" import="java.sql.*,
> java.io.*, java.util.*" %>
> <%@ page contentType="text/html;charset=gb2312"%>
> <jsp:useBean id="test" class="ConnectDB" />
>
> <html>
> <head><title> test database </title></head>
>
> <body>
> <table width="85%" border="1">
> <%
> test.connect ();
>
> ResultSet rs = test.viewDatabase ();
>
> while (rs.next()) {
> %>
>   <tr>
>    <td><%=rs.getInt("i")%></td>
>    <td><%=rs.getString("name")%></td>
>   </tr>
> <%
> }
>
> test.disconnect ();
> %>
> </table>
> <p> </p>
> </body>
> </html>
>
> The environment is Redhat 7.2, postgreSQL 7.2,
> j2sdk1.4.0, Tomcat 4.0.3
> and the parameter CLASSPATH contains the
> /usr/local/pgsql/share/pgjdbc2.jar
> While I browse the page, it is said
>
> javax.servlet.ServletException:
> ClassNotFoundException: Could not locate DB driver.
>     at
>
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:463)
>     at
>
org.apache.jsp.list$jsp._jspService(list$jsp.java:134)
> ........
>
> However, I can run my java application to connect
> the database and show the
> records in it well. What's wrong? Thanks a lot for
> help!
>
> yours Xiang Gao
>
>


__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com