帮我写一份Javaswing基于SQLserver的学生信息管理系统
时间: 2024-03-14 09:45:10 浏览: 60
好的,以下是一个基于Java Swing和SQL Server的学生信息管理系统的示例代码:
1. 数据库连接部分
```java
import java.sql.*;
public class DatabaseConnection {
static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=student";
static final String USER = "yourusername";
static final String PASSWORD = "yourpassword";
public Connection getConnection() {
Connection conn = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
```
2. 主界面部分
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame implements ActionListener {
private JButton addButton, searchButton, updateButton, deleteButton, reportButton;
public MainFrame() {
super("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
addButton = new JButton("添加");
searchButton = new JButton("查询");
updateButton = new JButton("修改");
deleteButton = new JButton("删除");
reportButton = new JButton("报告");
addButton.addActionListener(this);
searchButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);
reportButton.addActionListener(this);
add(addButton);
add(searchButton);
add(updateButton);
add(deleteButton);
add(reportButton);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
new AddFrame();
} else if (e.getSource() == searchButton) {
new SearchFrame();
} else if (e.getSource() == updateButton) {
new UpdateFrame();
} else if (e.getSource() == deleteButton) {
new DeleteFrame();
} else if (e.getSource() == reportButton) {
new ReportFrame();
}
}
}
```
3. 添加学生信息部分
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class AddFrame extends JFrame implements ActionListener {
private JLabel nameLabel, genderLabel, ageLabel, majorLabel;
private JTextField nameText, genderText, ageText, majorText;
private JButton addButton, cancelButton;
public AddFrame() {
super("添加学生信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(5, 2));
nameLabel = new JLabel("姓名:");
genderLabel = new JLabel("性别:");
ageLabel = new JLabel("年龄:");
majorLabel = new JLabel("专业:");
nameText = new JTextField(20);
genderText = new JTextField(20);
ageText = new JTextField(20);
majorText = new JTextField(20);
addButton = new JButton("添加");
cancelButton = new JButton("取消");
addButton.addActionListener(this);
cancelButton.addActionListener(this);
add(nameLabel);
add(nameText);
add(genderLabel);
add(genderText);
add(ageLabel);
add(ageText);
add(majorLabel);
add(majorText);
add(addButton);
add(cancelButton);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameText.getText().trim();
String gender = genderText.getText().trim();
int age = Integer.parseInt(ageText.getText().trim());
String major = majorText.getText().trim();
Connection conn = new DatabaseConnection().getConnection();
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO student VALUES (?, ?, ?, ?)");
ps.setString(1, name);
ps.setString(2, gender);
ps.setInt(3, age);
ps.setString(4, major);
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "添加成功!");
dispose();
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == cancelButton) {
dispose();
}
}
}
```
4. 查询学生信息部分
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SearchFrame extends JFrame implements ActionListener {
private JLabel nameLabel;
private JTextField nameText;
private JButton searchButton, cancelButton;
private JTable table;
public SearchFrame() {
super("查询学生信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout());
nameLabel = new JLabel("姓名:");
nameText = new JTextField(20);
searchButton = new JButton("查询");
cancelButton = new JButton("取消");
searchButton.addActionListener(this);
cancelButton.addActionListener(this);
JPanel topPanel = new JPanel();
topPanel.add(nameLabel);
topPanel.add(nameText);
topPanel.add(searchButton);
topPanel.add(cancelButton);
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
add(topPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
setSize(500, 300);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
String name = nameText.getText().trim();
Connection conn = new DatabaseConnection().getConnection();
try {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM student WHERE name=?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
DefaultTableModel model = new DefaultTableModel();
model.addColumn("姓名");
model.addColumn("性别");
model.addColumn("年龄");
model.addColumn("专业");
while (rs.next()) {
Object[] row = new Object[4];
row[0] = rs.getString("name");
row[1] = rs.getString("gender");
row[2] = rs.getInt("age");
row[3] = rs.getString("major");
model.addRow(row);
}
table.setModel(model);
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == cancelButton) {
dispose();
}
}
}
```
5. 修改学生信息部分
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UpdateFrame extends JFrame implements ActionListener {
private JLabel nameLabel, genderLabel, ageLabel, majorLabel;
private JTextField nameText, genderText, ageText, majorText;
private JButton searchButton, updateButton, cancelButton;
public UpdateFrame() {
super("修改学生信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(5, 2));
nameLabel = new JLabel("姓名:");
genderLabel = new JLabel("性别:");
ageLabel = new JLabel("年龄:");
majorLabel = new JLabel("专业:");
nameText = new JTextField(20);
genderText = new JTextField(20);
ageText = new JTextField(20);
majorText = new JTextField(20);
searchButton = new JButton("查询");
updateButton = new JButton("修改");
cancelButton = new JButton("取消");
searchButton.addActionListener(this);
updateButton.addActionListener(this);
cancelButton.addActionListener(this);
nameText.setEditable(false);
genderText.setEditable(false);
ageText.setEditable(false);
majorText.setEditable(false);
add(nameLabel);
add(nameText);
add(genderLabel);
add(genderText);
add(ageLabel);
add(ageText);
add(majorLabel);
add(majorText);
add(updateButton);
add(cancelButton);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
String name = JOptionPane.showInputDialog(this, "请输入要修改的学生姓名:");
Connection conn = new DatabaseConnection().getConnection();
try {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM student WHERE name=?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
nameText.setText(rs.getString("name"));
genderText.setText(rs.getString("gender"));
ageText.setText(Integer.toString(rs.getInt("age")));
majorText.setText(rs.getString("major"));
nameText.setEditable(true);
genderText.setEditable(true);
ageText.setEditable(true);
majorText.setEditable(true);
} else {
JOptionPane.showMessageDialog(this, "未找到该学生信息!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == updateButton) {
String name = nameText.getText().trim();
String gender = genderText.getText().trim();
int age = Integer.parseInt(ageText.getText().trim());
String major = majorText.getText().trim();
Connection conn = new DatabaseConnection().getConnection();
try {
PreparedStatement ps = conn.prepareStatement("UPDATE student SET gender=?, age=?, major=? WHERE name=?");
ps.setString(1, gender);
ps.setInt(2, age);
ps.setString(3, major);
ps.setString(4, name);
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "修改成功!");
dispose();
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == cancelButton) {
dispose();
}
}
}
```
6. 删除学生信息部分
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DeleteFrame extends JFrame implements ActionListener {
private JLabel nameLabel;
private JTextField nameText;
private JButton searchButton, deleteButton, cancelButton;
public DeleteFrame() {
super("删除学生信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(2, 2));
nameLabel = new JLabel("姓名:");
nameText = new JTextField(20);
searchButton = new JButton("查询");
deleteButton = new JButton("删除");
cancelButton = new JButton("取消");
searchButton.addActionListener(this);
deleteButton.addActionListener(this);
cancelButton.addActionListener(this);
add(nameLabel);
add(nameText);
add(searchButton);
add(deleteButton);
add(cancelButton);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
String name = nameText.getText().trim();
Connection conn = new DatabaseConnection().getConnection();
try {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM student WHERE name=?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int option = JOptionPane.showConfirmDialog(this, "是否确认删除该学生信息?");
if (option == JOptionPane.YES_OPTION) {
ps = conn.prepareStatement("DELETE FROM student WHERE name=?");
ps.setString(1, name);
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "删除成功!");
dispose();
}
} else {
JOptionPane.showMessageDialog(this, "未找到该学生信息!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == deleteButton) {
dispose();
} else if (e.getSource() == cancelButton) {
dispose();
}
}
}
```
7. 生成报告部分
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
public class ReportFrame extends JFrame implements ActionListener {
private JButton reportButton, cancelButton;
public ReportFrame() {
super("生成报告");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new FlowLayout());
reportButton = new JButton("生成报告");
cancelButton = new JButton("取消");
reportButton.addActionListener(this);
cancelButton.addActionListener(this);
add(reportButton);
add(cancelButton);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == reportButton) {
Connection conn = new DatabaseConnection().getConnection();
try {
URL url = getClass().getResource("/report/student_info.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(url.openStream());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
JasperViewer.viewReport(jasperPrint, false);
File file = new File("D:/student_info.pdf");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfStream(jasperPrint, fos);
fos.close();
JOptionPane.showMessageDialog(this, "报告已生成!");
dispose();
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == cancelButton) {
dispose();
}
}
}
```
这是一个简单的学生信息管理系统的示例代码,您可以根据需求进行修改和完善。希望对您有帮助!
阅读全文