I have these two forms. One contains several buttons. The other contains two tables. On the click of one button the data is saved in a database and is then transferred to the JTable in the other form. I wrote the code through the first form only. But it doesn't work. Here's the code:
int dress1=100;
DefaultTableModel CurrPurchases=(DefaultTableModel) CurrentPurchases.getModel();
double price1=Double.parseDouble(Price1.getText());
int rows=CurrPurchases.getRowCount();
if(rows > 0){
for (int i = 0; i < rows; i++) {
CurrPurchases.removeRow(0);
}
}
try{
Connection connection=getConnection();
ResultSet curprs=null;
Statement buy1stmt=connection.createStatement();
String Buy1Query1="Update Products set Quantity=Quantity-1 where Product_no=1;";
String Buy1Query2="Insert into Buy values('"+Pname1.getText()+"',"+price1+");";
buy1stmt.executeUpdate(Buy1Query1);
buy1stmt.executeUpdate(Buy1Query2);
dress1--;
if(dress1==0){
JOptionPane.showMessageDialog(null,"Sorry, This Item is Out of Stock!");
}
new ShoppingCart().setVisible(true);
PreparedStatement buy2stmt=connection.prepareStatement("Select * from Buy;");
curprs=buy2stmt.executeQuery();
if(curprs.last()){
CurrPurchases.addRow(new Object[]{curprs.getString(1),curprs.getDouble(2)});
}
}
catch(Exception ex){
ex.printStackTrace();
}
finally{}
But this line in specific shows error(I mean underlined with a red line):
DefaultTableModel CurrPurchases=(DefaultTableModel) CurrentPurchases.getModel();
How can I add the data into that table (CurrentPurchases) in the other form? Note: I don't want to add any button to the form, I just want the data to be displayed.
Any help would be appreciated.
import javax.swing.table.DefaultTableModel;. Give it a try and let me know how it went :)