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?