主题:问个数据库问题
我用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;
}
}
,即没有改变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;
}
}