写一个Java通讯系统。要求实现增删改查的功能
时间: 2024-03-08 07:49:56 浏览: 88
好的,下面是一个简单的Java通讯系统实现,包括增删改查的功能。这个系统使用了Java Swing来构建GUI界面,使用JDBC来连接MySQL数据库。
注意:这个示例仅用于演示Java通讯系统的实现方式,实际应用中需要根据具体的业务需求进行调整。
```java
import java.awt.EventQueue;
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 java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class CommunicationSystem {
private JFrame frame;
private JTextField textFieldUsername;
private JTextField textFieldPassword;
private JTable table;
private DefaultTableModel tableModel;
private Connection conn;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CommunicationSystem window = new CommunicationSystem();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CommunicationSystem() {
initialize();
connectToDatabase();
loadData();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(10, 10, 80, 20);
frame.getContentPane().add(lblUsername);
textFieldUsername = new JTextField();
textFieldUsername.setBounds(100, 10, 100, 20);
frame.getContentPane().add(textFieldUsername);
textFieldUsername.setColumns(10);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(10, 40, 80, 20);
frame.getContentPane().add(lblPassword);
textFieldPassword = new JTextField();
textFieldPassword.setBounds(100, 40, 100, 20);
frame.getContentPane().add(textFieldPassword);
textFieldPassword.setColumns(10);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addData();
}
});
btnAdd.setBounds(220, 10, 80, 20);
frame.getContentPane().add(btnAdd);
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteData();
}
});
btnDelete.setBounds(220, 40, 80, 20);
frame.getContentPane().add(btnDelete);
JButton btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateData();
}
});
btnUpdate.setBounds(310, 10, 80, 20);
frame.getContentPane().add(btnUpdate);
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchData();
}
});
btnSearch.setBounds(310, 40, 80, 20);
frame.getContentPane().add(btnSearch);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 70, 414, 181);
frame.getContentPane().add(scrollPane);
tableModel = new DefaultTableModel();
tableModel.addColumn("ID");
tableModel.addColumn("Username");
tableModel.addColumn("Password");
table = new JTable(tableModel);
scrollPane.setViewportView(table);
}
/**
* Connect to the MySQL database.
*/
private void connectToDatabase() {
String url = "jdbc:mysql://localhost:3306/communication_system";
String user = "root";
String password = "123456";
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Load data from the database.
*/
private void loadData() {
String sql = "SELECT * FROM user";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
Vector<String> rowData = new Vector<String>();
rowData.add(Integer.toString(id));
rowData.add(username);
rowData.add(password);
tableModel.addRow(rowData);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Add data to the database.
*/
private void addData() {
String username = textFieldUsername.getText();
String password = textFieldPassword.getText();
if (username == null || username.length() == 0 || password == null || password.length() == 0) {
JOptionPane.showMessageDialog(frame, "Username and password cannot be empty.");
return;
}
String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
Vector<String> rowData = new Vector<String>();
rowData.add("");
rowData.add(username);
rowData.add(password);
tableModel.addRow(rowData);
JOptionPane.showMessageDialog(frame, "Add data successfully.");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Add data failed.");
}
}
/**
* Delete data from the database.
*/
private void deleteData() {
int selectedRow = table.getSelectedRow();
if (selectedRow < 0) {
JOptionPane.showMessageDialog(frame, "Please select a row to delete.");
return;
}
int id = Integer.parseInt((String) tableModel.getValueAt(selectedRow, 0));
String sql = "DELETE FROM user WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
tableModel.removeRow(selectedRow);
JOptionPane.showMessageDialog(frame, "Delete data successfully.");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Delete data failed.");
}
}
/**
* Update data in the database.
*/
private void updateData() {
int selectedRow = table.getSelectedRow();
if (selectedRow < 0) {
JOptionPane.showMessageDialog(frame, "Please select a row to update.");
return;
}
int id = Integer.parseInt((String) tableModel.getValueAt(selectedRow, 0));
String username = textFieldUsername.getText();
String password = textFieldPassword.getText();
if (username == null || username.length() == 0 || password == null || password.length() == 0) {
JOptionPane.showMessageDialog(frame, "Username and password cannot be empty.");
return;
}
String sql = "UPDATE user SET username = ?, password = ? WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.setInt(3, id);
pstmt.executeUpdate();
tableModel.setValueAt(username, selectedRow, 1);
tableModel.setValueAt(password, selectedRow, 2);
JOptionPane.showMessageDialog(frame, "Update data successfully.");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Update data failed.");
}
}
/**
* Search data in the database.
*/
private void searchData() {
String username = textFieldUsername.getText();
String sql = "SELECT * FROM user WHERE username = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery();
tableModel.setRowCount(0);
while (rs.next()) {
int id = rs.getInt("id");
String password = rs.getString("password");
Vector<String> rowData = new Vector<String>();
rowData.add(Integer.toString(id));
rowData.add(username);
rowData.add(password);
tableModel.addRow(rowData);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
这个Java通讯系统使用了JDBC连接MySQL数据库,实现了增删改查的功能。在GUI界面中,用户可以输入用户名和密码,然后通过点击对应的按钮来实现增删改查的功能。这个系统还可以根据用户名来进行查询,查询结果会显示在表格中。
阅读全文