1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java swing开发学生成绩管理系统

java swing开发学生成绩管理系统

时间:2023-10-28 13:02:51

相关推荐

java  swing开发学生成绩管理系统

临近期末,java实验报告,真的是很手忙脚乱,看了那么多篇,没有一篇说的比较完整的,那这篇的话就整体的说一下吧,

这个是用swing开发的学生成绩管理系统,其中与数据库建立连接,数据库用的是SQL server ,java开发环境需要用到eclipise

因为个人知识储备的不足,因此代码中有部分功能不能全部实现,本篇的源码来源于GitHub,感谢网友分享。

本题的源代码如下

package practice_student;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.*;import java.util.Arrays;import java.util.List;import java.util.Vector;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Student_Mangement extends JFrame {//程序入口private JPanel panel_1 = new JPanel();private JButton btnConfim = new JButton("登陆");private JButton btnSignUp = new JButton("注册");private JTextField userName = new JTextField(20);private JPasswordField pass = new JPasswordField(20);private JLabel userLaber = new JLabel("用户名");private JLabel passLaber = new JLabel("密码");JLabel text = new JLabel("欢迎使用学生成绩管理系统!");private ConnetDB conn = new ConnetDB();private ImageIcon icon=new ImageIcon("D:/icon.jpg");public static void main(String[] args) {Student_Mangement enterGui=new Student_Mangement();}public Student_Mangement(){super();this.setIconImage(icon.getImage());this.setTitle("学生成绩管理系统");this.setBounds(650, 250, 500, 400);this.setVisible(true);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLayout(null);panel_1.setLayout(null);panel_1.setBounds(0, 50, 600, 250);text.setBounds(140, 0, 250, 50);btnConfim.setBounds(150, 130, 70, 30);btnSignUp.setBounds(250,130,70,30);userLaber.setBounds(95, 50, 40, 30);passLaber.setBounds(95, 80, 40, 30);userName.setBounds(135, 50, 200, 30);pass.setBounds(135, 80, 200, 30);panel_1.add(text);panel_1.add(userLaber);panel_1.add(userName);panel_1.add(passLaber);panel_1.add(pass);panel_1.add(btnConfim);panel_1.add(btnSignUp);this.add(panel_1);setActionLintener();}private void setActionLintener(){btnConfim.addActionListener(new ActionListener() {//内部类实现主界面class MainGUI extends JFrame{final int COLUMN=10;private final List<String> TITLE= Arrays.asList("姓名","学号","教师","院系","英语成绩","高数成绩","大物成绩","平均分","最高分","最低分");Vector<Vector<String>> dataModel=new Vector<>();private JMenuBar bar=new JMenuBar();private JMenu menu_stu =new JMenu("管理学生成绩");private JMenu menu_course =new JMenu("课程成绩排名");private JMenu menu_exUser =new JMenu("退出登陆");private JMenuItem showAll =new JMenuItem("显示所有学生成绩");private JMenuItem addStu =new JMenuItem("添加学生");private JMenuItem findByName =new JMenuItem("按关键字查询");private JMenuItem showEng =new JMenuItem("按英语成绩排名");private JMenuItem showMath =new JMenuItem("按高数成绩排名");private JMenuItem showPhy =new JMenuItem("按大物成绩排名");private JMenuItem userEx =new JMenuItem("退出登录");private JButton btnDelete =new JButton("删除此条");private JButton btnChange =new JButton("修改此条");private JTable table;private ConnetDB conn=new ConnetDB();public MainGUI(){super();this.setTitle("学生成绩管理系统");this.setBounds(650,250,800,500);this.setVisible(true);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setIconImage(icon.getImage());//菜单栏menu_stu.add(showAll);menu_stu.add(addStu);menu_stu.add(findByName);menu_course.add(showEng);menu_course.add(showMath);menu_course.add(showPhy);menu_exUser.add(userEx);bar.add(menu_stu);bar.add(menu_course);bar.add(menu_exUser);this.setJMenuBar(bar);Vector<String> titles=new Vector<>(TITLE);table=new JTable(dataModel,titles);table.getTableHeader().setReorderingAllowed(false);//表头不可拖动for (int i = 0; i < COLUMN; i++) {if(1==i||i==3){//这2列数据更长一些table.getColumnModel().getColumn(i).setPreferredWidth(150);}}//承载table的panelJPanel tablePanel=new JPanel();tablePanel.setLayout(new BoxLayout(tablePanel,BoxLayout.Y_AXIS));this.add(tablePanel);JScrollPane jScrollPane=new JScrollPane();jScrollPane.setViewportView(table);tablePanel.add(jScrollPane, BorderLayout.CENTER);tablePanel.add(btnChange);tablePanel.add(btnDelete);tablePanel.updateUI();setActionListener();}//监听事件private void setActionListener(){btnChange.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {int row = table.getSelectedRow();int column = table.getSelectedColumn();if (row == -1 || column == 0) return;String val = dataModel.get(row).get(column);String name = dataModel.get(row).get(0);//解决表列名和table列名不一样String convert="";if(TITLE.get(column).equals("学号")) {convert = "number";}else if (TITLE.get(column).equals("教师")) {convert = "teacher";}else if (TITLE.get(column).equals("院系")) {convert = "major";}else if (TITLE.get(column).equals("英语成绩")) {convert = "english";}else if (TITLE.get(column).equals("高数成绩")) {convert = "math";}else if (TITLE.get(column).equals("大物成绩")) {convert = "physic";}String sql = "update Student set " + convert + " = ? where Sname = '"+name+"';";PreparedStatement ps;try {ps = conn.getConnect().prepareStatement(sql);if (TITLE.get(column).equals("学号")||TITLE.get(column).equals("英语成绩")||TITLE.get(column).equals("高数成绩")||TITLE.get(column).equals("大物成绩")) {ps.setInt(1, Integer.parseInt(val));}else {ps.setString(1, val);}ps.executeUpdate();} catch (SQLException e1) {e1.printStackTrace();}table.validate();table.updateUI();}});btnDelete.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {int row = table.getSelectedRow();String sname = dataModel.get(row).get(0);String sql = "delete from Student where Sname = '" + sname + "';";try {if (conn.getConnect().createStatement().executeUpdate(sql) == 0) return;dataModel.remove(row);//更新表格table.validate();table.updateUI();} catch (SQLException e1) {e1.printStackTrace();}}});showAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dataModel.clear();Statement stmt;try {stmt =conn.getConnect().createStatement();ResultSet rs = stmt.executeQuery("select *from Student");initTable(rs);} catch (SQLException e1) {e1.printStackTrace();}//更新表格table.validate();table.updateUI();}});addStu.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {class AddStu extends JFrame {ConnetDB conn = new ConnetDB();private JTextField txName=new JTextField();private JTextField txNo=new JTextField();private JTextField txTeacher=new JTextField();private JTextField txCol=new JTextField();private JTextField txEng=new JTextField();private JTextField txMath=new JTextField();private JTextField txPhy=new JTextField();private JButton btnComfirm=new JButton("确认");private JPanel panel=new JPanel();private JLabel lbName =new JLabel("姓名");private JLabel lbNo =new JLabel("学号");private JLabel lbTeacher =new JLabel("教师");private JLabel lbCol =new JLabel("院系");private JLabel lbEng =new JLabel("英语成绩");private JLabel lbMath =new JLabel("高数成绩");private JLabel lbPhy =new JLabel("大物成绩");public AddStu(){super();this.setTitle("输入学生信息");this.setBounds(820, 330, 350, 300);this.setVisible(true);this.setResizable(false);this.setIconImage(icon.getImage());panel.setLayout(null);txName.setBounds(70,5,200,30);txNo.setBounds(70,35,200,30);txTeacher.setBounds(70,65,200,30);txCol.setBounds(70,95,200,30);txEng.setBounds(70,125,200,30);txMath.setBounds(70,155,200,30);txPhy.setBounds(70,185,200,30);btnComfirm.setBounds(130,220,70,30);lbName.setBounds(30,5,200,30);lbNo.setBounds(30,35,200,30);lbTeacher.setBounds(30,65,200,30);lbCol.setBounds(30,95,200,30);lbEng.setBounds(20,125,200,30);lbMath.setBounds(20,155,200,30);lbPhy.setBounds(20,185,200,30);panel.add(lbName);panel.add(txName);panel.add(lbNo);panel.add(txNo);panel.add(lbTeacher);panel.add(txTeacher);panel.add(lbCol);panel.add(txCol);panel.add(lbEng);panel.add(txEng);panel.add(lbMath);panel.add(txMath);panel.add(lbPhy);panel.add(txPhy);panel.add(btnComfirm);this.add(panel);btnComfirm.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String sname="";int sNo=Integer.parseInt(txNo.getText());String tname="";String col="";int eng=Integer.parseInt(txEng.getText());int math=Integer.parseInt(txMath.getText());int phy=Integer.parseInt(txPhy.getText());if (!("".equals(txName.getText()))){sname=txName.getText();}if (!("".equals(txTeacher.getText()))){tname=txTeacher.getText();} if (!("".equals(txCol.getText()))){col=txCol.getText();}String sql="insert into Student(Sname,Sno," +"Steacher,Sdept,SEnglish,SMath,SPhystis) ";String finalSname = sname;String finalTname = tname;String finalCol = col;new Thread(() -> {//新开线程添加数据PreparedStatement ps;try {ps=conn.getConnect().prepareStatement(sql);ps.setString(1, finalSname);ps.setInt(2, sNo);ps.setString(3, finalTname);ps.setString(4, finalCol);ps.setInt(5, eng);ps.setInt(6, math);ps.setInt(7, phy);ps.executeUpdate();table.validate();table.updateUI();} catch (SQLException e1) {e1.printStackTrace();}}).start();dispose();}});}}AddStu addStu=new AddStu();}});findByName.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {class FindByName extends JFrame{String input ="";JTextField txInput=new JTextField();JPanel panel=new JPanel();JLabel lbInput =new JLabel("输入");JButton btnComfirm=new JButton("确认");public FindByName() {super();this.setTitle("请输入学号或姓名或老师或院系");this.setBounds(820, 330, 350, 200);this.setVisible(true);this.setResizable(false);this.setIconImage(icon.getImage());panel.setLayout(null);txInput.setBounds(70, 20, 200, 30);lbInput.setBounds(40, 20, 200, 30);btnComfirm.setBounds(120, 60, 70, 30);panel.add(lbInput);panel.add(txInput);panel.add(btnComfirm);this.add(panel);btnComfirm.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {input =txInput.getText();//使用正则表达式提取输入数字String regEx="[^0-9]";Pattern p = pile(regEx);Matcher m = p.matcher(input);//为解决关键词查询String与number的冲突int forNumber;if ("".equals(m.replaceAll("").trim())){forNumber=999999;}else forNumber=Integer.parseInt(m.replaceAll("").trim());dataModel.clear();try {Statement stmt = conn.getConnect().createStatement();ResultSet rs = stmt.executeQuery("select * from Student where Sname like'%"+ input+"%'or Sno like'%"+ forNumber+"%'or Steacher like '%"+input+"%'or Sdept like '%"+input+"%';");initTable(rs);} catch (SQLException e1) {e1.printStackTrace();}dispose();table.validate();table.updateUI();}});}}new Thread(() -> {FindByName finder=new FindByName();}).start();}});showEng.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dataModel.clear();try {Statement stmt =conn.getConnect().createStatement();ResultSet rs = stmt.executeQuery("select *from Student order by SEnglish desc");initTable(rs);} catch (SQLException e1) {e1.printStackTrace();}//更新表格table.validate();table.updateUI();}});showMath.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dataModel.clear();try {Statement stmt =conn.getConnect().createStatement();ResultSet rs = stmt.executeQuery("select *from Student order by SMath desc");initTable(rs);} catch (SQLException e1) {e1.printStackTrace();}//更新表格table.validate();table.updateUI();}});showPhy.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dataModel.clear();try {Statement stmt = conn.getConnect().createStatement();ResultSet rs = stmt.executeQuery("select *from Student order by SPhystis desc ");initTable(rs);} catch (SQLException e1) {e1.printStackTrace();}//更新表格table.validate();table.updateUI();}});userEx.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {setVisible(false);Student_Mangement enterGui=new Student_Mangement();}});}int Max(int a,int b,int c){if (a>=b&&a>=c)return a;if (b>=a&&b>=c)return b;if (c>=a&&c>=b)return c;else return 0;}int Min(int a,int b,int c){if (a<=b&&a<=c)return a;if (b<=a&&b<=c)return b;if (c<=a&&c<=b)return c;else return 0;}//table赋值private void initTable(ResultSet rs) throws SQLException {Vector<String> record;while (rs.next()) {record = new Vector<String>();for (int i = 0; i < COLUMN; i++) {if(i<7) {record.add(rs.getString(i + 1));}else if (i==7){//平均成绩record.add(Integer.toString(((rs.getInt(7)+rs.getInt(5)+rs.getInt(6))/3)));}else if (i==8){record.add(Integer.toString(Max(rs.getInt(5),rs.getInt(6),rs.getInt(7))));}else if (i==9){record.add(Integer.toString(Min(rs.getInt(5),rs.getInt(6),rs.getInt(7))));}}dataModel.add(record);}}}@Overridepublic void actionPerformed(ActionEvent e) {Statement statement;ResultSet resultSet;String username = "";String password = "";username = userName.getText();password = String.valueOf(pass.getPassword()).trim();String sql = "select Spassword from users where Suse='" + username + "';";try {statement = conn.getConnect().createStatement();resultSet = statement.executeQuery(sql);System.out.println("执行查询");if (resultSet.next()) {//用户名对应密码相等则进入主界面if (resultSet.getString(1).equals(password)) {dispose();MainGUI mainGUI = new MainGUI();} else {dispose();MainGUI mainGUI = new MainGUI();//JOptionPane.showMessageDialog(null, "用户名或密码错误",//"提示", JOptionPane.INFORMATION_MESSAGE);}}else JOptionPane.showMessageDialog(null, "用户名或密码错误","提示", JOptionPane.INFORMATION_MESSAGE);} catch (SQLException e1) {e1.printStackTrace();}}});btnSignUp.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {class Register extends JFrame {private JTextField txName=new JTextField();private JTextField txPass=new JTextField();private JButton btnComfirm=new JButton("确认");private JLabel lbInputName =new JLabel("用户名");private JLabel lbInputPass =new JLabel("密码");private JPanel panel=new JPanel();private ConnetDB conn=new ConnetDB();public Register(){super();this.setTitle("注册");this.setBounds(720, 320, 350, 200);this.setVisible(true);this.setResizable(false);panel.setLayout(null);this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);txName.setBounds(70, 20, 200, 30);txPass.setBounds(70, 60, 200, 30);lbInputName.setBounds(30, 20, 40, 30);lbInputPass.setBounds(40, 60, 40, 30);btnComfirm.setBounds(120, 100, 70, 30);this.add(panel);panel.add(lbInputName);panel.add(txName);panel.add(lbInputPass);panel.add(txPass);panel.add(btnComfirm);panel.updateUI();btnComfirm.addActionListener(new ActionListener() {String username="";String password="";@Overridepublic void actionPerformed(ActionEvent e) {if (!("".equals(txName.getText()))){username=txName.getText();}if (!("".equals(txPass.getText()))){password=txPass.getText();}String sql="insert into users values(?,?);";PreparedStatement ps;try {ps = conn.getConnect().prepareStatement(sql);ps.setString(1, username);ps.setString(2, password);ps.executeUpdate();JOptionPane.showMessageDialog(null, "注册成功","提示", JOptionPane.INFORMATION_MESSAGE);} catch (SQLException e1) {e1.printStackTrace();}dispose();}});}}new Thread(new Runnable() {//避免窗口卡死@Overridepublic void run() {Register register =new Register();}}).start();}});}class ConnetDB {public Connection getConnect(){Connection connect = null;//登陆数据库的用户String username="sa";String password="123456";final String URL="jdbc:sqlserver://192.168.0.104:1433;DatabaseName=Test";try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");connect =DriverManager.getConnection(URL,username,password);System.out.println("连接成功");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return connect;}}}

因为代码是一体的,直接建立一个类就可以使用,运行截图,读者可自行运行程序,来获得截图,这里就不放截图了

然后这其中最重要的一步就是连接数据库,接下来我将叙述连接SQL server数据库的方法,

首先因为之前安装数据库的时候都是选择的是Windows用户安装,现在我们需要将其改变为用户登录模式,改变方法为

/question/756938170935896004.html

/weixin_30952535/article/details/95422715

当然那个登录名sa也是可以修改成你的名字,设置好这个以后,然后关闭软件,这回可以尝试用用户名和密码登录。

接下来

那个IP有几个弄几个就行,我的反正是没有IP10,

开启win 7的Telent服务

若提示“不能打开到主机的连接,在端口 1433: 连接失败”,则说明1433端口没有打开,需要重新进行以上配置

配置环境变量

右击 我的电脑 → 属性 → 高级系统设置(高级) → 环境变量,在系统变量中双击CLASSPATH变量(或选中CLASSPATH后 → 编辑),在最后面追加 “;D:\sqljdbc4 \sqljdbc4.jar” (注意最前面有个 ; )若不存在CLASSPATH,就新建CLASSPATH变量,并且将其值设为“D:\sqljdbc4 \sqljdbc4.jar”。

为了以防万一则需要在每一个文件夹下都放一个jar,下载jdbc请参考这篇文章/weixin_42220532/article/details/82729997

里边还详细的写明了如何在java中连接eclipise的代码,下载完jdbc以后,然后把jdbc放到相应的目录下,如下图

建好以后一定得在项目上刷新一下以确定建立成功,然后在你建立的项目上点击属性

导入以后,就可以写一段代码来连接数据库,因为我用的是SQL server ,所以在撰写的时候用的连接方式是SQL server的格式,读者可根据自己的数据库(Oracle MySQL )进行连接

packagepkg;importjava.sql.*;publicclass Main {publicstatic void main(String [] args){StringdriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";StringdbURL="jdbc:sqlserver://localhost:1433;DatabaseName=你的数据库名";String userName="填写你的用户名,我的是sa";String userPwd="填写你的密码";try{Class.forName(driverName);ConnectiondbConn=DriverManager.getConnection(dbURL,userName,userPwd);System.out.println("连接数据库成功");}catch(Exception e){e.printStackTrace();System.out.print("连接失败");} }}

上面的url 中 localhost 说一下,如果你的SQL 在你的本地,那么直接写本地的即可,如果不在本地,在虚拟机里,则需要将localhost替换为虚拟机的IP地址 按Windows +r 然后 cmd然后ipconfig 查看对应IpV4的地址输入即可,DatabaseName 就是你建立数据库的名字,下边的

一定要填写当初用户数据连接时的账号密码,以上就是一个整个的java学生成绩管理系统,通过上述方式可以和数据库建立连接

以上文章参考于下列博主

/weixin_42220532/article/details/82729997

/stewen_001/article/details/19553173

/stewen_001/article/details/19553173

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。