0

I got these two forms in Java which are linked to two tables in MySQL. I've done the connection and the database saves the data entered through the front-end. But the two forms ar linked. It's like when one button is clicked in one form, it sends all the data to the other form in the form of a table. Here's the coding I did for this:

int dress1=100;
double price1=Double.parseDouble(Price1.getText());
DefaultTableModel CurrentPurchases= new DefaultTableModel();
int rows=CurrentPurchases.getRowCount();
if (rows>0){
    for (int i = 0; i < rows; i++) {
    CurrentPurchases.removeRow(0); }
}
try{
    Connection connection=getConnection();
    stmt=connection.createStatement();
    String Buy1Query1="Update Products set Quantity=Quantity-1 where Product_no=1;";
    String Buy1Query2="Insert into Buy values('"+Pname1.getText()+"',"+price1+");";
    stmt.executeUpdate(Buy1Query1);
    stmt.executeUpdate(Buy1Query2);
    dress1--;
    if(dress1==0){
        JOptionPane.showMessageDialog(null,"Sorry, This Item is Out of Stock!");
    }
    new ShoppingCart().setVisible(true);
    String Pname="";
    double Price;
    PreparedStatement buyquery=connection.prepareStatement("Select * from Buy;");
    rs=buyquery.executeQuery();
    Pname=rs.getString("ProductName");
    Price=rs.getDouble("Price");
    CurrentPurchases.addRow(new Object[]{Pname,Price});

}
catch(SQLException ex){
    ex.printStackTrace();
    ex.getErrorCode();
}
finally{}

When I run the file the following errors appear:

java.sql.SQLException
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
        at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)
        at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528)
        at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)
        at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
        at Fashion_Footwear.Buy1ActionPerformed(Fashion_Footwear.java:370)
        at Fashion_Footwear.access$000(Fashion_Footwear.java:19)
        at Fashion_Footwear$1.actionPerformed(Fashion_Footwear.java:100)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6216)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5981)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4583)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

when I clicked on this error:

 at Fashion_Footwear.Buy1ActionPerformed(Fashion_Footwear.java:370)

It gave me this code as error:

    Pname=rs.getString("ProductName");

I'm a newbie to all this, and I'd really appreciate it if someone helps me as soon as possible. Thank you in advance!

Table Buy:

create table buy(ProductName varchar(100),Price decimal(7,2));
5
  • 2
    Could you post the definition of table Buy ? Commented Nov 19, 2013 at 21:45
  • by definition you mean? Commented Nov 19, 2013 at 21:49
  • @LuluLala in MySQL: describe Buy - it will provide details on the table schema Commented Nov 19, 2013 at 21:51
  • Which columns does this table have? And what datatype? For example, you could add the CREATE TABLE statement. Commented Nov 19, 2013 at 21:51
  • oh! I've added it to the question. Commented Nov 19, 2013 at 21:57

1 Answer 1

1

The problem is that your ResultSetis BEFORE the first row. You should use a code like:

PreparedStatement buyquery=connection.prepareStatement("Select * from Buy;");
rs=buyquery.executeQuery();
if (rs.next())
{
    Pname=rs.getString("ProductName");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Okay I tried it, there are no errors shown, but it still doesn't transfer the data to the table in the other form.
So, this answer solved the problem stated in the qüestion? Please, could you accept it?
Now you should open another question with your new problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.