gpt4 book ai didi

java - JDBC ResultSet.getString 在结果集异常开始/结束之前/之后

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:47 31 4
gpt4 key购买 nike

我已经通过 Google 对这个主题进行了大量搜索,但我还没有找到以我使用它的方式使用 getString() 的人,所以我无法以正常的方式解决这个问题建议。

我想做的是从数据库中获取所有信息,然后使用它来填充程序中的表模型。为此,我使用 getString 获取数据并将其放入 String[] 对象中。这是我的 MySQLConnection 类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class MySQLConnect
{
public MySQLConnect()
{
connect = null;
statement = null;
rSet = null;
}

public void Connect(String dbase, String uname, String pword)
{
try
{
Class.forName("com.mysql.jdbc.Driver");

connect = DriverManager.getConnection("jdbc:mysql://localhost/" + dbase , uname, pword);
JOptionPane.showMessageDialog(null, "Connection successful. Please retry your submission." , "Information", JOptionPane.INFORMATION_MESSAGE);
}

catch (Exception e)
{
JOptionPane.showMessageDialog(null, e , "SQL Error", JOptionPane.ERROR_MESSAGE);
}
}

public void addDonor(int did, String dname, String dcname, int damount, DefaultTableModel model)
{
try
{
statement = connect.createStatement();

statement.execute("Insert Into prg421_w5.donors Values ("+ did + ",'" + dname + "','" + dcname + "'," + damount + ")");
JOptionPane.showMessageDialog(null, "Data entry added successfully." , "Information", JOptionPane.INFORMATION_MESSAGE);
getRSet();
updateTable(model);
}

catch (Exception e)
{
JOptionPane.showMessageDialog(null, e , "SQL Error", JOptionPane.ERROR_MESSAGE);
}
}

public void getRSet()
{
try
{
rSet = statement.executeQuery("Select * From donors");
}

catch (Exception e)
{

}
}

public void updateTable (DefaultTableModel model)
{
try
{
for (i = getRowCount(); i > 0; i--)
{
model.removeRow(0);
}
}

catch (Exception e)
{
JOptionPane.showMessageDialog(null, e , "SQL Error", JOptionPane.ERROR_MESSAGE);
}

try
{
while (rSet.next())
{
String row[] = {rSet.getString("DonorName"),rSet.getString("DonorCharity"),((String)rSet.getString("DonationAmount"))};

model.addRow(row);
}
}

catch (Exception e)
{
JOptionPane.showMessageDialog(null, e , "SQL Error", JOptionPane.ERROR_MESSAGE);
}
}

public int getRowCount()
{
try
{
rowCount = rSet.getInt(1);
}

catch (Exception e)
{
JOptionPane.showMessageDialog(null, e , "SQL Error", JOptionPane.ERROR_MESSAGE);
}

return rowCount;
}

public Boolean isConnected()
{
return connect != null;
}

public void Close()
{
try
{
if (rSet != null)
{
rSet.close();
}

if (statement != null)
{
statement.close();
}

if (connect != null)
{
connect.close();
}
}

catch (Exception e)
{
JOptionPane.showMessageDialog(null, e , "SQL Error", JOptionPane.ERROR_MESSAGE);
}
}


private Connection connect;
private Statement statement;
private ResultSet rSet;
private int rowCount, i;

private static Object o = null;
}

但是我总是收到一条对话框错误消息,在标题中说明这些错误消息:

异常:在结果集开始之前

异常:结果集结束后

这里是堆栈跟踪:

java.sql.SQLException: Before start of result set at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854) at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2709) at MySQLConnect.getRowCount(MySQLConnect.java:100) at MySQLConnect.updateTable(MySQLConnect.java:68) at MySQLConnect.addDonor(MySQLConnect.java:42) at Operations.addDonor(Operations.java:135) at GUI$EventHandler.actionPerformed(GUI.java:145) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

java.sql.SQLException: After end of result set at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854) at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2709) at MySQLConnect.getRowCount(MySQLConnect.java:100) at MySQLConnect.updateTable(MySQLConnect.java:68) at Operations.updateTable(Operations.java:164) at GUI$EventHandler.actionPerformed(GUI.java:148) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

错误实际上是由 rSet.getInt(1) 产生的;在我的 getRowCount() 方法中。

数据仍在添加并且一切正常,但不得不关闭这些消息窗口只是令人讨厌。有人有什么建议吗?

最佳答案

在获取结果之前,您需要在 getRowCount() 中调用 rSet.next()(我什至不认为您会在此代码中的任何位置创建行计数查询)。

此外,您在丢弃数据库资源之前没有关闭它们,这将导致资源泄漏。 始终在您使用完数据库资源后立即在 finally block 中关闭它们。

关于java - JDBC ResultSet.getString 在结果集异常开始/结束之前/之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328708/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com