帮我写一份Javaswing基于SQLserver的学生信息管理系统

时间: 2024-03-14 07:45:10 浏览: 29
好的,以下是一个基于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(); } } } ``` 这是一个简单的学生信息管理系统的示例代码,您可以根据需求进行修改和完善。希望对您有帮助!

相关推荐

最新推荐

recommend-type

java(swing)+ mysql实现学生信息管理系统

Java Swing + MySQL 实现学生信息管理系统知识点总结 通过对给定的文件信息,总结出以下 Java Swing + MySQL 实现学生信息管理系统的知识点: 一、Java Swing 组件 * 使用 Java Swing 组件创建图形用户界面(GUI...
recommend-type

基于java swing实现答题系统

基于Java Swing实现答题系统的技术要点 本文将详细介绍基于Java Swing实现答题系统的技术要点,涵盖了GUI设计、事件处理、swing组件使用、Java图形化编程等多个方面的知识点。 一、GUI设计 在本文中,我们使用了...
recommend-type

Java学生信息管理系统设计(数据库版)

Java学生信息管理系统设计是一个常见的项目,它通过使用Java编程语言和数据库技术来实现对学生信息的管理。这个系统通常包括学生的基本信息录入、查询、删除、修改和显示等功能,旨在提高教育机构对学生数据管理的...
recommend-type

Java swing读取txt文件实现学生考试系统

Java Swing读取txt文件实现学生考试系统知识点总结 Java Swing是一种用于构建图形用户界面的 Java 库,它提供了许多类和接口来创建窗口、按钮、文本框、标签等 GUI 组件。下面将从 Java Swing 读取 txt 文件实现...
recommend-type

java学生管理系统界面简单实现(全)

在这个示例中,我们使用Java的Swing库来创建一个学生管理系统的图形用户界面。 2. JFrameクラス:JFrame是Java的Swing库中的一种组件,用于创建一个窗口。我们使用JFrame来创建学生管理系统的主窗口。 3. JLabelク...
recommend-type

利用迪杰斯特拉算法的全国交通咨询系统设计与实现

全国交通咨询模拟系统是一个基于互联网的应用程序,旨在提供实时的交通咨询服务,帮助用户找到花费最少时间和金钱的交通路线。系统主要功能包括需求分析、个人工作管理、概要设计以及源程序实现。 首先,在需求分析阶段,系统明确了解用户的需求,可能是针对长途旅行、通勤或日常出行,用户可能关心的是时间效率和成本效益。这个阶段对系统的功能、性能指标以及用户界面有明确的定义。 概要设计部分详细地阐述了系统的流程。主程序流程图展示了程序的基本结构,从开始到结束的整体运行流程,包括用户输入起始和终止城市名称,系统查找路径并显示结果等步骤。创建图算法流程图则关注于核心算法——迪杰斯特拉算法的应用,该算法用于计算从一个节点到所有其他节点的最短路径,对于求解交通咨询问题至关重要。 具体到源程序,设计者实现了输入城市名称的功能,通过 LocateVex 函数查找图中的城市节点,如果城市不存在,则给出提示。咨询钱最少模块图是针对用户查询花费最少的交通方式,通过 LeastMoneyPath 和 print_Money 函数来计算并输出路径及其费用。这些函数的设计体现了算法的核心逻辑,如初始化每条路径的距离为最大值,然后通过循环更新路径直到找到最短路径。 在设计和调试分析阶段,开发者对源代码进行了严谨的测试,确保算法的正确性和性能。程序的执行过程中,会进行错误处理和异常检测,以保证用户获得准确的信息。 程序设计体会部分,可能包含了作者在开发过程中的心得,比如对迪杰斯特拉算法的理解,如何优化代码以提高运行效率,以及如何平衡用户体验与性能的关系。此外,可能还讨论了在实际应用中遇到的问题以及解决策略。 全国交通咨询模拟系统是一个结合了数据结构(如图和路径)以及优化算法(迪杰斯特拉)的实用工具,旨在通过互联网为用户提供便捷、高效的交通咨询服务。它的设计不仅体现了技术实现,也充分考虑了用户需求和实际应用场景中的复杂性。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】基于TensorFlow的卷积神经网络图像识别项目

![【实战演练】基于TensorFlow的卷积神经网络图像识别项目](https://img-blog.csdnimg.cn/20200419235252200.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MTQ4OTQw,size_16,color_FFFFFF,t_70) # 1. TensorFlow简介** TensorFlow是一个开源的机器学习库,用于构建和训练机器学习模型。它由谷歌开发,广泛应用于自然语言
recommend-type

CD40110工作原理

CD40110是一种双四线双向译码器,它的工作原理基于逻辑编码和译码技术。它将输入的二进制代码(一般为4位)转换成对应的输出信号,可以控制多达16个输出线中的任意一条。以下是CD40110的主要工作步骤: 1. **输入与编码**: CD40110的输入端有A3-A0四个引脚,每个引脚对应一个二进制位。当你给这些引脚提供不同的逻辑电平(高或低),就形成一个四位的输入编码。 2. **内部逻辑处理**: 内部有一个编码逻辑电路,根据输入的四位二进制代码决定哪个输出线应该导通(高电平)或保持低电平(断开)。 3. **输出**: 输出端Y7-Y0有16个,它们分别与输入的编码相对应。当特定的
recommend-type

全国交通咨询系统C++实现源码解析

"全国交通咨询系统C++代码.pdf是一个C++编程实现的交通咨询系统,主要功能是查询全国范围内的交通线路信息。该系统由JUNE于2011年6月11日编写,使用了C++标准库,包括iostream、stdio.h、windows.h和string.h等头文件。代码中定义了多个数据结构,如CityType、TrafficNode和VNode,用于存储城市、交通班次和线路信息。系统中包含城市节点、交通节点和路径节点的定义,以及相关的数据成员,如城市名称、班次、起止时间和票价。" 在这份C++代码中,核心的知识点包括: 1. **数据结构设计**: - 定义了`CityType`为short int类型,用于表示城市节点。 - `TrafficNodeDat`结构体用于存储交通班次信息,包括班次名称(`name`)、起止时间(原本注释掉了`StartTime`和`StopTime`)、运行时间(`Time`)、目的地城市编号(`EndCity`)和票价(`Cost`)。 - `VNodeDat`结构体代表城市节点,包含了城市编号(`city`)、火车班次数(`TrainNum`)、航班班次数(`FlightNum`)以及两个`TrafficNodeDat`数组,分别用于存储火车和航班信息。 - `PNodeDat`结构体则用于表示路径中的一个节点,包含城市编号(`City`)和交通班次号(`TraNo`)。 2. **数组和变量声明**: - `CityName`数组用于存储每个城市的名称,按城市编号进行索引。 - `CityNum`用于记录城市的数量。 - `AdjList`数组存储各个城市的线路信息,下标对应城市编号。 3. **算法与功能**: - 系统可能实现了Dijkstra算法或类似算法来寻找最短路径,因为有`MinTime`和`StartTime`变量,这些通常与路径规划算法有关。 - `curPath`可能用于存储当前路径的信息。 - `SeekCity`函数可能是用来查找特定城市的函数,其参数是一个城市名称。 4. **编程语言特性**: - 使用了`#define`预处理器指令来设置常量,如城市节点的最大数量(`MAX_VERTEX_NUM`)、字符串的最大长度(`MAX_STRING_NUM`)和交通班次的最大数量(`MAX_TRAFFIC_NUM`)。 - `using namespace std`导入标准命名空间,方便使用iostream库中的输入输出操作。 5. **编程实践**: - 代码的日期和作者注释显示了良好的编程习惯,这对于代码维护和团队合作非常重要。 - 结构体的设计使得数据组织有序,方便查询和操作。 这个C++代码实现了全国交通咨询系统的核心功能,涉及城市节点管理、交通班次存储和查询,以及可能的路径规划算法。通过这些数据结构和算法,用户可以查询不同城市间的交通信息,并获取最优路径建议。