通过java代码获取MySQL delete命令中的错误

时间:2011-10-27 14:42:26

标签: java mysql sql java-ee jdbc

我已经使用jdbc从表中删除记录,同时执行相同的操作,我得到了一个错误,如下所示:

java.sql.SQLException: Before start of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2674)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2815)
    at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:236)
    at net.ordernet.vd.lookupTable.UpdateLookupTable.deleteLookupTableRecords(UpdateLookupTable.java:118)
    at net.ordernet.vd.soap.UpdateLookupTableTool.deleteLookupTableRecords(UpdateLookupTableTool.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)

我的代码如下: 1)采用两个字符串来指定查询 2)连接对象用于准备语句 3)在'int'变量中检查更新查询的结果 4)我没有使用结果集,虽然获得了结果集异常......!

try 
{
  String queryDeleteLookupTableRecords = "delete from LookupTableRecords where tableId = ?";
  String queryDeleteLookupTableRows = "delete from LookupTableRows where tableId = ?";

  PreparedStatement psDeleteLookupTableRecords = oConnection.prepareStatement(queryDeleteLookupTableRecords);
  PreparedStatement psDeleteLookupTableRows = oConnection.prepareStatement(queryDeleteLookupTableRows);
  psDeleteLookupTableRecords.setInt(1, oLookupTable.getID());
  psDeleteLookupTableRows.setInt(1, oLookupTable.getID());
  int result = psDeleteLookupTableRecords.executeUpdate();
  int result2 = psDeleteLookupTableRows.executeUpdate();
  if(result > 0 && result2 > 0)
  {
     iReturnValue = 1;
  }
  psDeleteLookupTableRecords.close();
  psDeleteLookupTableRows.close();
}
catch (SQLException oSqlException) 
{
  Log.print(oSqlException);
} 

1 个答案:

答案 0 :(得分:6)

您的问题不在您展示的代码中。在致电resultSet.getInt(..)resultSet.first()

之前,您某处.next()