回 帖 发 新 帖 刷新版面

主题:问个数据库问题

我用BEAN直连数据库.下面的代码可以正常运行,但执行第二条executeQuery那句时没反应
,即没有改变NUM的值,但如果把上面的executeQuery去掉的话(rs.close();也去掉),就可以
执行正常.如果不用BEAN操作,是可以正常执行的.WHY?

<jsp:useBean id="conn" scope="page" class="DB.DBConn" />
ResultSet rs=conn.executeQuery(request.getRealPath(""),"select num from counte
r");
rs.close();
conn.executeQuery(request.getRealPath(""),"update counter set num=5");





BEAN也贴出来吧,这个BEAN可以正常使用的.
package DB;
import java.sql.*;
public class DBConn{
    String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
    String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=";
        Connection  con=null;
    ResultSet rs=null;
    public DBConn(){
        try
        {
            Class.forName(sDBDriver);
        }
        catch(java.lang.ClassNotFoundException e)
        {
            System.err.println(e.getMessage());
        }
        
    }
    public ResultSet executeQuery(String path,String sql)
    {
        try
        {
            url+=path+"\\data.mdb";
            con=DriverManager.getConnection(url) ;  
            Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,Result
Set.CONCUR_READ_ONLY);
            rs=stmt.executeQuery(sql);
        }
        catch(SQLException ex)
        {
            System.err.println(ex.getMessage());
        }
        return rs;
    }
    public int executeUpdate(String path,String sql)
    {
        int result=0;
        try
        {
            url+=path+"\\data.mdb";
            con=DriverManager.getConnection(url);  
            Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,Result
Set.CONCUR_READ_ONLY);
            result=stmt.executeUpdate(sql);
        }
        catch(SQLException ex)
        {
            System.err.println(ex.getMessage());
        }
        return result;
    }
}

回复列表 (共3个回复)

沙发

你既然把结果集给关闭了,还怎么查询数据来改啊

板凳

conn.executeQuery(request.getRealPath(""),"update counter set num=5");
这句错了,如果要更新的话,要用executeUpdate

3 楼

Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)
改成
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE)

我来回复

您尚未登录,请登录后再回复。点此登录或注册