编写一个java swing 系统,该系统基于java语言连接mysql,该系统要实现基本信息录入、修改、查询等功能: 各种信息的输入,包括参与项目信息,包括的数据项有:职工编号、项目编号、项目类别、作者排名、经费、获奖情况。 项目合作信息的处理。 各种信息的添加、修改、删除。 按照某种条件,查询统计符合条件的各类信息。 对查询、统计的结果打印输出。
时间: 2024-04-13 20:30:04 浏览: 89
基于Java swing和mysql实现学生信息管理系统(源码+数据库+运行指导视频)
好的,您想要一个基于 Java Swing 和 MySQL 连接的系统,实现基本信息录入、修改、查询等功能。我会为您提供一个简单的实现方案,请参考以下代码:
首先,您需要创建一个数据库表来存储项目信息和合作信息。在 MySQL 中执行以下 SQL 语句创建表:
```sql
CREATE TABLE projects (
id INT PRIMARY KEY AUTO_INCREMENT,
employee_id INT,
project_id INT,
project_category VARCHAR(100),
author_rank INT,
funding DOUBLE,
award VARCHAR(100)
);
CREATE TABLE collaboration (
id INT PRIMARY KEY AUTO_INCREMENT,
project_id INT,
collaborator_id INT
);
```
接下来,您可以编写 Java Swing 界面和与数据库交互的代码。下面是一个简单的示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ProjectManagementSystem extends JFrame {
private JTextField employeeIdField, projectIdField, projectCategoryField,
authorRankField, fundingField, awardField;
private JButton addButton, updateButton, deleteButton, searchButton;
private JTextArea resultArea;
private Connection connection;
private Statement statement;
public ProjectManagementSystem() {
super("Project Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 创建界面组件
JPanel inputPanel = new JPanel(new GridLayout(7, 2));
inputPanel.add(new JLabel("职工编号:"));
employeeIdField = new JTextField();
inputPanel.add(employeeIdField);
inputPanel.add(new JLabel("项目编号:"));
projectIdField = new JTextField();
inputPanel.add(projectIdField);
inputPanel.add(new JLabel("项目类别:"));
projectCategoryField = new JTextField();
inputPanel.add(projectCategoryField);
inputPanel.add(new JLabel("作者排名:"));
authorRankField = new JTextField();
inputPanel.add(authorRankField);
inputPanel.add(new JLabel("经费:"));
fundingField = new JTextField();
inputPanel.add(fundingField);
inputPanel.add(new JLabel("获奖情况:"));
awardField = new JTextField();
inputPanel.add(awardField);
addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addProject();
}
});
updateButton = new JButton("修改");
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateProject();
}
});
deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteProject();
}
});
searchButton = new JButton("查询");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchProjects();
}
});
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(searchButton);
resultArea = new JTextArea();
// 添加组件到窗口
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
add(new JScrollPane(resultArea), BorderLayout.SOUTH);
// 连接数据库
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password");
statement = connection.createStatement();
} catch (SQLException ex) {
ex.printStackTrace();
System.exit(1);
}
pack();
setVisible(true);
}
private void addProject() {
try {
int employeeId = Integer.parseInt(employeeIdField.getText());
int projectId = Integer.parseInt(projectIdField.getText());
String projectCategory = projectCategoryField.getText();
int authorRank = Integer.parseInt(authorRankField.getText());
double funding = Double.parseDouble(fundingField.getText());
String award = awardField.getText();
String query = "INSERT INTO projects (employee_id, project_id, project_category, author_rank, funding, award) " +
"VALUES (" + employeeId + ", " + projectId + ", '" + projectCategory + "', " +
authorRank + ", " + funding + ", '" + award + "')";
statement.executeUpdate(query);
JOptionPane.showMessageDialog(this, "项目添加成功!");
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "添加项目时发生错误!");
}
}
private void updateProject() {
try {
int projectId = Integer.parseInt(projectIdField.getText());
String projectCategory = projectCategoryField.getText();
int authorRank = Integer.parseInt(authorRankField.getText());
double funding = Double.parseDouble(fundingField.getText());
String award = awardField.getText();
String query = "UPDATE projects SET project_category = '" + projectCategory +
"', author_rank = " + authorRank + ", funding = " + funding +
", award = '" + award + "' WHERE project_id = " + projectId;
statement.executeUpdate(query);
JOptionPane.showMessageDialog(this, "项目更新成功!");
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "更新项目时发生错误!");
}
}
private void deleteProject() {
try {
int projectId = Integer.parseInt(projectIdField.getText());
String query = "DELETE FROM projects WHERE project_id = " + projectId;
statement.executeUpdate(query);
JOptionPane.showMessageDialog(this, "项目删除成功!");
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "删除项目时发生错误!");
}
}
private void searchProjects() {
try {
String query = "SELECT * FROM projects";
ResultSet resultSet = statement.executeQuery(query);
StringBuilder result = new StringBuilder();
while (resultSet.next()) {
int employeeId = resultSet.getInt("employee_id");
int projectId = resultSet.getInt("project_id");
String projectCategory = resultSet.getString("project_category");
int authorRank = resultSet.getInt("author_rank");
double funding = resultSet.getDouble("funding");
String award = resultSet.getString("award");
result.append("职工编号: ").append(employeeId).append(", ")
.append("项目编号: ").append(projectId).append(", ")
.append("项目类别: ").append(projectCategory).append(", ")
.append("作者排名: ").append(authorRank).append(", ")
.append("经费: ").append(funding).append(", ")
.append("获奖情况: ").append(award).append("\n");
}
resultArea.setText(result.toString());
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "查询项目时发生错误!");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ProjectManagementSystem();
}
});
}
}
```
请注意,上述代码中的数据库连接字符串应根据您的实际情况进行修改。另外,这只是一个基本示例,您可能需要根据您的具体需求进行进一步的扩展和改进。
希望这个示例能对您有所帮助!如果您有任何疑问,请随时提问。
阅读全文