java+mysql用gui创建一个5x5的表格导入数据库数据并能进行批量增删改查
时间: 2024-03-23 16:40:57 浏览: 118
java连接mysql数据库 增删改查
好的,我可以为您提供一个有批量增删改查功能的示例代码。以下是一个简单的示例代码:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class TableExample extends JFrame implements ActionListener {
private JTable table;
private DefaultTableModel model;
private JButton addButton;
private JButton deleteButton;
private JButton updateButton;
private JTextField textField;
public TableExample() {
setTitle("Table Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
// Create table model with 5 columns and 5 rows
model = new DefaultTableModel(5, 5);
// Create table with the model
table = new JTable(model);
// Add table to scroll pane
JScrollPane scrollPane = new JScrollPane(table);
// Add scroll pane to frame
add(scrollPane, BorderLayout.CENTER);
// Create buttons and text field
addButton = new JButton("Add");
deleteButton = new JButton("Delete");
updateButton = new JButton("Update");
textField = new JTextField(10);
// Add buttons and text field to panel
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(updateButton);
buttonPanel.add(textField);
// Add panel to frame
add(buttonPanel, BorderLayout.SOUTH);
// Add action listeners to buttons
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
// Load data from database
loadData();
}
// Load data from database into table model
private void loadData() {
// Connect to MySQL database
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "username";
String password = "password";
try {
Connection conn = DriverManager.getConnection(url, user, password);
// Prepare SQL statement to select all data from table
String sql = "SELECT * FROM mytable";
PreparedStatement stmt = conn.prepareStatement(sql);
// Execute SQL statement and get result set
ResultSet rs = stmt.executeQuery();
// Loop through result set and add data to table model
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString("col1");
row[1] = rs.getString("col2");
row[2] = rs.getString("col3");
row[3] = rs.getString("col4");
row[4] = rs.getString("col5");
model.addRow(row);
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Add row to table model and insert data into database
private void addRow() {
String[] row = new String[5];
row[0] = "";
row[1] = "";
row[2] = "";
row[3] = "";
row[4] = "";
model.addRow(row);
// Connect to MySQL database
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "username";
String password = "password";
try {
Connection conn = DriverManager.getConnection(url, user, password);
// Prepare SQL statement to insert data into table
String sql = "INSERT INTO mytable (col1, col2, col3, col4, col5) VALUES (?, ?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
// Get last row index and insert data into database
int rowIdx = model.getRowCount() - 1;
for (int i = 0; i < model.getColumnCount(); i++) {
String value = model.getValueAt(rowIdx, i).toString();
stmt.setString(i + 1, value);
}
stmt.executeUpdate();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Delete selected rows from table model and database
private void deleteRows() {
// Connect to MySQL database
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "username";
String password = "password";
try {
Connection conn = DriverManager.getConnection(url, user, password);
// Loop through selected rows and delete data from table
int[] selectedRows = table.getSelectedRows();
for (int i = selectedRows.length - 1; i >= 0; i--) {
int rowIdx = selectedRows[i];
String value = model.getValueAt(rowIdx, 0).toString();
String sql = "DELETE FROM mytable WHERE col1 = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, value);
stmt.executeUpdate();
model.removeRow(rowIdx);
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Update selected rows in table model and database
private void updateRows() {
// Connect to MySQL database
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "username";
String password = "password";
try {
Connection conn = DriverManager.getConnection(url, user, password);
// Loop through selected rows and update data in table
int[] selectedRows = table.getSelectedRows();
for (int i = 0; i < selectedRows.length; i++) {
int rowIdx = selectedRows[i];
String value = model.getValueAt(rowIdx, 0).toString();
String sql = "UPDATE mytable SET col2 = ?, col3 = ?, col4 = ?, col5 = ? WHERE col1 = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, model.getValueAt(rowIdx, 1).toString());
stmt.setString(2, model.getValueAt(rowIdx, 2).toString());
stmt.setString(3, model.getValueAt(rowIdx, 3).toString());
stmt.setString(4, model.getValueAt(rowIdx, 4).toString());
stmt.setString(5, value);
stmt.executeUpdate();
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Handle button clicks
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
addRow();
} else if (e.getSource() == deleteButton) {
deleteRows();
} else if (e.getSource() == updateButton) {
updateRows();
}
}
public static void main(String[] args) {
TableExample example = new TableExample();
example.setVisible(true);
}
}
```
请注意,此代码仅提供了基本的框架,您需要根据您的具体需求进行修改和完善。同时,您需要将MySQL的驱动程序添加到您的项目中,以便代码能够正确地连接到数据库。
阅读全文