javabean访问数据库出错
我的应用环境为:tomcat4.12+mysql+mm.mysql.jdbc+poolman2
在jsp中调用javaBean总是出错,请帮忙看看错在哪里?
另外:如果将javabean中open()方法的catch(Exception e)改为catch(SQLException e),编译时就出错,为什么?
错误信息
=================================
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 7 in the jsp file: /testpoolread.jsp
Generated servlet error:
[javac] Compiling 1 source file
E:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\testpoolread_jsp.java:44: cannot resolve symbol
symbol : class Mysql
location: class org.apache.jsp.testpoolread_jsp
Mysql tia = null;
^
An error occurred at line: 7 in the jsp file: /testpoolread.jsp
Generated servlet error:
E:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\testpoolread_jsp.java:46: cannot resolve symbol
symbol : class Mysql
location: class org.apache.jsp.testpoolread_jsp
tia = (Mysql) pageContext.getAttribute("tia", PageContext.PAGE_SCOPE);
^
An error occurred at line: 7 in the jsp file: /testpoolread.jsp
Generated servlet error:
E:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\testpoolread_jsp.java:49: cannot resolve symbol
symbol : class Mysql
location: class org.apache.jsp.testpoolread_jsp
tia = (Mysql) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "Mysql");
^
An error occurred at line: 9 in the jsp file: /testpoolread.jsp
Generated servlet error:
E:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\testpoolread_jsp.java:60: cannot resolve symbol
symbol : class ResultSet
location: class org.apache.jsp.testpoolread_jsp
ResultSet rs = null;
^
4 errors
javaBEAN
=============================================
import java.sql.*;
import java.io.*;
import javax.sql.*;
import javax.naming.*;
/***
* 处理数据库的连接和访问
* @version 1.01
***/
public class mysql
{
Connection conn;
Statement stmt;
PreparedStatement prepstmt;
/***
*构造方法
***/
public mysql()
{
conn = null;
stmt = null;
prepstmt = null;
}
/***
*功能: 连接数据库,创建Statement对象
*返回类型:int
*返回值:0:成功;-1:连接失败,无法建立Connection对象;-2:其他错误
***/
public int Open()
{
int f=0;
try
{
Class.forName("com.codestudio.sql.PoolMan").newInstance();
conn = DriverManager.getConnection("jdbc:poolman://tia");
stmt = conn.createStatement();
}
catch(Exception e)
{
if (conn==null || stmt==null) f=-1;
else f=-2;
this.close();
}
return f;
}
/***
*功能: 连接数据库,创建PrepareStatement对象
*参数:String sql:预置的SQL语句
*返回类型:int
*返回值:0:成功;-1:连接失败,无法建立Connection对象;-2:其他错误
***/
public int PreOpen(String sql)
{
int f=0;
try
{
Class.forName("com.codestudio.sql.PoolMan").newInstance();
conn = DriverManager.getConnection("jdbc:poolman://tia");
this.setPreStmt(sql);
}
catch(Exception e)
{
if (conn==null || prepstmt==null) f=-1;
else f=-2;
this.close();
}
return f;
}
/***
*功能:设置PreparedStatement对象的SQL语句
*参数:String sql:预置的SQL语句
***/
public void setPreStmt(String sql) throws SQLException
{
prepstmt = conn.prepareStatement(sql);
}
/***
*功能:清除PreparedStatement对象的SQL语句,
* 与设置PreparedStatement对象SQL语句的方法(setPreStmt)配合,
* 实现PreparedStatement对象的重复使用
***/
public void clearParameters() throws SQLException
{
prepstmt.clearParameters();
}
/***
* 功能:返回连接
* 返回值:Connection 连接
***/
public Connection getConnection()
{
return conn;
}
/***
* 返回PreparedStatement对象
***/
public PreparedStatement getPreparedStatement()
{
return prepstmt;
}
/***
* 返回Statement对象
***/
public Statement getStatement()
{
return stmt;
}
/***
* 设置PreparedStatement对象的参数值
* @param index 参数序号,从1开始
* @param value 对应值
***/
public void setString(int index,String value) throws SQLException
{
prepstmt.setString(index,value);
}
public void setInt(int index,int value) throws SQLException
{
prepstmt.setInt(index,value);
}
public void setDate(int index,Date value) throws SQLException
{
prepstmt.setDate(index,value);
}
public void setLong(int index,long value) throws SQLException
{
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException
{
prepstmt.setFloat(index,value);
}
//File file = new File("test/data.txt");
//int fileLength = file.length();
//InputStream fin = new java.io.FileInputStream(file);
//mysql.setBinaryStream(5,fin,fileLength);
public void setBinaryStream(int index,InputStream in,int length) throws SQLException
{
prepstmt.setBinaryStream(index,in,length);
}
/***
* 通过Statement对象,执行SQL查询语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
***/
public ResultSet Query(String sql) throws SQLException
{
if (stmt != null)
{
return stmt.executeQuery(sql);
}
else return null;
}
/***
* 通过PreparedStatement对象,执行SQL查询语句返回字段集
* @return ResultSet 字段集
***/
public ResultSet PreQuery() throws SQLException
{
if (prepstmt != null)
{
return prepstmt.executeQuery();
}
else return null;
}
/***
* 通过Statement对象,执行SQL更新语句
* @param sql SQL语句
***/
public void Update(String sql) throws SQLException
{
if (stmt != null) stmt.executeUpdate(sql);
}
/***
* 通过PreparedStatement对象,执行SQL更新语句
***/
public void PreUpdate() throws SQLException
{
if (prepstmt != null) prepstmt.executeUpdate();
}
/***
* 关闭连接
***/
public void close()
{
if (stmt != null)
{
try
{
stmt.close();
}
catch (Exception e1){}
stmt = null;
}
if (prepstmt != null)
{
try
{
prepstmt.close();
}
catch (Exception e1) {}
prepstmt = null;
}
try
{
conn.close();
}
catch (Exception e1) {}
conn = null;
}
}
jsp文件:
===========================================
//引用javaBean:tia.class
<jsp:useBean id="tia" class="Mysql" scope="page"/>
<%
ResultSet rs = null;
long start = System.currentTimeMillis();
tia.Open();
String sql="Select * from ccbhdzb";
//执行查询
rs = tia.Query(sql);
while (rs.next())
{
//输出每一条记录
for(int j=1; j<=rs.getMetaData().getColumnCount(); j++)
{
//输出一条记录每一列的值
out.print( rs.getObject(j)+"\t");
}
out.println("<BR>");
}
//取得程序执行时间
out.println("执行时间 (毫秒): " + (System.currentTimeMillis() - start));
%>
问题点数:0、回复次数:2Top
1 楼zxhong(红透半边天)回复于 2003-01-04 21:39:43 得分 0
class位置:
jsp文件所在目录下/WEB-INF/classes/package(if you have package in your java class)/*.class
用SQLException 要import java.sql.*Top
2 楼wangbd8(wangbd8)回复于 2003-01-05 12:28:18 得分 0
解决了,使病毒闹的Top




