如何將我的數據從mysql表中存入我的JTable?我希望能夠將UPDATE,DELETE和ADD操作放入mysql表中。 這應該是我的餐廳應用程序,並且我希望用戶在JTable中看到餐館庫存並單擊表格按鈕時,它會打開一個新框架,用戶可以在該框架中添加,刪除或從庫存中將產品從該表格中取出。一旦產品被添加或從表格消息中刪除,mysql表將自動更新將會很酷。在JTable中顯示來自mysql的數據
package project_bar;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class MainMenu extends javax.swing.JFrame {
Connection conn = new DBConnection().connect();
PreparedStatement pst = null;
ResultSet rs = null;
public MainMenu() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
logout_bt = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
table1_bt = new javax.swing.JButton();
table1_lb = new javax.swing.JLabel();
table2_lb = new javax.swing.JLabel();
table2_bt = new javax.swing.JButton();
table3_lb = new javax.swing.JLabel();
table3_bt = new javax.swing.JButton();
Bauturi_lb = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Table_stock = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
logout_bt.setText("LOGOUT");
logout_bt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
logout_btActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Verdana", 3, 18)); // NOI18N
jLabel1.setText("Tamas Vlad Restaurant");
table1_bt.setForeground(new java.awt.Color(204, 204, 204));
table1_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/round_table-128.png"))); // NOI18N
table1_bt.setText("TABLE 1");
table1_bt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
table1_btActionPerformed(evt);
}
});
table1_lb.setText("TABLE 1");
table2_lb.setText("Table 2");
table2_bt.setForeground(new java.awt.Color(204, 204, 204));
table2_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/round_table-128.png"))); // NOI18N
table2_bt.setText("TABLE 1");
table2_bt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
table2_btActionPerformed(evt);
}
});
table3_lb.setText("Table 3");
table3_bt.setForeground(new java.awt.Color(204, 204, 204));
table3_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/round_table-128.png"))); // NOI18N
table3_bt.setText("TABLE 1");
table3_bt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
table3_btActionPerformed(evt);
}
});
Bauturi_lb.setFont(new java.awt.Font("Vani", 3, 24)); // NOI18N
Bauturi_lb.setText("DRINKS ON STOCK");
Table_stock.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"Name", "Quantity", "Price"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
Table_stock.setCellSelectionEnabled(true);
Table_stock.addAncestorListener(new javax.swing.event.AncestorListener() {
public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
}
public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
Table_stockAncestorAdded(evt);
}
public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
}
});
jScrollPane1.setViewportView(Table_stock);
Table_stock.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(logout_bt)
.addGap(334, 334, 334)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 599, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(41, 41, 41)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(Bauturi_lb, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE))
.addGap(138, 138, 138)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(table1_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(table1_lb, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(123, 123, 123)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(table2_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(table2_lb, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(135, 135, 135)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(table3_lb)
.addComponent(table3_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(logout_bt))
.addGap(68, 68, 68)
.addComponent(Bauturi_lb, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 124, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(table1_lb)
.addComponent(table2_lb)
.addComponent(table3_lb))
.addGap(24, 24, 24)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(table1_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(table2_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(table3_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
.addGap(556, 556, 556))
);
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()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void table1_btActionPerformed(java.awt.event.ActionEvent evt) {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
new Table1().show();
}
private void logout_btActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
new Login(null,true).show();
}
private void table2_btActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
new Table1().show();
}
private void table3_btActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
new Table1().show();
}
private void Table_stockAncestorAdded(javax.swing.event.AncestorEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainMenu().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Bauturi_lb;
private javax.swing.JTable Table_stock;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton logout_bt;
private javax.swing.JButton table1_bt;
private javax.swing.JLabel table1_lb;
private javax.swing.JButton table2_bt;
private javax.swing.JLabel table2_lb;
private javax.swing.JButton table3_bt;
private javax.swing.JLabel table3_lb;
// End of variables declaration
}
執行SQL查詢,從數據庫'ResultSet'中取出行信息,將數據包裝在POJO中並將其添加到表模型中。通常使用基於'AbstractTableModel'(恕我直言)的自定義'TableModel'更好,因爲它可以讓您更好地控制數據的管理方式。請參見[JDBC數據庫訪問](http://docs.oracle.com/javase/tutorial/jdbc/)和[如何使用表](http://docs.oracle.com/javase/tutorial/uiswing/components/ table.html) – MadProgrammer
我用@MadProgrammer所提到的完成了同樣的工作,並做了一些調整以適合我的需求。爲什麼你想在這種情況下使用自動更新?這不像廚房開始製作漢堡,然後把漢堡放進去製作披薩。您的網絡帶寬也會降低您需要處理的更多查詢,尤其是自動更新 –
自動更新僅適用於庫存部分..所以如果您沒有更多可樂,請留下警報messaje會彈出 –