用bean连接mysql的问题

03-05-08 liumw

以下方法用bean连接mysql在单位的机器上可以,在家里就不行了,两台机的tomcat配置一样的。

我的代码是这样的:

mydb.java_______________________________

package netzero;
import java.sql.*;
public class mydb
{
String driverName = "org.gjt.mm.mysql.Driver";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String connURL= "jdbc:mysql://localhost/mysql?user=liumw&password=770326&useUnicode=true&characterEncode=8859_1";

public mydb()
{
try
{
Class.forName(driverName);
}
catch (java.lang.ClassNotFoundException e)
{
System.err.println("netzero(String): " + e.getMessage());
}
}



public ResultSet executeQuery(String sql) throws SQLException
{
conn = DriverManager.getConnection(connURL);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
return rs;
}



public boolean closeConn()
{
try
{
if (rs!=null) rs.close();
if (stmt!=null) stmt.close();
if (conn!=null) conn.close();
return true;
}
catch ( SQLException ex )
{
System.err.println("closeConn: " + ex.getMessage());
return false;
}
}
}


beanx.jsp______________________________________

<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>

<jsp:useBean id="mydb" scope="session" class="netzero.mydb" />


<html>
<head>
<title>db testing</title>
</head>
<body>
<center>db testing</center>
<hr>
<%
int intRowCount;
out.print("显示内容");
try {
String sql="select * from user";
ResultSet rs = mydb.executeQuery(sql);

while(rs.next())
{
out.print("您的第一个字段内容为:");
out.print(rs.getString("Host")+"<br>");
out.print("您的第二个字段内容为:");
out.print(rs.getString("User")+"<br>");
out.print("您的第三个字段内容为:");
out.print(rs.getString("password")+"<br><br>");
}

}catch (SQLException E) {
out.println("SQLException: " + E.getMessage());
out.println("SQLState: " + E.getSQLState());
out.println("VendorError: " + E.getErrorCode());
}
%>
<% //关闭mysql连接
try {
if(!mydb.closeConn());
} catch (Exception ex) {
System.err.println("closeConn: " + ex.getMessage());
}
%>
</body>
</html>


提示错误:
description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: org.gjt.mm.mysql.Connection.createStatement(II)Ljava/sql/Statement;
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:248)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
...............


哪里出了问题呢?