Java 多个ExecuteQuery()不';行不通
我在执行Java 多个ExecuteQuery()不';行不通,java,jdbc,Java,Jdbc,我在执行时遇到一些问题。我的代码: try{ String where =""; String set, sqlque; int help ; System.out.println("Laczenie z baza danych "); Connection conn = DriverManager.getConnection(DB,USER,PASS); conn.setAutoCommit(
时遇到一些问题。我的代码:
try{
String where ="";
String set, sqlque;
int help ;
System.out.println("Laczenie z baza danych ");
Connection conn = DriverManager.getConnection(DB,USER,PASS);
conn.setAutoCommit(false);
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
Statement new = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
temporary = describe.getSelectedItem().toString()+"";
if(!Ab.getText().equals("")){where +=" Ab="+"'"+Ab.getText()+"'AND";};
sqlpytanie="Select ab, product_id from product where describe=?";
PreparedStatement stm1 = conn.prepareStatement(sqlpytanie);
stm1.setString(1, temporary);
ResultSet rs = stm1.executeQuery();
while(rs.next()){
System.out.println("In 1 while");
tmp1 = rs.getString("Ab");
tmp = rs.getString("product_id");
}
help= Integer.parseInt(tmp1) - Integer.parseInt(Ab.getText());
set = "Update Product set ab='"+help+"'"+"where product_id="+tmp;
System.out.println(help);
System.out.println(set);
System.out.println(tmp);
new.executeUpdate(set);
sqlque="Select prices from price where product_id=?";
PreparedStatement stm2 = conn.prepareStatement(sqlque);
stm2.setString(1, tmp);
System.out.println(sqlque);
ResultSet res = stm2.executeQuery();
System.out.println(res);
while(res.next()){
System.out.println("In 2 while");
prices= res.getString("prices ");
}
问题在于第二个执行任务
,因为没有结果。Java没有正确执行这一点,为什么我从System.out.println(res.next())获取false
代码>?在我的数据库中,一切正常,它应该显示信息。我做错了什么 我看不到现在在哪里定义了
。请清理代码,以便只显示相关部分(例如:删除所有打印输出),此外:如果使用PreparedStatement
,则应按其应该的方式使用它们:不要在查询中串联值,使用参数占位符(?
)我清理代码。现在我对第二个PreparedStatement有问题,因为它没有在sqlque中替换?使用字符串tmp。
}
JOptionPane.showMessageDialog(new JFrame()," ab "+ tmp1 + " prices " + prices);
// updateTotal = conn.prepareStatement(updateStatement);
conn.commit();
stmt.close();
new.close();
conn.close();
}