每个方法里都有关闭数据库连接,导致rs=db.executeQuery(sql)之后rs.next()出错。
package com.db;
import java.io.*;
import java.sql.*;
public class sqlBean {
public Connection conn = null;
public ResultSet rs = null;
private String DatebaseDriver = "com.mysql.jdbc.Driver";
private String DatebaseConnStr = null;
public void setDatebaseDriver(String Driver){
this.DatebaseDriver = Driver;
}
public String getDatebaseDriver(){
return (this.DatebaseDriver);
}
public void setDatebaseConnStr(String ConnStr){
this.DatebaseConnStr = ConnStr;
}
public String getDatebaseConnStr(){
return (this.DatebaseConnStr);
}
public sqlBean(){
try{
Class.forName(DatebaseDriver);
}
catch(ClassNotFoundException e){
System.err.println("加载驱动器失败" + e.getMessage());
System.out.print("执行插入有错误" + e.getMessage());
}
}
public int executeInsert(String sql){
int num = 0;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/charge?useUnicode=true&characterEncoding=GBK", "root", "mysql51");
Statement stmt = conn.createStatement();
num = stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.err.println("执行插入有错误" + ex.getMessage());
System.out.print("执行插入有错误" + ex.getMessage());
}
CloseDateBase();
return num;
}
public ResultSet executeQuery(String sql){
rs = null;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/charge?useUnicode=true&characterEncoding=GBK", "root", "mysql51");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("执行查询有错误" + ex.getMessage());
System.out.print("执行查询有错误" + ex.getMessage());
}
//CloseDateBase();
return rs;
}
public int executeDelete(String sql){
int num = 0;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/charge?useUnicode=true&characterEncoding=GBK", "root", "mysql51");
Statement stmt = conn.createStatement();
num = stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.err.println("执行删除有错误" + ex.getMessage());
System.out.print("执行删除有错误" + ex.getMessage());
}
CloseDateBase();
return num;
}
public void CloseDateBase(){
try{
conn.close();
}
catch(Exception end){
System.err.println("关闭conn时出错" + end.getMessage());
System.out.print("关闭conn时出错" + end.getMessage());
}
}
}