生成Java通过连接数据库并在窗口显示编写代码 专家管理。对专家信息的导入、导出管理。专家信息一般包括姓名、专业、年龄、职称、所在单位,职务等。 (3)项目管理。对项目信息的导入、导出管理。项目信息一般包括项目类别,项目名称,项目主持人,项目参与人员,项目介绍,项目所属学科。 (4)项目评审专家分配。对系统项目,设置专家分配规则。简单规则:可根据项目类别或主持人所在单位随机分配3名以上专家(5)统计。按照专家或项目等分类查看结果,导出结果。
时间: 2024-03-02 12:48:43 浏览: 59
以下是Java连接数据库并在窗口显示专家管理的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ExpertManagement extends JFrame implements ActionListener {
private JLabel nameLabel, majorLabel, ageLabel, titleLabel, companyLabel, positionLabel;
private JTextField nameField, majorField, ageField, titleField, companyField, positionField;
private JButton addButton, updateButton, deleteButton, importButton, exportButton;
private JTable expertTable;
private JScrollPane scrollPane;
private Connection conn;
private Statement stmt;
private ResultSet rs;
public ExpertManagement() {
super("Expert Management");
nameLabel = new JLabel("Name:");
majorLabel = new JLabel("Major:");
ageLabel = new JLabel("Age:");
titleLabel = new JLabel("Title:");
companyLabel = new JLabel("Company:");
positionLabel = new JLabel("Position:");
nameField = new JTextField(20);
majorField = new JTextField(20);
ageField = new JTextField(20);
titleField = new JTextField(20);
companyField = new JTextField(20);
positionField = new JTextField(20);
addButton = new JButton("Add");
updateButton = new JButton("Update");
deleteButton = new JButton("Delete");
importButton = new JButton("Import");
exportButton = new JButton("Export");
expertTable = new JTable();
scrollPane = new JScrollPane(expertTable);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
add(nameLabel, gc);
gc.gridx = 1;
add(nameField, gc);
gc.gridx = 0;
gc.gridy = 1;
add(majorLabel, gc);
gc.gridx = 1;
add(majorField, gc);
gc.gridx = 0;
gc.gridy = 2;
add(ageLabel, gc);
gc.gridx = 1;
add(ageField, gc);
gc.gridx = 0;
gc.gridy = 3;
add(titleLabel, gc);
gc.gridx = 1;
add(titleField, gc);
gc.gridx = 0;
gc.gridy = 4;
add(companyLabel, gc);
gc.gridx = 1;
add(companyField, gc);
gc.gridx = 0;
gc.gridy = 5;
add(positionLabel, gc);
gc.gridx = 1;
add(positionField, gc);
gc.gridx = 0;
gc.gridy = 6;
add(addButton, gc);
gc.gridx = 1;
add(updateButton, gc);
gc.gridx = 2;
add(deleteButton, gc);
gc.gridx = 0;
gc.gridy = 7;
add(importButton, gc);
gc.gridx = 1;
add(exportButton, gc);
gc.gridx = 0;
gc.gridy = 8;
gc.gridwidth = 3;
add(scrollPane, gc);
addButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);
importButton.addActionListener(this);
exportButton.addActionListener(this);
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
connectToDatabase();
showExpertTable();
}
public void connectToDatabase() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/expert_management", "root", "password");
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public void showExpertTable() {
try {
String sql = "SELECT * FROM expert";
rs = stmt.executeQuery(sql);
expertTable.setModel(buildTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
try {
String name = nameField.getText();
String major = majorField.getText();
int age = Integer.parseInt(ageField.getText());
String title = titleField.getText();
String company = companyField.getText();
String position = positionField.getText();
String sql = "INSERT INTO expert (name, major, age, title, company, position) VALUES ('" + name + "', '" + major + "', '" + age + "', '" + title + "', '" + company + "', '" + position + "')";
stmt.executeUpdate(sql);
showExpertTable();
nameField.setText("");
majorField.setText("");
ageField.setText("");
titleField.setText("");
companyField.setText("");
positionField.setText("");
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == updateButton) {
try {
int row = expertTable.getSelectedRow();
String id = expertTable.getValueAt(row, 0).toString();
String name = nameField.getText();
String major = majorField.getText();
int age = Integer.parseInt(ageField.getText());
String title = titleField.getText();
String company = companyField.getText();
String position = positionField.getText();
String sql = "UPDATE expert SET name='" + name + "', major='" + major + "', age='" + age + "', title='" + title + "', company='" + company + "', position='" + position + "' WHERE id=" + id;
stmt.executeUpdate(sql);
showExpertTable();
nameField.setText("");
majorField.setText("");
ageField.setText("");
titleField.setText("");
companyField.setText("");
positionField.setText("");
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == deleteButton) {
try {
int row = expertTable.getSelectedRow();
String id = expertTable.getValueAt(row, 0).toString();
String sql = "DELETE FROM expert WHERE id=" + id;
stmt.executeUpdate(sql);
showExpertTable();
nameField.setText("");
majorField.setText("");
ageField.setText("");
titleField.setText("");
companyField.setText("");
positionField.setText("");
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == importButton) {
// TODO: Import expert information from file
} else if (e.getSource() == exportButton) {
// TODO: Export expert information to file
}
}
public static void main(String[] args) {
new ExpertManagement();
}
public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// Vector for columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// Vector for data
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}
```
以上代码实现了连接数据库并在窗口中显示专家管理信息的功能,包括添加、更新、删除、导入和导出专家信息等操作。您可以根据自己的需求修改代码。
阅读全文