如果我把代码改写为如下,就OK了,二者的区别在于PreparedStatement与Statement,为什么??开发环境:wsad5.0+DB2/400
public int getKey(String keyName) throws SQLException, Exception {
Connection conn = null;
Statement pstmt = null;
LabResultSet labRS;
int result = -1;
try {
labRS =
queryDB(
"select kyvalu from lakeyp where kycode='"
+ keyName.trim()
+ "'");
if (labRS.recordcount > 0) {
result = labRS.get(0).getInt("kyvalu");
String sql =
"update lakeyp set kyvalu=kyvalu+1 where kycode='"
+ keyName.trim()
+ "'";
conn = ds.getConnection();
pstmt = conn.createStatement();
pstmt.execute(sql);
}
return result;
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} finally {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
}
}

解决方案 »

  1.   


    我在csdn里以“Function sequence error”关键字搜索了一下,发现前人遇到这样的问题,原因在于前一个RecordSet没有关闭,好像我的代码没有这样的问题啊!
    高手来解答一下!多谢!
      

  2.   

    pstmt = conn.prepareStatement(sql);
    pstmt.setString(1,keyName.trim());
    pstmt.execute(sql);
    改为
    pstmt = conn.prepareStatement(sql);
    pstmt.setString(1,keyName.trim());
    pstmt.executeUpdate(); 
    试试
      

  3.   

    解决了,pstmt.execute(sql); 改成 pstmt.execute();或pstmt.executeUpdate();
    因为PreparedStatement是extends自Statement,PreparedStatement本身是没有execute(String sql),
    和executeUpdate(String ..)方法的,pstmt.execute(sql);调的是其基类Statement的execute()方法,当然不行啰
    哈哈