写一个公园管理系统 用JDBC连接数据库 并且做出图形界面代码
时间: 2024-01-27 14:02:57 浏览: 116
用JDBC管理数据库连接
好的,以下是一个基于Java Swing和JDBC的公园管理系统示例代码,供您参考:
```java
import java.awt.BorderLayout;
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 javax.swing.JButton;
import javax.swing.JFrame;
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 ParkManagementSystem implements ActionListener {
private JFrame frame;
private JPanel panel;
private JButton parkBtn;
private JButton spotBtn;
private JButton visitorBtn;
private JButton orderBtn;
private JTextField searchField;
private JButton searchBtn;
private JButton exitBtn;
private DefaultTableModel tableModel;
private JTable table;
private Connection conn;
private PreparedStatement stmt;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ParkManagementSystem window = new ParkManagementSystem();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ParkManagementSystem() {
initialize();
connect();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
parkBtn = new JButton("公园信息管理");
parkBtn.setBounds(50, 50, 150, 30);
parkBtn.addActionListener(this);
panel.add(parkBtn);
spotBtn = new JButton("景点信息管理");
spotBtn.setBounds(50, 100, 150, 30);
spotBtn.addActionListener(this);
panel.add(spotBtn);
visitorBtn = new JButton("游客信息管理");
visitorBtn.setBounds(50, 150, 150, 30);
visitorBtn.addActionListener(this);
panel.add(visitorBtn);
orderBtn = new JButton("订单管理");
orderBtn.setBounds(50, 200, 150, 30);
orderBtn.addActionListener(this);
panel.add(orderBtn);
searchField = new JTextField();
searchField.setBounds(250, 50, 200, 30);
panel.add(searchField);
searchBtn = new JButton("搜索");
searchBtn.setBounds(470, 50, 80, 30);
searchBtn.addActionListener(this);
panel.add(searchBtn);
exitBtn = new JButton("退出");
exitBtn.setBounds(650, 50, 80, 30);
exitBtn.addActionListener(this);
panel.add(exitBtn);
String[] columnNames = { "编号", "名称", "地址", "面积", "介绍" };
tableModel = new DefaultTableModel(columnNames, 0);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(250, 100, 500, 400);
panel.add(scrollPane);
}
private void connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/park";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
private void disconnect() {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void showParkTable() {
try {
stmt = conn.prepareStatement("select * from park");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString(1);
row[1] = rs.getString(2);
row[2] = rs.getString(3);
row[3] = rs.getString(4);
row[4] = rs.getString(5);
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void clearTable() {
int rowCount = tableModel.getRowCount();
for (int i = rowCount - 1; i >= 0; i--) {
tableModel.removeRow(i);
}
}
private void searchParkTable(String keyword) {
try {
clearTable();
stmt = conn.prepareStatement("select * from park where name like ?");
stmt.setString(1, "%" + keyword + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString(1);
row[1] = rs.getString(2);
row[2] = rs.getString(3);
row[3] = rs.getString(4);
row[4] = rs.getString(5);
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void showParkDialog(String action) {
ParkDialog dialog = new ParkDialog(frame, action, conn);
dialog.setVisible(true);
if (dialog.isUpdated()) {
clearTable();
showParkTable();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == parkBtn) {
showParkDialog("添加");
} else if (e.getSource() == spotBtn) {
// TODO: 实现景点信息管理模块
} else if (e.getSource() == visitorBtn) {
// TODO: 实现游客信息管理模块
} else if (e.getSource() == orderBtn) {
// TODO: 实现订单管理模块
} else if (e.getSource() == searchBtn) {
String keyword = searchField.getText().trim();
searchParkTable(keyword);
} else if (e.getSource() == exitBtn) {
int choice = JOptionPane.showConfirmDialog(frame, "确认退出?", "确认", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
disconnect();
System.exit(0);
}
}
}
}
```
以上代码实现了公园信息管理模块的功能,包括展示公园信息、搜索公园信息、添加公园信息等。其中ParkDialog是一个继承自JDialog的类,用于实现添加公园信息的对话框。您可以根据实际需求进行修改和完善。
阅读全文