0

我已经通过 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:在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) 的 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) 开始结果集之前。 jdbc.SQLError.createSQLException(SQLError.java:974) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) 在 com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854) 在 com.mysql .jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2709) 在 MySQLConnect.getRowCount(MySQLConnect.java:100) 在 MySQLConnect.updateTable(MySQLConnect.java:68) 在 MySQLConnect.addDonor(MySQLConnect.java:42) 在 Operations.addDonor (Operations.java:135) 在 GUI$EventHandler.actionPerformed(GUI.java:145) 在 javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 在 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(未知来源)在 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.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 的 pumpEvents(Unknown Source)java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 的 pumpEvents(Unknown Source)

java.sql.SQLException:在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) 的 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) 结果集结束后。 jdbc.SQLError.createSQLException(SQLError.java:974) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) 在 com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854) 在 com.mysql .jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2709) 在 MySQLConnect.getRowCount(MySQLConnect.java:100) 在 MySQLConnect.updateTable(MySQLConnect.java:68) 在 Operations.updateTable(Operations.java:164) 在 GUI$EventHandler .actionPerformed(GUI.java:148) 在 javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 在 javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 在 javax.swing。javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax. DefaultButtonModel.fireActionPerformed(Unknown Source)。 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。 java.awt.Container 中的 dispatchEvent(Unknown Source)。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 (未知源)在 java.awt.EventQueue$3.run(未知源)在 java.awt.EventQueue$3.run(未知源)在 java.security.AccessController.doPrivileged(Native Method) 在 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 java.security.ProtectionDomain 的 .doPrivileged(Native Method) $1.doIntersectionPrivilege(Unknown Source) 的 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。 java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 的 pumpEvents(Unknown Source)EventDispatchThread.run(未知来源)EventDispatchThread.run(未知来源)

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

数据仍然被添加并且一切正常,但是不得不关闭那些消息窗口只是很烦人。有人有什么建议吗?

4

3 回答 3

3

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

此外,您不会在丢弃数据库资源之前关闭它们,这将导致资源泄漏。 完成后,请始终在 finally 块中关闭数据库资源。

于 2012-11-11T05:08:16.370 回答
0

我认为您将光标定位在第一行之前,然后请求数据。您需要将光标移动到第一行。

 rSet.next();
 String foundType = rSet.getString(1);

通常在 if 语句或循环中执行此操作。

if(rSet.next()){
   foundType = result.getString(1);
}
于 2012-11-11T05:08:25.400 回答
0

与其将字符串作为参数传递给 ResultSet.getString("ColumnName");,不如尝试将列的编号作为 int 传递。开始计数@ 1。

ResultSet rs = getRSet();
if(rs != null) {
    while(rs.next()) {
        String rows[] = { rs.getString(1), rs.getString(2), rs.getString(3) };
        model.add(rows);
    }
} //else, error handling

getString(int columnIndex) :返回 String
“检索此 ResultSet 对象的当前行中指定列的值,作为 Java 编程语言中的 String。”

来源:http ://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html

于 2012-11-11T05:09:56.707 回答