my configuration:win2000 tomcat4.1.24 jdk1.4.0
i have downloaded the mysql driver , put it to
the correct directory and set the CLASSPATH.
now the following
java code can always run correctly .
-----------------dataLink.java-----------------------
public class dataLink
{
public static void main(
String[] args){
Connection conn=null;
Statement stat=null;
ResultSet rst=null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.out.println("JDBC driver loaded.");
conn=DriverManager.getConnection("jdbc:mysql://localhost/address ?user=root&password=good");
stat=conn.createStatement();
rst=stat.executeQuery("select * from addressbook");
System.out.println("<table border=1>");
int i=1;
while (rst.next()){
System.out.println("<tr><td>");
System.out.println(rst.getString(1));
System.out.println("</td>");
System.out.println("<td>");
System.out.print(rst.getString(2));
System.out.println("</td>");
System.out.println("<td>");
System.out.print(rst.getString(3));
System.out.println("</td></tr>");
}
System.out.println("</table>");
System.out.println("Database connection established.");
} catch (ClassNotFoundException cnfe){
System.out.println("ClassNotFoundException:could not locate driver.");
} catch (SQLException cnfe){
System.out.println("SQLException:"+cnfe);
} catch (Exception e){
System.out.println("An unknown error occured while connecting to the database.");
}finally {
try{
if (rst!=null)
rst.close();
if (stat!=null)
stat.close();
if (conn!=null)
conn.close();
} catch (SQLException sqle) {
System.out.println("Unable to close database connection.");
}
}
}
};
------------------------------------------------------------------
but when i try to link to mysql using
jsp and javabean,it failed;
the jsp and javabean code is following!!!
-------------------listgroupid.jsp-------------------------------
<%@ page language="java" import="java.sql.*,java.io.*,database.*,java.util.*" %>
<jsp:useBean id="usergroupList" class="database.bbs" />
<table>
<%
usergroupList.connect();
ResultSet rs=usergroupList.listUsergroup();
while (rs.next()){
%>
<tr>
<td><%=rs.getString("1")%></td>
<td><%=rs.getString("2")%></td>
</tr>
<%
}
%>
</table>
<%
usergroupList.disconnect();
%>
-------------------------------------------------------------------------
----------------------------bbs.java-------------------------------------
package database;
import java.sql.*;
import java.util.*;
public class bbs
{
String error;
Connection conn;
public bbs()
{
}
//连接数据库
public void connect() throws ClassNotFoundException,SQLException,Exception
{
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost/address ?user=root&password=good ");
}
catch (ClassNotFoundException cnfe)
{
error="ClassNotFoundException:Could not locate DB driver.";
throw new ClassNotFoundException(error);
}
catch (SQLException cnfe)
{
error="SQLException:Could not connect to database.";
throw new SQLException(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 (conn!=null) {
conn.close();
}
}
catch (SQLException sqle)
{
error="SQLException:Unable to close the database connection";
throw new SQLException(error);
}
}
//打开记录集,并返回
public ResultSet listUsergroup() throws SQLException,Exception
{
ResultSet rst=null;
try
{
String queryString="select * from addressbook";
Statement stat=conn.createStatement();
rst=stat.executeQuery(queryString);
}
catch (SQLException sqle)
{
error="SQLException:Could not execute the query.";
throw new SQLException(error);
}
catch (Exception e)
{
error="An exception occured while retrieving usergroup.";
throw new SQLException(error);
}
return rst;
}
};
-------------------------------------------------------------------------
IE show me the following Exception note:
org.apache.jasper.JasperException: ClassNotFoundException:Could not locate DB driver.
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
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 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:536)
who can tell me why!!!