1、Java学生信息管理Swing窗体实验名称:实验5:综合性程序设计简单学生信息管理系统班级:* 姓名:Zzz 学号:20151601*一、实验目的二、实验内容与结果 (1)任务描述(1.3与1.4二选一,建议选择1.4)利用课程所学知识,设计并实现一个简单学生信息管理系统,要求如下: 1.1 声明Student类,该类实现Serializable接口以表明该类可以进行序列化。该类有姓名、学号(long),math、os、java用来存放对应的成绩,在构造方法中进行姓名、学号、课程成绩的赋值。Override由Object继承来的toString方法以便以友好格式显示自己的属性,格式为:张三 1
2、2 os:90 java:90 math: 90。 1.2 建立一个类,利用数据库来存储多个Student,写完一个方法在main中写一段测试代码,运行以保证目前所做工作的正确性。有以下方法: add(Student stu):可以增加新的学生,并保存在数据库中。 update(Student stu):可以修改特定的学生,并保存在数据库中。 dispAll():可以显示所有的学生信息。 findById(long id):可以按照学号来查找,并显示符合条件的学生信息,查无该人的话显示错误信息。 findByName(String name):可以按照姓名查找学生,找到后显示其信息,查无此人显
3、示错误信息。 delById(long id):可以按照id删除学生的信息,然后显示找到该人。若查无此人,显示相应的错误信息。 1.3 (控制台方式)完善应用程序,实现相应的功能如下:添加学生信息、修改指定学号的学生信息、显示所有学生信息、按学号查找、按姓名查找、按学号删除、按成绩排序和退出。 1.4 (Swing窗体方式)完善应用程序,实现相应的功能如下:添加学生信息、修改指定学号的学生信息、显示所有学生信息、按学号查找、按姓名查找、按学号删除、按成绩排序和退出。(2)处理思路及关键技术说明(3)程序完整源码(要求格式规范,适当注释)(4)运行结果截图(按每个操作分别截图)(2)处理思路及关
4、键技术说明分三个模块:一是 jdbc的代码,另一个是 Swing和AWT的代码,剩下是Student类。 Jdbc 一共有两个类:DBUtil(连接工具类)和Sql类(处理jdbc功能的静态方法) Swing 和AWT一个有两个类:MyJFrame构建总窗口,MyJPanel 对窗口内的面板行修饰。然后就是Student类和Main主方法类。关键部分:1)在主窗体中add三个Jpanel,底层和第二层左右三个,右边的Jpanel实现cardLayout布局,左边的Jpanel添加按钮并添加ActionListener来实现对象的card切换达到不同按钮进入不同功能面板的目的。2)在MyJPan
5、el类中,所有的init方法均传递参数JPanel p,对传递过来的Panel进行加工修饰,并且调用Sql里面对应功能的方法。3)Sql中的显示全部信息的方法中使用了ArrayList来存储从studenttbl中读取的对象信息,存储之后为了按照String的方式显示在JtextArea中,又用了listToString方法,通过for循环并追加回车,使每个对象都转化为String并在后面加上了回车(separator),4)在Sql中的查找,删除,添加都使用了jdbc中的预处理PreparedStatement在操作完毕后执行sts.executeUpdate();5)在Sql排序方法中,把
6、所有的信息都存储到list中以后,调用工具类Collections.sort:根据指定比较器产生的顺序对指定列表进行排序。(3)程序完整源码(要求格式规范,适当注释)class DBUtilpackage test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBUtil private static String driver; private static String url; private static String usernam
7、e; private static String password; static driver=com.mysql.jdbc.Driver; url=jdbc:mysql:/localhost:3306/mydb; username=root; password=123456; public static Connection open() try Class.forName(driver); return DriverManager.getConnection(url,username,password); catch (Exception e) e.printStackTrace();
8、return null; public static void close(Connection conn) if(conn!=null) try conn.close(); catch (SQLException e) e.printStackTrace(); class Sqlpackage test;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;impo
9、rt java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextArea;public class Sql public static String listToString(List list, char separator) /使list返回String类型并加入控制符(换行) StringB
10、uilder sb = new StringBuilder(); for (int i = 0; i list.size(); i+) sb.append(list.get(i).append(separator); return sb.toString().substring(0,sb.toString().length()-1); public static List disAll() /打印显示所有信息 /查询 Connection conn=DBUtil.open(); String sql=select id,name,frctionJava,fractionMath,fractio
11、nOS from studentTbl; Statement stmt; try stmt = conn.createStatement(); ResultSet rs=stmt.executeQuery(sql); List list=new ArrayList(); /遍历操作 while(rs.next() int id=rs.getInt(1); String name=rs.getString(2); double a=rs.getDouble(3); double b=rs.getDouble(4); double c=rs.getDouble(5); Student u=new
12、Student(); u.setId(id); u.setName(name); u.setFractionJava(a); u.setFractionMath(b); u.setFractionOS(c); list.add(u); conn.close(); return list; catch (SQLException e) e.printStackTrace(); return null; public static void FindByID(long id ,JLabel lblNewLabel )/按ID查找 Connection conn=DBUtil.open(); Res
13、ultSet rs = null; try String sql=select * from StudentTbl where id=?; PreparedStatement sts=conn.prepareStatement(sql); sts.setLong(1, id); rs=sts.executeQuery(); while(rs.next() int i=rs.getInt(1); String name=rs.getString(2); double a=rs.getDouble(3); double b=rs.getDouble(4); double c=rs.getDoubl
14、e(5); lblNewLabel.setText( | +i+ | +name+ | +a+ | +b+ | +c); catch(Exception e) e.printStackTrace(); finally try if(rs.first()!=true) lblNewLabel.setText(查无此人!); catch (SQLException e) e.printStackTrace(); public static void FindByName(String s, JLabel lblNewLabel) /按Name查找 Connection conn=DBUtil.op
15、en(); ResultSet rs = null; try String sql=select * from StudentTbl where name=?; PreparedStatement sts=conn.prepareStatement(sql); sts.setString(1, s); rs=sts.executeQuery(); while(rs.next() int id=rs.getInt(1); String name=rs.getString(2); double a=rs.getDouble(3); double b=rs.getDouble(4); double
16、c=rs.getDouble(5); lblNewLabel.setText( | +id+ | +name+ | +a+ | +b+ | +c); catch(Exception e) e.printStackTrace(); finally try if(rs.first()!=true) lblNewLabel.setText(查无此人!); catch (SQLException e) e.printStackTrace(); public static void DeleteByID(long id)/删除 Connection conn=DBUtil.open(); Prepare
17、dStatement sts=null; int a=0; try sts=conn.prepareStatement(delete from StudentTbl where id=?); sts.setLong(1, id); a=sts.executeUpdate(); if(a0) JOptionPane.showMessageDialog(null, 删除成功!, WO, JOptionPane.INFORMATION_MESSAGE); if(a=0) JOptionPane.showMessageDialog(null, 无此人!, WO, JOptionPane.INFORMA
18、TION_MESSAGE); catch(Exception e) e.printStackTrace(); DBUtil.close(conn); public static void Sort(JTextArea ta,int index) /排序 /查询 Connection conn=DBUtil.open(); String sql=select id,name,frctionJava,fractionMath,fractionOS from studentTbl; Statement stmt; try stmt = conn.createStatement(); ResultSe
19、t rs=stmt.executeQuery(sql); List list=new ArrayList(); /遍历操作 while(rs.next() int id=rs.getInt(1); String name=rs.getString(2); double a=rs.getDouble(3); double b=rs.getDouble(4); double c=rs.getDouble(5); Student u=new Student(); u.setId(id); u.setName(name); u.setFractionJava(a); u.setFractionMath
20、(b); u.setFractionOS(c); list.add(u); Collections.sort(list,new Comparator() public int compare(Student o1, Student o2) int i = 1 ; if(index=1) i= (int) (o2.getFractionJava() - o1.getFractionJava(); if(index=2) i = (int) (o2.getFractionMath() - o1.getFractionMath(); if(index=3) i = (int) (o2.getFrac
21、tionOS() - o1.getFractionOS(); return i; ); String s=listToString(list, n); ta.setText(s); catch (SQLException e) e.printStackTrace(); DBUtil.close(conn); public static void add(Student stu) /添加 Connection conn=DBUtil.open(); PreparedStatement sts; try sts = conn.prepareStatement(insert into Student
22、Tbl values(?,?,?,?,?); sts.setLong(1, stu.getId(); sts.setString(2, stu.getName(); sts.setDouble(3, stu.getFractionJava(); sts.setDouble(4, stu.getFractionMath(); sts.setDouble(5, stu.getFractionOS(); sts.executeUpdate(); catch (SQLException e) e.printStackTrace(); DBUtil.close(conn); public static
23、void UpdateStudent(Student stu)/更新Connection conn=DBUtil.open(); try String sql=update studenttbl set name=+stu.getName()+,FrctionJava=+ stu.getFractionJava()+,FractionMath=+stu.getFractionMath()+,FractionOS=+stu.getFractionOS()+ where id=+stu.getId()+; PreparedStatement sts=conn.prepareStatement(sq
24、l); sts.executeUpdate(); catch(Exception e) e.printStackTrace(); class Studentpackage test;import java.io.Serializable;public class Student implements Serializable private static final long serialVersionUID =4558876142427402513L; private String name; private long id; private double fractionOS; priva
25、te double fractionJava; private double fractionMath; public String getName() return name; public void setName(String name) this.name = name; public long getId() return id; public void setId(long id) this.id = id; public double getFractionOS() return fractionOS; public void setFractionOS(double fract
26、ionOS) this.fractionOS = fractionOS; public double getFractionJava() return fractionJava; public void setFractionJava(double fractionJava) this.fractionJava = fractionJava; public double getFractionMath() return fractionMath; public void setFractionMath(double fractionMath) this.fractionMath = fract
27、ionMath; public String toString() return (name+ +id+ Java:+fractionJava+ Math:+fractionMath+ OS:+fractionOS); class MyJFramepackage test;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import java.awt.Color;import javax.swing.SwingConstants;import javax.swing.JLabel;import java.awt.Font;public class MyJFrame
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1