私のJFrameの内部
すべてがちょうど生成された私は、JTableのフレームに私のデータベースの更新バージョンを示す上で問題が生じています...、私はちょうどこのリンクでの指示に従ってください:Binding JTable with MySQL
UPDATESデータベースの新しいJFrameを追加しました。ビューインベントリ(JFrameはMySQLでデータを表示するJTableになっています)、それは更新されたバージョンを表示していません。データベース。
しかし、アプリケーションを閉じてViewInventory JFrameを実行した後、既に更新されていました。
...これは、生成されたコードです助けてください:
public class ViewInventory extends javax.swing.JFrame {
public ViewInventory() {
initComponents();
setLocation(300, 120);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
inventoryPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("inventoryPU").createEntityManager();
itemsQuery = java.beans.Beans.isDesignTime() ? null : inventoryPUEntityManager.createQuery("SELECT i FROM Items i");
itemsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : itemsQuery.getResultList();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setAutoCreateRowSorter(true);
jTable1.setAlignmentX(1.0F);
jTable1.setAlignmentY(2.0F);
jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
jTable1.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, itemsList, jTable1);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
columnBinding.setColumnName("Id");
columnBinding.setColumnClass(Integer.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${itemName}"));
columnBinding.setColumnName("Item Name");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${category}"));
columnBinding.setColumnName("Category");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${QBox}"));
columnBinding.setColumnName("QBox");
columnBinding.setColumnClass(Integer.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${unitPriceBox}"));
columnBinding.setColumnName("Unit Price Box");
columnBinding.setColumnClass(Double.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${subTotalBox}"));
columnBinding.setColumnName("Sub Total Box");
columnBinding.setColumnClass(Double.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${QPc}"));
columnBinding.setColumnName("QPc");
columnBinding.setColumnClass(Integer.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${unitPricePc}"));
columnBinding.setColumnName("Unit Price Pc");
columnBinding.setColumnClass(Double.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${subTotalPc}"));
columnBinding.setColumnName("Sub Total Pc");
columnBinding.setColumnClass(Double.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${grandTotal}"));
columnBinding.setColumnName("Grand Total");
columnBinding.setColumnClass(Double.class);
bindingGroup.addBinding(jTableBinding);
jScrollPane1.setViewportView(jTable1);
jButton1.setText("Main Menu");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addComponent(jButton1)
.addContainerGap())
);
bindingGroup.bind();
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new Menu().setVisible(true);
setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ViewInventory().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.persistence.EntityManager inventoryPUEntityManager;
private java.util.List<inventory.Items> itemsList;
private javax.persistence.Query itemsQuery;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
}
私は、生成されたコードにすべてを表示されている場合、私は申し訳ありません。
try {
Class.forName(Connect.DRIVER);
Connection con = DriverManager.getConnection(Connect.CONNECTION_STRING,
Connect.USERNAME, Connect.PASSWORD);
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery("SELECT id, category,"
+ "q_box, unit_price_box, sub_total_box, q_pc, "
+ "unit_price_pc, sub_total_pc, grand_total from items WHERE "
+ "item_name = '" + item_name_combobox.getSelectedItem() + "';");
String category;
int q_box, q_pc;
double unit_price_box, sub_total_box, unit_price_pc, sub_total_pc,
grand_total;
if (rs.next()) {
q_box = rs.getInt("q_box");
category = rs.getString("category");
unit_price_box = rs.getDouble("unit_price_box");
sub_total_box = rs.getDouble("sub_total_box");
q_pc = rs.getInt("q_pc");
unit_price_pc = rs.getDouble("unit_price_pc");
sub_total_pc = rs.getDouble("sub_total_pc");
grand_total = rs.getDouble("grand_total");
double new_sub_total_box = q_box * Integer.parseInt(new_unit_price_box_txtfld.getText());
double new_sub_total_pc = q_pc * Integer.parseInt(new_unit_price_pc_txtfld.getText());
grand_total = new_sub_total_box + new_sub_total_pc;
stat.executeUpdate("UPDATE items SET unit_price_box = " + new_unit_price_box_txtfld.getText()
+ ", unit_price_pc = " + new_unit_price_pc_txtfld.getText()
+ ", sub_total_box = " + new_sub_total_box
+ ", sub_total_pc = "+ new_sub_total_pc
+ ", grand_total = "+ grand_total
+ " WHERE item_name = '"
+ item_name_combobox.getSelectedItem() + "';");
sub_total_box_txtfld.setText(Double.toString(new_sub_total_box));
sub_total_pc_txtfld.setText(Double.toString(new_sub_total_pc));
grand_total_txtfld.setText(Double.toString(grand_total));
lblmsg.setText("Record Saved!");
con.close();
}
} catch (Exception e) {
lblmsg.setText("Error: "+ e.getMessage());
System.out.println("Error: "+ e.getMessage());
}
あなたの投稿を編集しました。元に戻す場合は...ただ1つの通知 - DB文の場合は、最後にブロックを追加し、このブロックの内部にConnectionとResultSetも閉じなければなりません – mKorbel