通过java代码在MySQL删除命令中出现错误
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题不在于您显示的代码中。在调用
resultSet.first()
或.next()
之前,您在某处有一个resultSet.getInt(..)
Your problem is not in the code that you showed. You are having a
resultSet.getInt(..)
somewhere before callingresultSet.first()
or.next()