NetBeans には、「ファッションとフットウェア」と「ショッピング カート」という 2 つのフォームがあります。最初のフォームには 4 つのボタンが含まれています。もう 1 つのテーブルには、CurrentPurchases と PreviousPurchases の 2 つのテーブルが含まれています。最初のフォームでいずれかのボタンをクリックすると、アイテム名と価格が別のフォームの CurrentPurchases テーブルに転送されます。これを達成するには、どのコード/クエリを使用する必要がありますか? このコードを試しましたが、うまくいきませんでした。
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{}
この行はエラーを示しています:
DefaultTableModel CurrPurchases=(DefaultTableModel)CurrentPurchases.getModel();
注:CurrentPurchases
表はこの形式ではなく、別の形式です。