JDBC ResultSet.getString ResultSet Exception的开始/结束之前/之后

时间:2012-11-11 04:56:07

标签: java mysql exception jdbc

我已经通过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:1074)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)at at   com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854)at at   com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2709)at 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(未知来源)at   javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)at   javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)at   javax.swing.DefaultButtonModel.setPressed(未知来源)at   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知   来自)java.awt.Component.processMouseEvent(未知来源)at   javax.swing.JComponent.processMouseEvent(未知来源)at   java.awt.Component.processEvent(未知来源)at   java.awt.Container.processEvent(未知来源)at   java.awt.Component.dispatchEventImpl(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at   java.awt.LightweightDispatcher.processMouseEvent(未知来源)at   java.awt.LightweightDispatcher.dispatchEvent(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Window.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.EventQueue.dispatchEventImpl(未知来源)at   java.awt.EventQueue.access $ 200(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来源)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue $ 4.run(未知来源)at   java.awt.EventQueue $ 4.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue.dispatchEvent(未知来源)at   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知   来自java.awt.EventDispatchThread.pumpEvents(未知来源)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at   java.awt.EventDispatchThread.run(未知来源)

     

java.sql.SQLException:结果集结束后   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)at at   com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854)at at   com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2709)at 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(未知来源)at   javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)at   javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)at   javax.swing.DefaultButtonModel.setPressed(未知来源)at   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知   来自)java.awt.Component.processMouseEvent(未知来源)at   javax.swing.JComponent.processMouseEvent(未知来源)at   java.awt.Component.processEvent(未知来源)at   java.awt.Container.processEvent(未知来源)at   java.awt.Component.dispatchEventImpl(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at   java.awt.LightweightDispatcher.processMouseEvent(未知来源)at   java.awt.LightweightDispatcher.dispatchEvent(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Window.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.EventQueue.dispatchEventImpl(未知来源)at   java.awt.EventQueue.access $ 200(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来源)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue $ 4.run(未知来源)at   java.awt.EventQueue $ 4.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue.dispatchEvent(未知来源)at   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知   来自java.awt.EventDispatchThread.pumpEvents(未知来源)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at   java.awt.EventDispatchThread.run(未知来源)

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

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

3 个答案:

答案 0 :(得分:3)

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

另外,在丢弃数据库资源之前,不会关闭它们,这会导致资源泄漏。 总是在完成后立即关闭finally块中的数据库资源。

答案 1 :(得分:0)

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

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

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

if(rSet.next()){
   foundType = result.getString(1);
}

答案 2 :(得分: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
“以Java编程语言中String的形式检索此ResultSet对象的当前行中指定列的值。”

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