首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

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

2011-12-31 
小弟初学,问下,在JSP中,不同的statement状态集是不是不可以对同一个表执行相同 的操作啊 !!!!!!!!!!!!!!!R

小弟初学,问下,在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 ";


conn = DataSourceHelper.getConnection();

stmt = conn.createStatement();
stmt1 = conn.createStatement();
rs1 = stmt.executeQuery(sql1);
rs2 = stmt1.executeQuery(sql2);
.......
.......
if (rs1!= null)
rs1.close();
if (rs2!= null)
rs2.close();
if (stmt != null)
stmt.close();
if (stmt1 != null)
stmt1.close();
if (conn != null)
conn.close();

热点排行