如何将我的数据从mysql表输入我的JTable?我希望能够在mysql表中进行UPDATE,DELETE和ADD操作。 这应该是我的餐馆应用程序,我希望用户在该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
}
答案 0 :(得分:1)
DbConnection conn = new DbConnection();
Connection connection = conn.getConnection();
Statement st;
Vector data = new Vector();
try {
st = connection.createStatement();
ResultSet res = st.executeQuery("SELECT col_name FROM table_name");
ResultSetMetaData metaData = res.getMetaData();
int columns = metaData.getColumnCount();
while (res.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(res.getObject(i));
}
data.addElement(row);
}
} catch (SQLException e) {
e.printStackTrace();
}
Vector columnNames = new Vector();
columnNames.addElement("col_1");
columnNames.addElement("col_name_n");
table = new JTable(data,columnNames);
你可以使用类似上面代码的东西。 脚步: 1.你与数据库建立联系 2.您执行查询 3.将数据库中的数据存储到向量中 4.使用存储在向量
中的数据创建Jtable