当前位置: 代码迷 >> Java Web开发 >> 小弟初学,问下,在JSP中,不同的statement状态集是不是不可以对同一个表执行相同 的操作啊 !该如何解决
  详细解决方案

小弟初学,问下,在JSP中,不同的statement状态集是不是不可以对同一个表执行相同 的操作啊 !该如何解决

热度:58   发布时间:2016-04-17 15:07:25.0
小弟初学,问下,在JSP中,不同的statement状态集是不是不可以对同一个表执行相同 的操作啊 !!!!!!!!!!!!!!!
RT:
        ResultSet   rs2   =   stmt2.executeQuery( "select   *   from   xuejidangan   where   行政班级= ' "+xingzhengbanji0+ " ' ");  
  out.println( "ooooooooooooooo ");
 
  ResultSet   rs5   =   stmt4.executeQuery( "select   *   from   xuejidangan   where   行政班级= ' "+xingzhengbanji11+ " ' ");  
  out.println( "ooooooooooooooo ");
我用out.println打印了下,rs2结果正确,而rs5却为空,奇怪??

------解决方案--------------------
你可以把两个select换成insert,
看看,两次执行是事都把数据写入了,
就知道可不可以用两次了.
------解决方案--------------------
好像没有什么错误,可以这样操作的。
------解决方案--------------------
try{
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;

Class.forName( "com.mysql.jdbc.Driver ");
String dburl,dbname,dbpwd;
con = DriverManager.getConnection(dburl,dbname,dbpwd);
stmt = con.prepareStatement( "SELECT id FROM table1 ");
rs = stmt.executeQuery();
while(rs.next()){
out.print(rs.getInt( "id "));
}
stmt.close();

stmt = con.prepareStatement( "SELECT name FROM table1 ");
rs = stmt.executeQuery();
while(rs.next()){
out.print(rs.getString( "name "));
}

stmt = con.prepareStatement( "SELECT name,type FROM table1 ");
rs = stmt.executeQuery();
while(rs.next()){
out.print(rs.getString( "name ");
out.print(rs.getString( "type ");
} //如此类推

rs.close();
stmt.close();
con.close();
}
catch(Exception e){}
------解决方案--------------------
stmt2,stmt4是用同一个Conn对象吧,不可以的,要先释放掉,才可以再用,不可以一个conn对象对应多个Stmt对象,这样做,第二个会出错吧

或者改成这样
ResultSet rs2 = stmt2.executeQuery( "select * from xuejidangan where 行政班级= ' "+xingzhengbanji0+ " ' ");
out.println( "ooooooooooooooo ");

ResultSet rs5 = stmt2.executeQuery( "select * from xuejidangan where 行政班级= ' "+xingzhengbanji11+ " ' ");
out.println( "ooooooooooooooo ");
------解决方案--------------------
ResultSet rs2 = stmt2.executeQuery( "select * from xuejidangan where 行政班级= ' "+xingzhengbanji0+ " ' ");
out.println( "ooooooooooooooo ");

stmt2.close();

ResultSet rs5 = stmt4.executeQuery( "select * from xuejidangan where 行政班级= ' "+xingzhengbanji11+ " ' ");
out.println( "ooooooooooooooo ");

------解决方案--------------------
我的环境如下 jdk1.5 oracle 9i 没有问题
------解决方案--------------------
同时使用多个stmt或rs来操作时,最好是用几次声明几次,否则会出先一些莫名其妙的错误.
建议使用格式(查询两次):

Connection conn = null;
Statement stmt = null;
Statement stmt1 = null;
// PreparedStatement pstmt = null;
// PreparedStatement pstmt1 = null;
ResultSet rs= null;
ResultSet rs= null;

String sql1 = "SELECT * FROM TBL ";
String sql2 = "SELECT * FROM TBL ";
  相关解决方案