日期:2014-05-17  浏览次数:20763 次

jsp连接数据库时发生错误求解
JAVABEAN
package bean;

import java.sql.*;

public class QueryBean {
public String query_statement;/* 定义sql语句 */
public String param[];/* 查询条件 */
public ResultSet result = null;/* 查询结果 */
public Connection conn;

// 设置查询参数
public void setParam(String[] param) {
this.param = param;
}

// 设置SQL查询语句
public void setQuery_statement(String query_statement) {
this.query_statement = query_statement;
}

// 设置连接参数
public void setConnection(String driverName, String jdbcURL,
String username, String passwd) throws Exception {
Connection conn1;
Class.forName(driverName);
conn1 = DriverManager.getConnection(jdbcURL, username, passwd);
conn1.setAutoCommit(false);
this.conn = conn1;
}

// 获取查询结果
public ResultSet getResult() {
try {
PreparedStatement select_stm = conn.prepareStatement(
query_statement,
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
if (param != null)
for (int i = 0; i < param.length; i++)
select_stm.setString(i + 1, param[i]);
result = select_stm.executeQuery();
} catch (Exception e) {
System.out.println(e);
}
return result;
}

public void insertRecord()throws SQLException,java.io.UnsupportedEncodingException{
try{
PreparedStatement insert_stm=conn.prepareStatement(query_statement);
if(param!=null){
for(int i=0;i<param.length;i++)
insert_stm.setString(i+1, param[i]);
insert_stm.executeUpdate();
insert_stm.close();
conn.commit();
}
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}

/* 对数据记录进行更新操作 */
public void updateRecord() throws SQLException,java.io.UnsupportedEncodingException{
try{
PreparedStatement update_stm=conn.prepareStatement(query_statement);
if(param!=null)
for(int i=0;i<param.length;i++)
update_stm.executeUpdate();
update_stm.close();
conn.commit();
}
catch(Exception e){
System.out.println(e);
conn.rollback();
}
}
/* 删除数据记录 */
public void deleteRecord() throws SQLException,java.io.UnsupportedEncodingException{
try{
PreparedStatement delete_stm=conn.prepareStatement(query_statement);
if(param!=null)
for(int i=0;i<param.length;i++)
delete_stm.setString(i+1,param[i]);
delete_stm.executeUpdate();
delete_stm.close();
conn.commit();
}
catch(Exception e){
System.out.println(e);
conn.rollback();
}
}
}
查询界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>


<jsp:useBean id="query" class="bean.QueryBean" scope="page" />

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用javabean查询数据库</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" cont