通过java代码在MySQL删除命令中出现错误

发布于 2024-12-12 08:43:29 字数 2183 浏览 0 评论 0原文

我使用 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);
} 

I have used jdbc to delete records from the table, while eexcuting the same, I got an error as I have shown below :

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)

My code is as follows :
1) two strings are taken to specify the query
2) connection object is used to prepare statement
3) checked the result of update query in 'int' variable
4) I havent used the result-Set, getting the resultset exception though...!

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);
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2024-12-19 08:43:29

您的问题不在于您显示的代码中。在调用 resultSet.first().next() 之前,您在某处有一个 resultSet.getInt(..)

Your problem is not in the code that you showed. You are having a resultSet.getInt(..) somewhere before calling resultSet.first() or .next()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文