需求分析
1 信息需求
对学校而言,学生成绩管理是管理工作中重要的一环,但是高校学生的成绩管理工作量大、繁杂,人工处理非常困难。因此,借助于强大计算机的处理能力,能够把人从繁重的成绩管理工作中解脱出来,并且更加准确、安全、清晰的管理环境。
2 功能需求
能够进行数据库的数据定义、数据操纵、数据控制等处理功能。具体功能应包括:可提供课程安排、课程成绩数据的添加、插入、删除、更新、查询,学生及教职工基本信息查询的功能。
3 安全性与完整性要求
对于学生成绩管理系统数据库来讲,由于其主要数据是学生成绩,只能由本人以及所教老师及教务处知道,因此做好数据安全性是重中之重。另外,要求所有在校学生的信息都要录入其中,并且要设计好个别情况。
E-R图
一位学生会被多位老师教导,一位老师会教导多位学生,所有学生与教师之间是多对多(m:n)的关系;
一位学生可能会选修多门课程,一门课程会被多位学生选修,所以学生与课程之间是多对多(m:n)的关系;
一位学生会有多项成绩(具体指某学生一门课程的分数),一项成绩仅被一位学生拥有,所以学生与成绩是一对多(1:n)的关系;
一位教师会讲授多门课程,一门课程会被一位教师讲授,所以教师与课程的关系是一对多(1:n)的关系;
一门课程拥有多项成绩,一项成绩仅被一门课程拥有,所以课程与成绩的关系是一对多(1:n)的关系;
学生(学号、姓名、班级、性别、专业、出生日期、学分);
老师(教师编号、姓名、学院);
课程(课程编号、教师编号、课程名称、课程学分);
成绩(学号、课程编号、分数);
逻辑关系
根据题目要求可以简化成三个表:
student 表:学号(sno)、姓名(sname)、性别(ssex)、专业(sdept),此为联系“student表”所对应的关系模式,学号为该关系的候选码,满足第三范式。
course表:课程编号(cno)、教师(cteacher)、课程名称(cname)、先修课课程号(cpno)、学分(ccredit),此为联系“course表”所对应的关系模式,课程编号和教师编号为该关系的候选码,满足第三范式。
sc表:学号(sno)、课程编号(cno)、分数(grade),此为联系“sc表”所对应的关系模式,学号和课程编号为该关系的候选码,满足第三范式。
物理设计和实施
创建course表
属性依次为:课程号、课程名、先修课课程号、学分、教师
主键为课程号
创建student表
属性依次为:学号、姓名、性别、年龄、所在系系名
主键为学号
sdept char(15) null勾选
注意学号不要写重复,会报错
创建sc表,表示选课关系
属性依次为学号、课程号、分数
主键为学号、课程号
数据库链接到idea
idea没有database的下载:
File+settings+editor+plugins+database navigator+apply+ok
出现感叹号:进connection+properties+serverTimezone+Asia/Shanghai+test connection
创建包、Add、Del、Update、Sel
主代码再最后面
AddC
package sc;import java.awt.*;import javax.swing.*;import java.sql.*;import java.awt.event.*;public class AddC extends JPanel implements ActionListener{ JTextField 课程号,课程名,先修课课程号,学分; JButton 录入; public AddC(){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} 课程号=new JTextField(12); 课程名=new JTextField(12); 先修课课程号=new JTextField(12); 学分=new JTextField(12); 录入=new JButton("录入"); 录入.addActionListener(this); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box3=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); Box box5=Box.createHorizontalBox(); Box box6=Box.createHorizontalBox(); box1.add(new JLabel("课程号:")); box1.add(课程号); box2.add(new JLabel("课程名:")); box2.add(课程名); box3.add(new JLabel("先修课课程号:")); box3.add(先修课课程号); box4.add(new JLabel("学分:")); box4.add(学分); box6.add(录入); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(box5); boxH.add(box6); boxH.add(Box.createVerticalGlue()); JPanel messPanel=new JPanel(); messPanel.add(boxH); setLayout(new BorderLayout()); add(messPanel,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent c){ Object obj=c.getSource(); if(obj==录入){ if(课程号.getText().equals("")||课程名.getText().equals("")||先修课课程号.getText().equals("")||学分.getText().equals("")){ JOptionPane.showMessageDialog(this,"学生信息请填满再录入!" ); } Statement stmt=null; ResultSet rs=null,rs1=null; String sql,sql1; sql1="select * from Course where Cno='"+课程号.getText()+"'"; sql="insert into Course values('"+课程号.getText()+"','"+课程名.getText()+"','"+先修课课程号.getText()+"','"+学分.getText()+"')"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs1=stmt.executeQuery(sql1); if(rs1.next()){JOptionPane.showMessageDialog(this,"该课号以存在,无法添加");} else{ stmt.executeUpdate(sql); JOptionPane.showMessageDialog(this,"添加成功"); } rs1.close(); stmt.close(); } catch(SQLException e){ System.out.print("SQL Exception occur.Message is:"+e.getMessage()); } } } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
AddS
package sc;import java.awt.*;import javax.swing.*;import java.sql.*;import java.awt.event.*;public class AddS extends JPanel implements ActionListener{ JTextField 学号,姓名,性别,年龄,所在系系名; JButton 录入; public AddS(){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} 学号=new JTextField(12); 姓名=new JTextField(12); 性别=new JTextField(12); 年龄=new JTextField(12); 所在系系名=new JTextField(12); 录入=new JButton("录入"); 录入.addActionListener(this); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box3=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); Box box5=Box.createHorizontalBox(); Box box6=Box.createHorizontalBox(); box1.add(new JLabel("学号:")); box1.add(学号); box2.add(new JLabel("姓名:")); box2.add(姓名); box3.add(new JLabel("性别:")); box3.add(性别); box4.add(new JLabel("年龄:")); box4.add(年龄); box5.add(new JLabel("所在系系名:")); box5.add(所在系系名); box6.add(录入); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(box5); boxH.add(box6); boxH.add(Box.createVerticalGlue()); JPanel messPanel=new JPanel(); messPanel.add(boxH); setLayout(new BorderLayout()); add(messPanel,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent c){ Object obj=c.getSource(); if(obj==录入){ if(学号.getText().equals("")||姓名.getText().equals("")||性别.getText().equals("")||年龄.getText().equals("")||所在系系名.getText().equals("")){ JOptionPane.showMessageDialog(this,"学生信息请填满再录入!" ); } Statement stmt=null; ResultSet rs1=null; String sql,sql1; sql1="select * from Student where Sno='"+学号.getText()+"'"; sql="insert into Student values('"+学号.getText()+"','"+姓名.getText()+"','"+性别.getText()+"','"+年龄.getText()+"','"+所在系系名.getText()+"')"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs1=stmt.executeQuery(sql1); if(rs1.next()){JOptionPane.showMessageDialog(this,"该学号已经存在,无法添加");} else{ stmt.executeUpdate(sql); JOptionPane.showMessageDialog(this,"添加成功"); } rs1.close(); stmt.close(); } catch(SQLException e){ System.out.print("SQL Exception occur.Message is:"+e.getMessage()); } } } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
AddSC
package sc;import java.awt.*;import javax.swing.*;import java.sql.*;import java.util.*;import javax.swing.filechooser.*;import java.io.*;import java.awt.event.*;public class AddSC extends JPanel implements ActionListener{ JTextField 课程号,学号,成绩; JButton 录入; public AddSC(){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} 课程号=new JTextField(12); 学号=new JTextField(12); 成绩=new JTextField(12); 录入=new JButton("录入"); 录入.addActionListener(this); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box3=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); box1.add(new JLabel("课号:")); box1.add(课程号); box2.add(new JLabel("学号:")); box2.add(学号); box3.add(new JLabel("成绩:")); box3.add(成绩); box4.add(录入); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(Box.createVerticalGlue()); JPanel messPanel=new JPanel(); messPanel.add(boxH); setLayout(new BorderLayout()); add(messPanel,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent c){ Object obj=c.getSource(); if(obj==录入){ if(课程号.getText().equals("")||学号.getText().equals("")){ JOptionPane.showMessageDialog(this,"填写课号与学号才能录入!" ); } else { Statement stmt=null; ResultSet rs=null,rs1=null,rsC=null,rsS=null; String sql,sql1,sqlS,sqlC; sqlC="select * from Course where Cno='"+课程号.getText()+"'"; sqlS="select * from Student where Sno='"+学号.getText()+"'"; sql1="select * from SC where Cno='"+课程号.getText()+"' and Sno='"+学号.getText()+"'"; sql="insert into SC values('"+学号.getText()+"','"+课程号.getText()+"','"+成绩.getText()+"')"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rsC=stmt.executeQuery(sqlC); if(rsC.next()){ rsS=stmt.executeQuery(sqlS); if(rsS.next()){rs1=stmt.executeQuery(sql1);if(rs1.next()){JOptionPane.showMessageDialog(this,"该学生以选该课程号,无法添加");}else{ stmt.executeUpdate(sql); JOptionPane.showMessageDialog(this,"添加成功");}rs1.close(); } else{JOptionPane.showMessageDialog(this,"该学生不存在,无法添加");} rsS.close(); } else{JOptionPane.showMessageDialog(this,"该课程不存在,无法添加");} rsC.close(); stmt.close(); } catch(SQLException e){ System.out.print("SQL Exception occur.Message is:"+e.getMessage()); } } } } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
DelC
package sc;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class DelC extends JPanel implements ActionListener{ String save=null; JTextField 课号1,课程号,课程名,先修课课程号,学分; JButton 删除,查找; public DelC(){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} 课号1=new JTextField(12); 课程号=new JTextField(12); 课程名=new JTextField(12); 先修课课程号=new JTextField(12); 学分=new JTextField(12); 删除=new JButton("删除"); 查找=new JButton("查找"); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box3=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); Box box5=Box.createHorizontalBox(); Box box6=Box.createHorizontalBox(); Box box7=Box.createHorizontalBox(); box1.add(new JLabel("课程号:",JLabel.CENTER)); box1.add(课程号); box2.add(new JLabel("课程名:",JLabel.CENTER)); box2.add(课程名); box3.add(new JLabel("先修课课程号:",JLabel.CENTER)); box3.add(先修课课程号); box4.add(new JLabel("学分:",JLabel.CENTER)); box4.add(学分); box6.add(删除); box7.add(new JLabel("课号:",JLabel.CENTER)); box7.add(课号1); box7.add(查找); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(box5); boxH.add(box6); //boxH.add(box7); boxH.add(Box.createVerticalGlue()); 删除.addActionListener(this); 查找.addActionListener(this); JPanel picPanel=new JPanel(); JPanel messPanel=new JPanel(); messPanel.add(box7); picPanel.add(boxH); setLayout(new BorderLayout()); JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割 add(splitV,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent e){ Object obj=e.getSource(); Statement stmt=null; ResultSet rs=null,rs1=null; String sql=null,sql1=null,sqlSC=null; if(obj==查找){if(课号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写查询的课号!" ); else{ sql1="select * from Course where Cno='"+课号1.getText()+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs1=stmt.executeQuery(sql1); if(rs1.next()){课程号.setText(rs1.getString("Cno").trim()); 课程名.setText(rs1.getString("Cname").trim()); 先修课课程号.setText(rs1.getString("Cpno").trim()); 学分.setText(rs1.getString("Ccredit").trim()); save=课号1.getText().trim(); } else{JOptionPane.showMessageDialog(this,"没有这个课号的课程" );} stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } else{ if(obj==删除){if(save==null)JOptionPane.showMessageDialog(this,"还没查找需要修改的课程" ); else{sql="delete from Course where Cno='"+save+"'"; sqlSC="delete from SC where Cno='"+save+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); stmt.executeUpdate(sql); stmt.executeUpdate(sqlSC); save=null; JOptionPane.showMessageDialog(this,"删除完成" ); 课程号.setText(""); 课程名.setText(""); 先修课课程号.setText(""); 学分.setText(""); stmt.close(); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } } } //连接数据库方法 public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
DelS
package sc;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class DelS extends JPanel implements ActionListener{ String save=null; JTextField 学号1,学号,姓名,性别,年龄,所在系系名; JButton 删除,查找; public DelS(){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} 学号1=new JTextField(12); 学号=new JTextField(12); 姓名=new JTextField(12); 性别=new JTextField(12); 年龄=new JTextField(12); 所在系系名=new JTextField(12); 删除=new JButton("删除"); 查找=new JButton("查找"); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box3=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); Box box5=Box.createHorizontalBox(); Box box6=Box.createHorizontalBox(); Box box7=Box.createHorizontalBox(); box1.add(new JLabel("学号:",JLabel.CENTER)); box1.add(学号); box2.add(new JLabel("姓名:",JLabel.CENTER)); box2.add(姓名); box3.add(new JLabel("性别:",JLabel.CENTER)); box3.add(性别); box4.add(new JLabel("年龄:",JLabel.CENTER)); box4.add(年龄); box5.add(new JLabel("所在系系名:",JLabel.CENTER)); box5.add(所在系系名); box6.add(删除); box7.add(new JLabel("学号:",JLabel.CENTER)); box7.add(学号1); box7.add(查找); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(box5); boxH.add(box6); boxH.add(Box.createVerticalGlue()); 删除.addActionListener(this); 查找.addActionListener(this); JPanel picPanel=new JPanel(); JPanel messPanel=new JPanel(); messPanel.add(box7); picPanel.add(boxH); setLayout(new BorderLayout()); JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割 add(splitV,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent e){ Object obj=e.getSource(); Statement stmt=null; ResultSet rs=null,rs1=null; String sql=null,sql1=null,sqlSC=null; if(obj==查找){if(学号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写查询的学号!" ); else{ sql1="select * from Student where Sno='"+学号1.getText()+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs1=stmt.executeQuery(sql1); if(rs1.next()){学号.setText(rs1.getString("Sno").trim()); 姓名.setText(rs1.getString("Sname").trim()); 性别.setText(rs1.getString("Ssex").trim()); 年龄.setText(rs1.getString("Sage").trim()); 所在系系名.setText(rs1.getString("Sdept").trim()); save=学号1.getText().trim(); } else{JOptionPane.showMessageDialog(this,"没有这个学号的学生" );} stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } else{ if(obj==删除){if(save==null)JOptionPane.showMessageDialog(this,"还没查找需要修改的学生" ); else{sql="delete from Student where Sno='"+save+"'"; sqlSC="delete from SC where Sno='"+save+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); stmt.executeUpdate(sql); stmt.executeUpdate(sqlSC); save=null; JOptionPane.showMessageDialog(this,"删除完成" ); 学号.setText(""); 姓名.setText(""); 性别.setText(""); 年龄.setText(""); 所在系系名.setText(""); stmt.close(); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } } } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
DelSC
package sc;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class DelSC extends JPanel implements ActionListener{ String saveC=null; String saveS=null; JTextField 课号1,学号1,学号,课程号,成绩; JButton 删除,查找; public DelSC(){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} 学号1=new JTextField(12); 课号1=new JTextField(12); 课程号=new JTextField(12); 学号=new JTextField(12); 成绩=new JTextField(12); 删除=new JButton("删除"); 查找=new JButton("查找"); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box3=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); Box box5=Box.createHorizontalBox(); box1.add(new JLabel("课程号:",JLabel.CENTER)); box1.add(课程号); box2.add(new JLabel("学号:",JLabel.CENTER)); box2.add(学号); box3.add(new JLabel("成绩:",JLabel.CENTER)); box3.add(成绩); box4.add(删除); box5.add(new JLabel("课号:",JLabel.CENTER)); box5.add(课号1); box5.add(new JLabel("学号:",JLabel.CENTER)); box5.add(学号1); box5.add(查找); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(Box.createVerticalGlue()); 删除.addActionListener(this); 查找.addActionListener(this); JPanel picPanel=new JPanel(); JPanel messPanel=new JPanel(); messPanel.add(box5); picPanel.add(boxH); setLayout(new BorderLayout()); JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割 add(splitV,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent e){ Object obj=e.getSource(); Statement stmt=null; ResultSet rs=null,rs1=null; String sql=null,sql1=null; if(obj==查找){if(课号1.getText().equals("")||学号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写完成查询的信息!" ); else{ sql1="select * from SC where Cno='"+课号1.getText()+"' and Sno='"+学号1.getText()+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs1=stmt.executeQuery(sql1); if(rs1.next()){课程号.setText(rs1.getString("Cno").trim()); 学号.setText(rs1.getString("Sno").trim()); 成绩.setText(rs1.getString("grade").trim()); saveC=课号1.getText().trim(); saveS=学号1.getText().trim(); } else{JOptionPane.showMessageDialog(this,"没有这个课号的学生" );} stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } else{ if(obj==删除){if(saveC==null||saveS==null)JOptionPane.showMessageDialog(this,"还没查找需要修改的学生/课程" ); else{sql="delete from SC where Cno='"+saveC+"' and Sno='"+saveS+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); stmt.executeUpdate(sql); saveC=null; saveS=null; JOptionPane.showMessageDialog(this,"删除完成" ); 课程号.setText(""); 学号.setText(""); 成绩.setText(""); stmt.close(); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } } } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
SelC
package sc;import java.sql.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;public class SelC extends JFrame { Vector rowData, columnNames; Statement stmt=null; String sql=null; JTable jt = null; JScrollPane jsp = null; PreparedStatement ps=null; ResultSet rs=null; public SelC(String sql1){ columnNames = new Vector(); // 设置列名 columnNames.add("课程号"); columnNames.add("课程名"); columnNames.add("先修课课程号"); columnNames.add("学分"); rowData=new Vector(); sql=sql1; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=stmt.executeQuery(sql); while(rs.next()){ Vector hang = new Vector(); hang.add(rs.getString("Cno"));System.out.print(rs.getString("Cno")); hang.add(rs.getString("Cname"));System.out.print(rs.getString("Cname")); hang.add(rs.getString("Cpno"));System.out.print(rs.getString("Cpno")); hang.add(rs.getString("Ccredit"));System.out.print(rs.getString("Ccredit")); rowData.add(hang);} jt=new JTable(rowData,columnNames); jsp=new JScrollPane(jt); this.add(jsp); this.setSize(400,300); this.setVisible(true); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
SelS
package sc;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class SelS extends JPanel implements ActionListener{ JTextField 学号,姓名,性别,年龄,所在系系名; JButton 查找; JTextField 课程号,课程名,先修课课程号,学分; JButton 查找1; JTextField 课号1,学号1,成绩; JButton 查找2; public SelS (){ 学号=new JTextField(12); 姓名=new JTextField(12); 性别=new JTextField(12); 年龄=new JTextField(12); 所在系系名=new JTextField(12); 课程号=new JTextField(12); 课程名=new JTextField(12); 先修课课程号=new JTextField(12); 学分=new JTextField(12); 课号1=new JTextField(12); 学号1=new JTextField(12); 成绩=new JTextField(12); 查找=new JButton("查找学生信息"); 查找1=new JButton("查找课程信息"); 查找2=new JButton("查找选课信息"); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); Box box5=Box.createHorizontalBox(); Box box6=Box.createHorizontalBox(); Box box7=Box.createHorizontalBox(); box1.add(new JLabel("学号:",JLabel.CENTER)); box1.add(学号); box1.add(new JLabel("姓名:",JLabel.CENTER)); box1.add(姓名); box1.add(new JLabel("性别:",JLabel.CENTER)); box1.add(性别); box1.add(new JLabel("年龄:",JLabel.CENTER)); box1.add(年龄); box1.add(new JLabel("所在系系名:",JLabel.CENTER)); box1.add(所在系系名); box2.add(查找); box4.add(new JLabel("课程号:",JLabel.CENTER)); box4.add(课程号); box4.add(new JLabel("课程名:",JLabel.CENTER)); box4.add(课程名); box4.add(new JLabel("先修课课程号:",JLabel.CENTER)); box4.add(先修课课程号); box4.add(new JLabel("学分:",JLabel.CENTER)); box4.add(学分); box6.add(查找1); box5.add(new JLabel("课程号:",JLabel.CENTER)); box5.add(课号1); box5.add(new JLabel("学号:",JLabel.CENTER)); box5.add(学号1); box5.add(new JLabel("成绩:",JLabel.CENTER)); box5.add(成绩); box7.add(查找2); Box boxH1=Box.createVerticalBox();//竖放box boxH1.add(box1); boxH1.add(box2); boxH1.add(Box.createVerticalGlue()); Box boxH2=Box.createVerticalBox();//竖放box boxH2.add(box4); boxH2.add(box6); boxH2.add(Box.createVerticalGlue()); Box boxH3=Box.createVerticalBox();//竖放box boxH3.add(box5); boxH3.add(box7); boxH3.add(Box.createVerticalGlue()); 查找.addActionListener(this); 查找1.addActionListener(this); 查找2.addActionListener(this); JPanel messPanel=new JPanel(); JPanel picPanel=new JPanel(); JPanel threePanel=new JPanel(); messPanel.add(boxH1); picPanel.add(boxH2); threePanel.add(boxH3); setLayout(new BorderLayout()); JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割 add(splitV,BorderLayout.CENTER); JSplitPane splitV1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,splitV,threePanel);//分割 add(splitV1,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent c){ Object obj=c.getSource(); Statement stmt=null; ResultSet rs=null; int row=0; int i=0; String sql=null; //Student K; SelC K1; SelS K2; if(obj==查找){ if(学号.getText().equals("")&&姓名.getText().equals("")&&性别.getText().equals("")&&年龄.getText().equals("")&&所在系系名.getText().equals("")){ sql="select * from Student ";System.out.print("00");//00 } else if(学号.getText().equals("")){ if(姓名.getText().equals("")){ if(性别.getText().equals("")){ if(年龄.getText().equals("")) {sql="select * from Student where Sdept like'%"+所在系系名.getText()+"%'";System.out.print("10"); } else {if(所在系系名.getText().equals("")) { sql="select * from Student where Sage like'%"+年龄.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";} } } else { if(年龄.getText().equals("")) {if(所在系系名.getText().equals("")) { sql="select * from Student where Ssex like'%"+性别.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";} } else {if(所在系系名.getText().equals("")) { sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";} } } } else{ if(性别.getText().equals("")){ if(年龄.getText().equals("")) {if(所在系系名.getText().equals("")) { sql="select * from Student where Sname like'%"+姓名.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";} } else {if(所在系系名.getText().equals("")) { sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sage like '%"+年龄.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sage like '%"+年龄.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";} } } else { if(年龄.getText().equals("")) {if(所在系系名.getText().equals("")) { sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sname like '%"+姓名.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sname like '%"+姓名.getText()+"%'";} } else {if(所在系系名.getText().equals("")) { sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sname like '%"+姓名.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sname like '%"+姓名.getText()+"%'";} } } } } else{ if(姓名.getText().equals("")){ if(性别.getText().equals("")){ if(年龄.getText().equals("")) {if(所在系系名.getText().equals("")) { sql="select * from Student where Sno like'%"+学号.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sno like'%"+学号.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'";} } else {if(所在系系名.getText().equals("")) { sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+姓名.getText()+"%'";} } } else { if(年龄.getText().equals("")) {if(所在系系名.getText().equals("")) { sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+学号.getText()+"%'";} } else {if(所在系系名.getText().equals("")) { sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+学号.getText()+"%'";} } } } else{ if(性别.getText().equals("")){ if(年龄.getText().equals("")) {if(所在系系名.getText().equals("")) { sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+学号.getText()+"%'";} } else {if(所在系系名.getText().equals("")) { sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sage like '%"+年龄.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sname like'%"+姓名.getText()+"%'and Sage like '%"+年龄.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sno like '%"+学号.getText()+"%'";} } } else { if(年龄.getText().equals("")) {if(所在系系名.getText().equals("")) { sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sname like '%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Ssex like'%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sname like '%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";} } else {if(所在系系名.getText().equals("")) { sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sname like '%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";System.out.print("10");}else {sql="select * from Student where Sage like'%"+年龄.getText()+"%'and Ssex like '%"+性别.getText()+"%'and Sdept like '%"+所在系系名.getText()+"%'and Sname like '%"+姓名.getText()+"%'and Sno like '%"+学号.getText()+"%'";} } } } } //K=new Student(sql); } else{if(obj==查找1){ if(课程号.getText().equals("")&&课程名.getText().equals("")&&先修课课程号.getText().equals("")&&学分.getText().equals("")){ sql="select * from Course ";System.out.print("00");//00 } else if(课程号.getText().equals("")){ if(课程名.getText().equals("")){ if(先修课课程号.getText().equals("")){ sql="select * from Course where Ccredit like'%"+学分.getText()+"%'";System.out.print("10"); } else { if(学分.getText().equals("")) {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'";System.out.print("10");} else {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Ccredit like '%"+学分.getText()+"%'";} } } else { if(先修课课程号.getText().equals("")&&学分.getText().equals("")){ sql="select * from Course where Cno like'%"+课程号.getText()+"%'";System.out.print("10"); } else { if(先修课课程号.getText().equals("")){sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10"); } else {if(学分.getText().equals("")) {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10");}else {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'and Ccredit like '%"+学分.getText()+"%'";} } } } } else { if(课程名.getText().equals("")){ if(先修课课程号.getText().equals("")){ if(学分.getText().equals("")) {sql="select * from Course where Cno like'%"+课程号.getText()+"%'";System.out.print("10"); } else {sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10");} } else { if(学分.getText().equals("")) {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10"); } else {sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cpno like '%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'";System.out.print("10");} } } else { if(先修课课程号.getText().equals("")){ if(学分.getText().equals("")) {sql="select * from Course where Cno like'%"+课程号.getText()+"%'and Cname like '%"+课程名.getText()+"%'";System.out.print("10"); } else {sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cno like '%"+课程号.getText()+"%'and Cname like '%"+课程名.getText()+"%'";System.out.print("10");} } else { if(学分.getText().equals("")) {sql="select * from Course where Cpno like'%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'and Cname like '%"+课程名.getText()+"%'";System.out.print("10"); } else {sql="select * from Course where Ccredit like'%"+学分.getText()+"%'and Cpno like '%"+先修课课程号.getText()+"%'and Cno like '%"+课程号.getText()+"%'and Cname like '%"+课程名.getText()+"%'";System.out.print("10");} } } } K1=new SelC(sql);} else{if(obj==查找2){ if(课号1.getText().equals("")&&学号1.getText().equals("")&&成绩.getText().equals("")){ sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("000");//000 } else{ if(课号1.getText().equals("")){ if(学号1.getText().equals("")){ sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where grade like'%"+成绩.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("001");} else{if(成绩.getText().equals("")){sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Sno like'%"+学号1.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("010");} else{sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Sno like'%"+学号1.getText()+"%'and grade like'%"+成绩.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("011");}}} else{if(学号1.getText().equals("")){ if(成绩.getText().equals("")){sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Cno like'%"+课号1.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("100");} else{sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Cno like'%"+课号1.getText()+"%' and grade like'%"+成绩.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("101");}} else{if(成绩.getText().equals("")){sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Cno like'%"+课号1.getText()+"%' and SC.Sno like'%"+学号1.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("110");} else{sql="select SC.Cno,Cname,SC.Sno,Sname,grade from SC,Course,Student where SC.Cno like'%"+课号1.getText()+"%' and SC.Sno like'%"+学号1.getText()+"%' and grade like'%"+成绩.getText()+"%' and Course.Cno=SC.Cno and Student.Sno=SC.Sno";System.out.print("111");}}} } K2=new SelS(); } } } }}
SelSC
package sc;import java.sql.*;import javax.swing.*;//import connection.java;import java.util.*;public class SelSC extends JFrame { Vector rowData, columnNames; Statement stmt=null; String sql=null; JTable jt = null; JScrollPane jsp = null; PreparedStatement ps=null; ResultSet rs=null; public SelSC(String sql1){ columnNames = new Vector(); // 设置列名 columnNames.add("课程号"); columnNames.add("学号"); columnNames.add("成绩"); rowData=new Vector(); sql=sql1; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=stmt.executeQuery(sql); while(rs.next()){ Vector hang = new Vector(); hang.add(rs.getString("Sno"));System.out.print(rs.getString("Sno")); hang.add(rs.getString("Cno"));System.out.print(rs.getString("Cno")); hang.add(rs.getString("grade"));System.out.println(rs.getString("grade")); rowData.add(hang);} jt=new JTable(rowData,columnNames); jsp=new JScrollPane(jt); this.add(jsp); this.setSize(400,300); this.setVisible(true); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
UpdateC
package sc;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class UpdateC extends JPanel implements ActionListener{ String save=null; JTextField 课号1,课程号,课程名,先修课课程号,学分; JButton 修改,查找; public UpdateC(){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} 课号1=new JTextField(12); 课程号=new JTextField(12); 课程名=new JTextField(12); 先修课课程号=new JTextField(12); 学分=new JTextField(12); 修改=new JButton("修改"); 查找=new JButton("查找"); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box3=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); Box box5=Box.createHorizontalBox(); Box box6=Box.createHorizontalBox(); Box box7=Box.createHorizontalBox(); box1.add(new JLabel("课程号:",JLabel.CENTER)); box1.add(课程号); box2.add(new JLabel("课程名:",JLabel.CENTER)); box2.add(课程名); box3.add(new JLabel("先修课课程号:",JLabel.CENTER)); box3.add(先修课课程号); box4.add(new JLabel("学分:",JLabel.CENTER)); box4.add(学分); box6.add(修改); box7.add(new JLabel("课号:",JLabel.CENTER)); box7.add(课号1); box7.add(查找); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(box5); boxH.add(box6); 修改.addActionListener(this); 查找.addActionListener(this); boxH.add(Box.createVerticalGlue()); JPanel picPanel=new JPanel(); JPanel messPanel=new JPanel(); messPanel.add(box7); picPanel.add(boxH); setLayout(new BorderLayout()); JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割 add(splitV,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent e){ Object obj=e.getSource(); Statement stmt=null; ResultSet rs=null,rs1=null; String sql=null,sql1=null,sqlSC=null; if(obj==查找){if(课号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写查询的课号!" ); else{ sql1="select * from Course where Cno='"+课号1.getText()+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs1=stmt.executeQuery(sql1); if(rs1.next()){课程号.setText(rs1.getString("Cno").trim()); 课程名.setText(rs1.getString("Cname").trim()); 先修课课程号.setText(rs1.getString("Cpno").trim()); 学分.setText(rs1.getString("Ccredit").trim()); save=课号1.getText(); } else{JOptionPane.showMessageDialog(this,"没有这个课号的课程" );} stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } else{ if(obj==修改){if(save==null){JOptionPane.showMessageDialog(this,"还没查找需要修改的课程" );} else{ if(课程号.getText().equals("")||课程名.getText().equals("")||先修课课程号.getText().equals("")||学分.getText().equals("")){ JOptionPane.showMessageDialog(this,"课程信息填满才能修改!" ); } else{sql="update Course set Cno='"+课程号.getText()+"',Cname='"+课程名.getText()+"',Cpno='"+先修课课程号.getText()+"',Ccredit='"+学分.getText()+"' where Cno='"+save+"'"; if(save.trim().equals(课程号.getText().trim())){ try{Connection dbConn1=CONN();stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);stmt.executeUpdate(sql);save=null;JOptionPane.showMessageDialog(this,"修改完成" );课程号.setText("");课程名.setText("");stmt.close(); }catch(SQLException e1){System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } else{sql1="select * from Course where Cno='"+课程号.getText()+"'"; try{Connection dbConn1=CONN();stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);rs1=stmt.executeQuery(sql1);if(rs1.next()){ JOptionPane.showMessageDialog(this,"已存在此课号课程" );}else{sqlSC="update SC set Cno='"+课程号.getText()+"' where Cno='"+save+"'"; stmt.executeUpdate(sql); stmt.executeUpdate(sqlSC); save=null; JOptionPane.showMessageDialog(null,"修改完成" ); 课程号.setText(""); 课程名.setText(""); 先修课课程号.setText(""); 学分.setText("");}stmt.close();rs1.close(); }catch(SQLException e1){System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } }}}} } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
UpdateS
package sc;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class UpdateS extends JPanel implements ActionListener{ String save=null; JTextField 学号1,学号,姓名,性别,年龄,所在系系名; JButton 修改,查找; public UpdateS(){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} 学号1=new JTextField(12); 学号=new JTextField(12); 姓名=new JTextField(12); 性别=new JTextField(12); 年龄=new JTextField(12); 所在系系名=new JTextField(12); 修改=new JButton("修改"); 查找=new JButton("查找"); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box3=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); Box box5=Box.createHorizontalBox(); Box box6=Box.createHorizontalBox(); Box box7=Box.createHorizontalBox(); box1.add(new JLabel("学号:",JLabel.CENTER)); box1.add(学号); box2.add(new JLabel("姓名:",JLabel.CENTER)); box2.add(姓名); box3.add(new JLabel("性别:",JLabel.CENTER)); box3.add(性别); box4.add(new JLabel("年龄:",JLabel.CENTER)); box4.add(年龄); box5.add(new JLabel("所在系系名:",JLabel.CENTER)); box5.add(所在系系名); box6.add(修改); box7.add(new JLabel("学号:",JLabel.CENTER)); box7.add(学号1); box7.add(查找); 修改.addActionListener(this); 查找.addActionListener(this); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(box5); boxH.add(box6); boxH.add(Box.createVerticalGlue()); JPanel picPanel=new JPanel(); JPanel messPanel=new JPanel(); messPanel.add(box7); picPanel.add(boxH); setLayout(new BorderLayout()); JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割 add(splitV,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent e){ Object obj=e.getSource(); Statement stmt=null; ResultSet rs=null,rs1=null; String sql=null,sql1=null,sqlSC; if(obj==查找){if(学号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写查询的学号!" ); else{ sql1="select * from Student where Sno='"+学号1.getText()+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs1=stmt.executeQuery(sql1); if(rs1.next()){学号.setText(rs1.getString("Sno").trim()); 姓名.setText(rs1.getString("Sname").trim()); 性别.setText(rs1.getString("Ssex").trim()); 年龄.setText(rs1.getString("Sage").trim()); 所在系系名.setText(rs1.getString("Sdept").trim()); save=学号1.getText(); } else{JOptionPane.showMessageDialog(this,"没有这个学号的学生" );} stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } else{ if(obj==修改){if(save==null){JOptionPane.showMessageDialog(this,"还没查找需要修改的学生" );} else{ if(学号.getText().equals("")||姓名.getText().equals("")||性别.getText().equals("")||年龄.getText().equals("")||所在系系名.getText().equals("")){ JOptionPane.showMessageDialog(this,"学生信息填满才能修改!" ); } else{sql="update Student set Sno='"+学号.getText()+"',Sname='"+姓名.getText()+"',Ssex='"+性别.getText()+"',Sage='"+年龄.getText()+"',Sdept='"+所在系系名.getText()+"'where Sno='"+save+"'"; if(save.trim().equals(学号.getText().trim())){ try{Connection dbConn1=CONN();stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);stmt.executeUpdate(sql);save=null;JOptionPane.showMessageDialog(this,"修改完成" );学号.setText("");姓名.setText("");性别.setText("");年龄.setText("");所在系系名.setText("");stmt.close(); }catch(SQLException e1){System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } else{sql1="select * from Student where Sno='"+学号.getText()+"'"; try{Connection dbConn1=CONN();stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);rs1=stmt.executeQuery(sql1);if(rs1.next()){ JOptionPane.showMessageDialog(this,"已存在此学号学生" );}else{ sqlSC="update SC set Sno='"+学号.getText()+"' where Sno='"+save+"'"; stmt.executeUpdate(sql); stmt.executeUpdate(sqlSC); save=null; JOptionPane.showMessageDialog(null,"修改完成" ); 学号.setText(""); 姓名.setText(""); 性别.setText(""); 年龄.setText(""); 所在系系名.setText("");}stmt.close();rs1.close(); }catch(SQLException e1){System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } }}}} } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
UpdateSC
package sc;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class UpdateSC extends JPanel implements ActionListener{ String saveC=null; String saveS=null; JTextField 课号1,学号1,学号,课程号,成绩; JButton 修改,查找; public UpdateSC(){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} 学号1=new JTextField(12); 课号1=new JTextField(12); 课程号=new JTextField(12); 学号=new JTextField(12); 成绩=new JTextField(12); 修改=new JButton("修改"); 查找=new JButton("查找"); Box box1=Box.createHorizontalBox();//横放box Box box2=Box.createHorizontalBox(); Box box3=Box.createHorizontalBox(); Box box4=Box.createHorizontalBox(); Box box5=Box.createHorizontalBox(); box1.add(new JLabel("课程号:",JLabel.CENTER)); box1.add(课程号); box2.add(new JLabel("学号:",JLabel.CENTER)); box2.add(学号); box3.add(new JLabel("成绩:",JLabel.CENTER)); box3.add(成绩); box4.add(修改); box5.add(new JLabel("课号:",JLabel.CENTER)); box5.add(课号1); box5.add(new JLabel("学号:",JLabel.CENTER)); box5.add(学号1); box5.add(查找); Box boxH=Box.createVerticalBox();//竖放box boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(Box.createVerticalGlue()); 修改.addActionListener(this); 查找.addActionListener(this); JPanel picPanel=new JPanel(); JPanel messPanel=new JPanel(); messPanel.add(box5); picPanel.add(boxH); setLayout(new BorderLayout()); JSplitPane splitV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割 add(splitV,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent e){ Object obj=e.getSource(); Statement stmt=null; ResultSet rs=null,rs1=null,rsC=null,rsS=null; String sql,sql1,sqlS,sqlC; if(obj==查找){if(课号1.getText().equals("")||学号1.getText().equals(""))JOptionPane.showMessageDialog(this,"请填写完成查询的信息!" ); else{ sql1="select * from SC where Cno='"+课号1.getText()+"' and Sno='"+学号1.getText()+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs1=stmt.executeQuery(sql1); if(rs1.next()){课程号.setText(rs1.getString("Cno").trim()); 学号.setText(rs1.getString("Sno").trim()); 成绩.setText(rs1.getString("grade").trim()); saveC=课号1.getText().trim(); saveS=学号1.getText().trim(); } else{JOptionPane.showMessageDialog(this,"没有这个课号的学生" );} stmt.close(); rs1.close(); }catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } else{ if(obj==修改){if(saveC==null||saveS==null)JOptionPane.showMessageDialog(this,"还没查找需要修改的学生/课程" ); else{ if(课程号.getText().equals("")||学号.getText().equals("")){ JOptionPane.showMessageDialog(this,"课程信息填满才能修改!" ); } else{ sqlC="select * from Course where Cno='"+课程号.getText()+"'"; sqlS="select * from Student where Sno='"+学号.getText()+"'"; sql1="select * from SC where Cno='"+课程号.getText()+"' and Sno='"+学号.getText()+"'"; sql="update SC set Cno='"+课程号.getText()+"',Sno='"+学号.getText()+"',grade='"+成绩.getText()+"' where Cno='"+saveC+"' and Sno='"+saveS+"'"; try{ Connection dbConn1=CONN(); stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rsC=stmt.executeQuery(sqlC); if(rsC.next()){rsS=stmt.executeQuery(sqlS);if(rsS.next()){ if(课程号.getText().trim().equals(saveC)&& 学号.getText().trim().equals(saveS)){ stmt.executeUpdate(sql); JOptionPane.showMessageDialog(this,"添加成功"); saveC=null; saveS=null; } else{rs1=stmt.executeQuery(sql1); if(rs1.next()){JOptionPane.showMessageDialog(this,"学生与课程号以存在,无法修改");} else{ stmt.executeUpdate(sql); JOptionPane.showMessageDialog(this,"添加成功"); saveC=null; saveS=null; } rs1.close(); }}else{JOptionPane.showMessageDialog(this,"该学生不存在,无法修改");}rsS.close(); } else{JOptionPane.showMessageDialog(this,"该课程不存在,无法修改");} rsC.close(); stmt.close(); } catch(SQLException e1){ System.out.print("SQL Exception occur.Message is:"+e1.getMessage()); } } } } } } public static Connection CONN(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://主机名:1433; DatabaseName=数据库名"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "密码"; //密码 Connection dbConn=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } return dbConn; }}
Menu
package sc;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Menu extends JFrame implements ActionListener{ AddS 增加学生界面; UpdateS 修改学生界面; DelS 删除学生界面; AddC 增加课程界面; DelC 删除课程界面; UpdateC 修改课程界面; AddSC 增加选课界面; DelSC 删除选课界面; UpdateSC 修改选课界面; SelS 学生查询界面; JPanel pCenter; CardLayout card=null; JLabel label=null; JMenuBar mb=new JMenuBar();//菜单栏 JMenu m1=new JMenu("学生管理"); JMenuItem add1=new JMenuItem("add "); JMenuItem updata1=new JMenuItem("update "); JMenuItem delete1=new JMenuItem("delete "); JMenu m2=new JMenu("课程管理"); JMenuItem add2=new JMenuItem("add "); JMenuItem updata2=new JMenuItem("update "); JMenuItem delete2=new JMenuItem("delete "); JMenu m3=new JMenu("选课管理"); JMenuItem add3=new JMenuItem("add "); JMenuItem updata3=new JMenuItem("update "); JMenuItem delete3=new JMenuItem("delete "); JMenu m4=new JMenu("查询管理"); JMenuItem 学生查询=new JMenuItem("查询信息 "); JMenuItem m5=new JMenuItem("系统退出"); Font t=new Font ("sanerif",Font.PLAIN,12); public Menu (){ this.setTitle("学生成绩管理系统"); try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){System.err.println("不能设置外观: "+e);} //组合菜单 addMenu1(); addMenu2(); addMenu3(); addMenu4(); addJMenuBar(); setJMenuBar(mb); label=new JLabel("BJTU成绩管理系统",JLabel.CENTER); label.setFont(new Font("宋体",Font.BOLD,36)); label.setHorizontalTextPosition(SwingConstants.CENTER); label.setForeground(Color.blue); //点击事件 add1.addActionListener(this); updata1.addActionListener(this); delete1.addActionListener(this); m5.addActionListener(this); add2.addActionListener(this); delete2.addActionListener(this); updata2.addActionListener(this); add3.addActionListener(this); delete3.addActionListener(this); updata3.addActionListener(this); 学生查询.addActionListener(this); card=new CardLayout(); pCenter=new JPanel(); pCenter.setLayout(card); 增加学生界面=new AddS(); 修改学生界面=new UpdateS(); 删除学生界面=new DelS(); 增加课程界面=new AddC(); 删除课程界面=new DelC(); 修改课程界面=new UpdateC(); 增加选课界面=new AddSC(); 删除选课界面=new DelSC(); 修改选课界面=new UpdateSC(); 学生查询界面=new SelS(); pCenter.add("欢迎界面",label); pCenter.add("增加学生界面",增加学生界面); pCenter.add("修改学生界面",修改学生界面); pCenter.add("删除学生界面",删除学生界面); pCenter.add("增加课程界面",增加课程界面); pCenter.add("删除课程界面",删除课程界面); pCenter.add("修改课程界面",修改课程界面); pCenter.add("增加选课界面",增加选课界面); pCenter.add("删除选课界面",删除选课界面); pCenter.add("修改选课界面",修改选课界面); pCenter.add("学生查询界面", 学生查询界面); add(pCenter,BorderLayout.CENTER); validate(); setVisible(true); setBounds(400,150,1200,790); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addWindowListener(new WindowAdapter(){//关闭程序时的操作 public void windowClosing(WindowEvent e){System.exit(0);} }); validate(); } private void addJMenuBar() { mb.add(m1);mb.add(m2);mb.add(m3);mb.add(m4);mb.add(m5); } private void addMenu4() { m4.add(学生查询); m4.setFont(t); } private void addMenu3() { m3.add(add3); m3.add(updata3); m3.add(delete3); m3.setFont(t); } private void addMenu2() {//将菜单加入到菜单栏中 m2.add(add2); m2.add(updata2); m2.add(delete2); m2.setFont(t); } private void addMenu1() { m1.add(add1); m1.add(updata1); m1.add(delete1); m1.setFont(t);//字体 } public void actionPerformed(ActionEvent e){ Object obj=e.getSource(); if(obj==m5){System.exit(0);} else{if(obj==add1){ card.show(pCenter,"增加学生界面"); } else{if(obj==updata1){ card.show(pCenter,"修改学生界面"); } else{if(obj==delete1){ card.show(pCenter, "删除学生界面"); } else{if(obj==add2){ card.show(pCenter, "增加课程界面"); } else{if(obj==delete2){ card.show(pCenter, "删除课程界面"); } else{if(obj==updata2){ card.show(pCenter, "修改课程界面"); } else{if(obj==add3){ card.show(pCenter, "增加选课界面"); } else{if(obj==delete3){ card.show(pCenter, "删除选课界面"); } else{if(obj==updata3){ card.show(pCenter, "修改选课界面"); } else{if(obj==学生查询){ card.show(pCenter, "学生查询界面"); }} }}}}}}}}}} public static void main(String[] args) { new Menu(); }}
idea链接数据库
File+settings+editor+plugins+database navigator+apply+ok
出现感叹号:
进connection+properties+serverTimezone+Asia/Shanghai+test connection
简单SQL语句
按老师要求还是进行一点语句补充吧(如果觉得费钱就把重复的代码保留一个表的,其他的做文字说明就可以了)
建表:
CREATE TABLE student(sno CHAR(10),sname VARCHAR(20),ssex CHAR(2),sage INT,sdept CHAR(15) );
增:
insert into 表名(属性1,属性2)value ('内容1','内容2')
删:
delete from 表名 where 学号='****'
改:
update 表名 set 成绩=NULL
查:
select student.sno,sname,ssex,sage,sdept,cnofrom 学生成绩管理.student,学生成绩管理.scwhere 学生成绩管理.student.sno=学生成绩管理.sc.sno
对成绩的查询:
select student.sname ,gradefrom 学生成绩管理.student,学生成绩管理.scwhere 学生成绩管理.student.sno=学生成绩管理.sc.sno
//不优秀的学生学号、课程号、成绩select sno,cno,gradefrom 学生成绩管理.scwhere grade<80;//不优秀课程的学生学号select distinct snofrom 学生成绩管理.scwhere grade<80;//所有课程信息及选修该课程的学生学号及成绩select cname,sno,gradefrom 学生成绩管理.sc,学生成绩管理.coursewhere 学生成绩管理.course.cno =学生成绩管理.sc.cno//每个学生及选修课程名及成绩select student.sno,sname,cname,sc.gradefrom 学生成绩管理.student,学生成绩管理.sc,学生成绩管理.coursewhere student.sno=sc.snoand sc.cno=course.cno
对成绩的统计:
1、使用MAX ()函数统计某个字段最大数据;
2、使用MIN ()函数统计某个字段最小数据;
3、使用COUNT ()函数统计查询结果的行数;
4、使用SUM ()函数统计数据之和;
5、使用AVG ()函数统计平均数。 MySQL MAX () 函数
答辩
好了好了就到这里,答辩讲解需求分析和E-R图为主,明天答辩祝我自己好运,过过过
来源地址:https://blog.csdn.net/weixin_57780589/article/details/124687211