我在按钮事件处理中连续使用executeQuery来去除数据库的值赋于变量。
发现只能执行一句。而后一句则不执行,为什么?
代码如下
public void jButton1_actionPerformed(ActionEvent e) {
try {
rs=toUpdate.executeQuery("select zhjhdbl from jchshjb where ID="+1);
rs.next();
n10=rs.getFloat("zhjhdbl");
rs.close(); rs=toUpdate.executeQuery("select zhxfzhj from gdzchzhjwxhdyzchtxb where ID="+(t-1)+"");
rs.next();
n11=rs.getFloat("zhxfzhj");
}
catch{
..........
这是为什么?请各位前辈帮忙!!!
谢谢!!!
发现只能执行一句。而后一句则不执行,为什么?
代码如下
public void jButton1_actionPerformed(ActionEvent e) {
try {
rs=toUpdate.executeQuery("select zhjhdbl from jchshjb where ID="+1);
rs.next();
n10=rs.getFloat("zhjhdbl");
rs.close(); rs=toUpdate.executeQuery("select zhxfzhj from gdzchzhjwxhdyzchtxb where ID="+(t-1)+"");
rs.next();
n11=rs.getFloat("zhxfzhj");
}
catch{
..........
这是为什么?请各位前辈帮忙!!!
谢谢!!!
Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All statement execute methods implicitly close a statment's current ResultSet object if an open one exists.
一个Statement object 一次只能打开一个ResultSet object
是不是关闭了。。后面的没作用了。
Connection con=DriverManager.getConnection("jdbc:odbc:jjtz","sa","sa");
toUpdate=con.createStatement();
rs = toUpdate.executeQuery("select qshnf from xmmchnfb where ID="+1);
rs.next();
n1=rs.getInt("qshnf");
//con.setAutoCommit(false);
//toUpdate.addBatch("update jshtzgsb set yearn=(select year1 from jshtzb where ID='1') where ID="+1);
//toUpdate.executeBatch();
//con.commit();
//con.setAutoCommit(true);
rs = toUpdate.executeQuery("select zhzhnf from xmmchnfb where ID="+1);
rs.next();
n2=rs.getInt("zhzhnf");
n3=n2-n1+1;
rs = toUpdate.executeQuery("select year1 from jshtzb where ID=(select max(ID) from jshtzb)");
rs.next();
n4=rs.getInt("year1")+1;
n5=n4-n1;
rs = toUpdate.executeQuery("select zhjnx from jchshjb where ID="+1);
rs.next();
n6=rs.getInt("zhjnx");这个就可以
rs=toUpdate.executeQuery("select ynshsde from lrylrfpb where ID="+t);
rs.next();
n8=rs.getFloat("ynshsde");
if(n8>0){
toUpdate.executeUpdate("update lrylrfpb set sdsh=(select ynshsde from lrylrfpb where ID="+t+")*(select sdshl from jchshjb where ID="+1+") where ID="+t);
}
else{
toUpdate.executeUpdate("update lrylrfpb set sdsh="+0+" where ID="+t);
} toUpdate.executeUpdate("update lrylrfpb set jlr=(select ynshsde from lrylrfpb where ID="+t+")-(select sdsh from lrylrfpb where ID="+t+") where ID="+t); rs=tUpdate.executeQuery("select dnhdqk from lrylrfpb where ID="+t);
rs.next();
n9=rs.getFloat("dnhdqk");
rs.close();可是只要两个紧挨在一起就不行。
rs=toUpdate.executeQuery("select zhxfzhj from gdzchzhjwxhdyzchtxb where ID="+(t-1)+"");
rs.next();
n11=rs.getFloat("zhxfzhj");
下面
也不行
一个用rs
另一个用rs1
还是只能运行一个
这只是其中两句紧挨的
因为debug了所以知道在第二个那里出错了
那就是你的SQL语句,没查到数据select zhxfzhj from gdzchzhjwxhdyzchtxb where ID="+(t-1)+""你看看这条语句,能执行查出数据吗??
1. 你可以多创建个Statement打开结果集.
2. 换个新版本的驱动, 也可到sun的网站上找个第三方的jdbc驱动
试过了。