0
votes

I have done a lot of searching about this topic through Google but I have yet to find someone using getString() in the way that I am using it so I have not been able to fix this issue in the normal ways that are suggested.

What I am trying to do is to obtain all of the information from the database and then use it to populate a table model within the program. I do so by obtaining the data with getString and place it into a String[] object. Here is my MySQLConnection class:

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

However I am always getting a dialog error message stating those error messages in the title:

Exception: Before start of result set

Exception: After end of result set

Here are the stack traces:

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)

The errors are actually being generated from the rSet.getInt(1); in my getRowCount() method.

The data is still added and everything works correctly but it is just annoying to have to dismiss those message windows. Does anyone have some suggestions?

3
can you please post more code and the StackTrace to see how the code behaves and explain you what kind of error/warning you're facing? - Luiggi Mendoza
Write your complete code for the class. - Bhavik Ambani
Write complete stack trace of the error you are getting - Bhavik Ambani

3 Answers

3
votes

you need to call rSet.next() in getRowCount() before getting the result (and i don't even think you are creating the row count query anywhere in this code).

also, you are not closing your database resources before discarding them, which will cause resource leaks. always close database resources in finally blocks as soon as you are finished with them.

0
votes

I think you are positioning the cursor before the first row and then requesting data. You need to move the cursor to the first row.

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

It is common to do this in an if statement or loop.

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

Instead of pasing a string as parameter to the ResultSet.getString("ColumnName");, could you try to pas the number of the column, as an int. starts to count @ 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) : returns String
"Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language."

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