连接MYSQL的一个javaBean出错
package note;
import java.sql.*;
public class Jdbc_conn{
String sDBDriver="com.mysql.jdbc.Driver";
String sConnStr="jdbc:mysql://localhost:3306/sample_db?user=root&password=root";
Connection conn=null;
ResultSet rs=null;
public Jdbc_conn(){
try
{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e)
{
System.out.println("Jdbc_conn():"+e.getMessage());
}
}
public void executeUpdate(String sql)throws Exception{
sql=new String(sql.getBytes("GBK"),"ISO8859_1"); //设置编码格式
try{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex){
System.out.println("sql.executeUpdate:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql)throws Exception{
rs=null;
try{
sql=new String(sql.getBytes("GBK"),"ISO8859_1"); //编码设置
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex){
System.out.println("sql.executeQuery:"+ex.getMessage());
}
return rs;
}
*******************************************
我把二个方法中的stmt.close();conn.close();去掉就可以了,但是我觉得不应该去掉呀,就这样到底错在那里了?请高人指点!