数据库大作业实验报告

一、实验目的

通过这个实验回顾数据库这门课程的基本内容,深化自己的理解,掌握高级语言对数据库操作的方法。

二、总体功能设计与ER图

这次的实验是使用Java+ SQL server 完成的。
2.1 总体的功能设计

在这里插入图片描述
2.2 ER 图
在这里插入图片描述
2.3 流程图
在这里插入图片描述
2.4 基本表
(1)SC 表记录学生的选课信息
在这里插入图片描述
(2) Course记录课程信息
在这里插入图片描述
(3)Student记录学生的基本信息
在这里插入图片描述
(4)账户密码记录用户名以及密码
在这里插入图片描述
(5)登录信息记录用户登录的时间
在这里插入图片描述

三、T-sql 代码

(1)

create table Student(
Sno char(9) primary key,
Sname char(9),
Sage smallint,
)
---创建Student表
create table Course(
Cno char(9) primary key,
Cname char(9) ,
Grade smallint  
)
--创建Course表
create table SC (
Sno char(9),
Cno char(9),
Grade smallint,
primary key (Sno,Cno)
)
alter table SC add constraint from_stu_sc foreign key (Sno) references Student (Sno) on delete cascade
alter table Sc add constraint from_course_sc foreign key(Cno) references Course(Cno) on delete cascade
--创建SC并且实现级联删除

(2)
在sql server 中创建存储过程


if exists(select * from sys.objects where name='pro_tran')
 drop procedure pro_tran
  create procedure pro_tran 
  @Sno char(9)
  as 

  begin 
select SC.Sno, Course.Cno ,Course.Cname
from   SC ,Course
where SC.Sno=@Sno and SC.Cno=Course.Cno
 end

在学生模块中会用到
(3)
创建触发器,当管理员教师insert或者是update课程的学分时候,如果大于4会自动修改为4。

create trigger Check_Course_grade
 on Course
  for insert ,update
  as
  declare @num smallint,
     @cno char(9),
	 @cname char(9)
  select @num= Grade from inserted
  select @cno=Cno from inserted
  select @cname= Cname from inserted
 begin
  if(@num>4)
  if(exists( select * from Course where Cno=@cno))
  update Course set Grade =4
  if not exists(select * from Course where Cno=@cno)
  insert into Course values(@cno,@cname,@num)
  end 

(4)创建登录密码表

create table (
account char(9) primary key,
password char(40) ,
image image,
)

(5)创建登录信息表

create table login_account(
account char(9),
login_time datetime,
)

四、 Java代码

1、准备工作
(1) 因为将会使用Jtable组件,并且是不能编辑的,因此需要重写DefaultTableModel 这个类为MyDefaultTable。

(2)Md5 加密算法:作为一个类,因为不只有一个模块用到这个类

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
public class Md5  {
    
    
    public String getMd5_String(String a){
    
    
    //重点就是这个方法,返回加密后的密码
        String str = null;
        try{
    
    
            MessageDigest md=MessageDigest.getInstance("md5");
            byte[] bytes = md.digest(a.getBytes("UTF-8"));
            //得到了一个字节 计算摘要
            // a-z A-Z 0-9 / * 表示生成的string
             str= Base64.getEncoder().encodeToString(bytes);

        }catch (NoSuchAlgorithmException e){
    
    
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e){
    
    
            e.printStackTrace();
            System.out.println("编码不支持");
        }
        return str;
    }
}

(3)验证码组件 因为都是鼠标点击可以更换验证码,需要注册一下监听器。


public class validCode  extends JComponent implements MouseListener {
    
    
 private int width,height =40;
   private String code;
   private int codeLength = 4;
   private Random random = new Random();
   public validCode() {
    
    
      this.width = this.codeLength * 16 + (this.codeLength - 1) * 10;
      this.setPreferredSize(new Dimension(width, height));
      this.setSize(width, height);
      this.addMouseListener(this);
      //提示信息
      setToolTipText("看不清楚,换一个!");
   }
   public int getCodeLength() {
    
    
      return this.codeLength;
   }
   /*
   设置验证码文字的长度
   */
   public void setCodeLength(int codeLength) {
    
    
      if(codeLength < 4) {
    
    
         this.codeLength = 4;
      } else {
    
    
         this.codeLength = codeLength;
      }
   }
   public String getCode() {
    
    
      return code;
   }
   /*
       产生随机的颜色
   */
   public Color getRandColor(int min, int max) {
    
    
      if (min > 255)
         min = 255;
      if (max > 255)
         max = 255;
      int red = random.nextInt(max - min) + min;
      int green = random.nextInt(max - min) + min;
      int blue = random.nextInt(max - min) + min;
      return new Color(red, green, blue);
   }
   /*
       设置验证码具体的字母是什么
   */
   protected String generateCode() {
    
    
      char[] codes = new char[this.codeLength];
      for (int i = 0, len = codes.length; i < len; i++) {
    
    
         if (random.nextBoolean()) {
    
    
            codes[i] = (char) (random.nextInt(26) + 65);
         } else {
    
    
            codes[i] = (char) (random.nextInt(26) + 97);
         }
      }
      this.code = new String(codes);
      return this.code;
   }
   //核心部分 
   @Override
   protected void paintComponent(Graphics g) {
    
    
      super.paintComponent(g);
      if(this.code == null || this.code.length() != this.codeLength) {
    
    
         this.code = generateCode();
      }
      this.width = this.codeLength * 16 + (this.codeLength - 1) * 10;
      super.setSize(width, height);
      super.setPreferredSize(new Dimension(width, height));
      Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
      g.setFont(mFont);
      //绘制出验证码的背景的矩形轮廓
      Graphics2D g2d = (Graphics2D) g;
      g2d.setColor(getRandColor(200, 250));
      g2d.fillRect(0, 0, width, height);
      g2d.setColor(getRandColor(180, 200));
      g2d.drawRect(0, 0, width - 1, height - 1);
      //绘制出验证码背景的线
      int i = 0, len = 150;
      for (; i < len; i++) {
    
    
         int x = random.nextInt(width - 1);
         int y = random.nextInt(height - 1);
         int x1 = random.nextInt(width - 10) + 10;
         int y1 = random.nextInt(height - 4) + 4;
         g2d.setColor(getRandColor(180, 200));
         g2d.drawLine(x, y, x1, y1);
      }
      //绘制出验证码的具体字母
      i = 0; len = this.codeLength;
      FontMetrics fm = g2d.getFontMetrics();
      int base = (height - fm.getHeight())/2 + fm.getAscent();
      for(;i<len;i++) {
    
    
         int b = random.nextBoolean() ? 1 : -1;
         g2d.rotate(random.nextInt(10)*0.01*b);
         g2d.setColor(getRandColor(20, 130));
         g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);
      }
   }
   public void nextCode(){
    
    
      this.generateCode();
      repaint();
   }
    @Override
    public void mouseClicked(MouseEvent mouseEvent) {
    
    
      this.nextCode();
    }
    @Override
    public void mousePressed(MouseEvent mouseEvent) {
    
    
    }
    @Override
    public void mouseReleased(MouseEvent mouseEvent) {
    
    
    }
    @Override
    public void mouseEntered(MouseEvent mouseEvent) {
    
    
    }
    @Override
    public void mouseExited(MouseEvent mouseEvent) {
    
    
    }
}
验证码组件是利用random产生随机数,进而产生验证码

(4)自定义Test类 连接数据库,内置基本的方法,例如增删改查基本操作,其他的类直接调用函数即可。

public class Test {
    
    
 
    public CallableStatement callableStatement=null;
   public Connection ct=null;
    public PreparedStatement ps=null;
     public ResultSet rs=null;
 public Vector<String> con,sno,Cname;
// 只是在Student页面用到的
public  Test(){
    
    
   //释放的时候容易释放
    this.con=new Vector<String>(); this.sno= new Vector<>(); this.Cname=new Vector<>();
}
//select 函数 这个函数的参数是tag ,目标表
public void select (String tag){
    
    
    this.get_sql_connection();
    try{
    
    
        this.vector_free();
        this.ps=this.ct.prepareStatement("select * from "+tag);
        this.rs=this.ps.executeQuery();
        
        while(this.rs.next()) {
    
    
            this.con.add(rs.getString(1));
            this.sno.add(rs.getString(2));
            this.Cname.add(rs.getString(3));
        }
        System.out.println("数据成功加入");
    }
    catch (Exception e){
    
    
        e.printStackTrace();
    }
    finally {
    
    
        this.close();
    }
}
//释放 vector 的资源
public  void vector_free(){
    
       // 
   this.con.clear();
   this.Cname.clear();this.sno.clear();
}
//连接数据库
public void get_sql_connection(){
    
    
    String  url="jdbc:sqlserver://localhost;
    databaseName=数据库名;user=用户名; password=密码";
    try {
    
    
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        this.ct=DriverManager.getConnection(url);
        System.out.println("数据库连接成功");
    }
    catch(Exception e){
    
    
        e.printStackTrace();
    }
}
// 学生插入 SC 表信息 
    public void insert_SC_by_Stu (String a, String b) throws SQLException {
    
    
            this.get_sql_connection();
            this.ps=this.ct.prepareStatement("use [Stu1] insert into  SC (Sno,Cno,Grade) values(?,?,?)");
            this.ps.setString(1,a); this.ps.setString(2,b); this.ps.setString(3,null);
            int i=ps.executeUpdate();
            System.out.println("数据成功加入"+i+"行");
            this.close();
    }
//教师插入SC
public void insert_SC_by_tea(String a,String b,String c) throws SQLException {
    
    
        this.get_sql_connection();
        this.ps=this.ct.prepareStatement("use [Stu1] insert into SC (Sno,Cno,Grade) values(?,?,?)");
        Integer num=Integer.parseInt(c);
        this.ps.setString(1,a); this.ps.setString(2,b); this.ps.setInt(3,num);
        int i=ps.executeUpdate();
        System.out.println("数据成功加入"+i+"行");
        this.close();
}
//教师插入,Course
public void insert_by_tea(String a,String b,String c,String tag) throws SQLException {
    
    
    this.get_sql_connection();
        this.ps=this.ct.prepareStatement("use [Stu1] insert into "+tag+" values(?,?,?) ");
        Integer num=Integer.parseInt(c);
        this.ps.setString(1,a); this.ps.setString(2,b); this.ps.setInt(3,num);
        int i=ps.executeUpdate();
        System.out.println("数据成功加入"+i+"行");
        this.close();
}
//删除 Student
public  int delete ( String a) throws SQLException {
    
    
    //这个是按照主码删除元组
    this.get_sql_connection();
        String sql ="delete from Student where Sno=?";
        this.ps=this.ct.prepareStatement(sql);
        this.ps.setString(1,a);
       int i= ps.executeUpdate();
       System.out.println(i+"行 修改");
        this.close();
        return i;
}//删除SC
    public  int  delete_SC ( String a,String b) throws SQLException {
    
    
        //这个是按照主码删除元组
        this.get_sql_connection();
            String sql ="delete from SC  where Sno=? and Cno=?";
            this.ps=this.ct.prepareStatement(sql);
            this.ps.setString(1,a); this.ps.setString(2,b);
            int i= ps.executeUpdate();
            System.out.println(i+"行 修改");
            this.close();
          return i;
}
//删除Course时候需要参数较多
    public  int  delete_Course( String a) throws Exception {
    
    
        //这个是按照主码删除元组
            this.get_sql_connection();
            String sql ="delete from Course where Cno=?" ;
            this.ps=this.ct.prepareStatement(sql);
            this.ps.setString(1,a);
            int i= ps.executeUpdate();
            System.out.println(i+"行 修改");
            this.close();
   return i;
}
//创建表
public  void create_table() throws Exception {
    
    
    this.get_sql_connection();
  String sql= "create table Stu_rt ( Sno char(9) not null, Cno char(4) not null , Grade smallint , primary key (Sno,Cno) )";

     this.ps=this.ct.prepareStatement(sql);
     this.ps.executeUpdate();
     System.out.println("ok!!");
  System.out.println("sorry !! data is error!!");
     this.close();
}
//教师更新 Student
  public int update_sc_by_tea (String a,String b,String c) throws  Exception{
    
    
     int i;
    this.get_sql_connection();
        int  num=Integer.parseInt(c);
        String sql ="update  SC set Grade= ?  where Sno =? and Cno=?" ;
        this.ps=this.ct.prepareStatement(sql);
        this.ps.setString(3,b);
        this.ps.setInt(1,num);
        this.ps.setString(2,a);
         i=this.ps.executeUpdate();
     System.out.println(i+"行 update");
        this.close();
 return i;
}
//  更新Student
public int  update_Student_tea(String a,String b,String c) throws Exception {
    
    
     int i;
    this.get_sql_connection();
        int  num=Integer.parseInt(c);
        String sql ="update  Student set  Sname=? ,Sage =? where Sno =? " ;
        this.ps=this.ct.prepareStatement(sql);
        this.ps.setString(3,a);
        this.ps.setInt(2,num);
        this.ps.setString(1,b);
         i=this.ps.executeUpdate();
        System.out.println(i+"行 update");
        this.close();
return i;
}
// 更新 Course
    public int  update_Course_tea(String a,String b,String c) throws Exception {
    
    
    int i;
    this.get_sql_connection();
       int num=Integer.parseInt(c);
        String sql= "update Course set Cname=?, Grade=? where Cno=?";
       this.ps=this.ct.prepareStatement(sql);
       this.ps.setString(3,a);
       this.ps.setInt(2,num);
       this.ps.setString(1,b);
        i=this.ps.executeUpdate();
       System.out.println(i+"行 update");
       this.close();
return  i;
}
// 存储过程
public void call_produce(String sql) throws SQLException {
    
    
    String Sno,Cname,Sage;
    this.get_sql_connection();
        //尝试做
        this.callableStatement=  this.ct.prepareCall("{call pro_tran1}");
         this. callableStatement.registerOutParameter(1, Types.SMALLINT);
        this.callableStatement.setString(2,"1");
         this.callableStatement.execute();
        System.out.println("是最大值"+this.callableStatement.getInt(1));
        this.close();
}
//最后的方法一定要运行 ,释放
public void close(){
    
    
    try {
    
    
        if(this.rs != null) this.rs.close();
        if(this.ps != null)this.ps.close();
        if(this.ct != null)this.ct.close();
        if(this.callableStatement!=null) this.callableStatement.close();
        System.out.println("数据库已经关闭");
    }catch(Exception e) {
    
    
    }
}
}

Test 是整个程序的核心内容,而Test的最重要的部分可以由以下代码代替:

 this.get_sql_connection();
 this.ps=this.ct.prepareStatement(sql);
sql 指的是T-sql 语句


该类的大多数方法是抛出异常而不是直接处理异常,因为Test将作为属性放在别的类中,在保证数据库连接正确的情况下,如果出现错误,那么一定是参数的错误,根据不同类的不同特性,可以做出相应的处理异常动作,比如说“改”这个功能,可能这个类中需要一个JOptionPane的提示,而别的类不需要。
在update ,delete 功能的函数中是有返回值的 i=this.ps.executeUpdate(); return i; 这个返回值是检验被执行动作的元组是否在基本表中存在的依据。 不存在就是0 ,存在是1 。

(5)将表格组件设置为透明 ,这里给出一个实例

  • 别的组件都较为简单,表格是最为麻烦的。
  • 这个实例也是设置背景图片的例子

public class Touming  extends JFrame {
    
    
    private JTable jTable;
    private JScrollPane jScrollPane;
    private MyDefaultTable table ;
    private  ImageIcon img;
    private  JLabel jl1;
    private DefaultTableCellRenderer renderer;
    public Touming(){
    
    
        super("TEST");
        this.renderer= new DefaultTableCellRenderer();
        this.renderer.setOpaque(false);
        this.img= new ImageIcon(getClass().getResource("/photo/13.jpg"));//导入图片
        this.jl1= new JLabel(this.img);
        this.jl1.setBounds(0,0,1000,700);
        String []str={
    
    "a","b","c"};
        this.table= new MyDefaultTable(str,22);
        this.jTable= new JTable(this.table);
        this.jScrollPane= new JScrollPane(this.jTable);
        //添加组件
        this.getLayeredPane().add(this.jl1, new Integer(Integer.MIN_VALUE));
        ((JPanel)this.getContentPane()).setOpaque(false);
        for(int i=0;i<=2;i++){
    
    
            this.jTable.getColumn(str[i]).setCellRenderer(this.renderer);
        }
        this.jScrollPane.setOpaque(false);
        this.jTable.setOpaque(false);
        this.jScrollPane.getViewport().setOpaque(false);
        this.getContentPane().add(this.jScrollPane);
        this.setSize(1000,700);
        this.setDefaultCloseOperation(Windows.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    public static void main(String[] args) {
    
    
        new Touming();
    }
}

2、登录页面
(1)这个页面较为简单,由按钮,提示框,输入框组成,在JTextField中得到account 以及密码 ,首先是由正则表达式根据用户名的长度判断是教师还是学生,教师为6位,学生为4位,判断之后与数据库中的account_password 表进行比较,正确的话可以分别进入不同的页面。main方法在这个类中。
(2)具体代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.*;

public class Login_in  extends JFrame implements ActionListener {
    
    
    //组件
    public Test test;
    private Md5 md5;
    public JLabel account;
    private  JPanel jPanel;
    public TableText tableText;
    public JLabel password;
    public JButton button_login_in;
    public JTextField jt_account;
    public JPasswordField jt_password;
    private JButton jb_register; private  Register register;
    public JLabel jl1;
    public ImageIcon img;
    protected Windows windows = null;
    public Login_in() {
    
    
        super("选课管理模拟系统");
        this.test = new Test();
        //Test 类是连接数据库的类,由一些基本增删改查方法。
        this.md5 = new Md5();
        // md5 是自己根据MD5算法写的加密密码的类
        this.account = new JLabel("输入用户名"); this.account.setOpaque(false);
        this.password = new JLabel("输入密码"); this.password.setOpaque(false);
        this.button_login_in = new JButton("登录"); this.button_login_in.setOpaque(false);
        this.jb_register=new JButton("注册"); this.jb_register.setOpaque(false);
        //设置背景图片
        this.img= new ImageIcon(getClass().getResource("/photo/6.png"));//导入图片
        this.jl1= new JLabel(this.img);
        this.jl1.setBounds(0,0,this.img.getIconWidth(),this.img.getIconHeight());
         this.jPanel= new JPanel();
         this.jPanel.setOpaque(false);
        this.button_login_in.addActionListener(this);
        this.jt_account = new JTextField(10); this.jt_account.setOpaque(false);
        this.jt_password = new JPasswordField(10); this.jt_password.setOpaque(false);
        //设置页面布局
        this.setSize(this.img.getIconWidth(), this.img.getIconHeight());
        this.getLayeredPane().add(this.jl1, new Integer(Integer.MIN_VALUE));
        //添加背景图片
        ((JPanel)this.getContentPane()).setOpaque(false);
        this.getContentPane().setLayout(null);
        //添加组件
        this.account.setBounds(this.img.getIconHeight()/4,this.img.getIconWidth()/3-80,100,30);
        this.jt_account.setBounds(this.img.getIconHeight()/4+110,this.img.getIconWidth()/3-80,150,30);
        this.password.setBounds(this.img.getIconHeight()/4,this.img.getIconWidth()/3-30,100,30);
        this.jt_password.setBounds(this.img.getIconHeight()/4+110,this.img.getIconWidth()/3-30,150,30);
        this.button_login_in.setBounds(this.img.getIconHeight()/4+20,this.img.getIconWidth()/3+30,80,35);
        this.jb_register.setBounds(this.img.getIconHeight()/4+160,this.img.getIconWidth()/3+30,80,35);
        this.jb_register.setContentAreaFilled(false); this.button_login_in.setContentAreaFilled(false);
        this.jb_register.addActionListener(this);
        this.setLocationRelativeTo(null);
        this.getContentPane().add(this.account);this.getContentPane().add(this.jt_account);this.getContentPane().add(this.password);
        this.getContentPane().add(this.jt_password); this.getContentPane().add(this.button_login_in);this.getContentPane().add(this.jb_register);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setResizable(false);//不能改变大小
        this.setVisible(true);
    }
    //向数据库中login_account 登录时间信息
    public void Login_time(){
    
    
        Date date = new Date();
        SimpleDateFormat dateformat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.test.get_sql_connection();
        //连接数据库
        try{
    
    
        
            String sql ="insert into login_account values(?,?)";
            this.test.ps=this.test.ct.prepareStatement(sql);
            this.test.ps.setString(1,this.jt_account.getText());
            this.test.ps.setString(2,dateformat.format(date));
            this.test.ps.executeUpdate();
        }catch (Exception e){
    
    
            e.printStackTrace();
        }finally {
    
    
            this.test.close();
            //关闭连接
        }
    }
    public static void main(String[] args) {
    
    
        new Login_in();
    }
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
    
    
        //如果是登录
            String a;
            boolean flag = false;
            String b;
            String result = null;
            String str = null;
            if (actionEvent.getSource().equals(this.button_login_in)) {
    
    
                a = this.jt_account.getText();
                b = new String(this.jt_password.getPassword());
               //正则表达式
                Pattern p_account = Pattern.compile("^[A-Za-z0-9]{6}$");
                //
                Pattern p_account_student = Pattern.compile("^[A-Za-z0-9]{4}$");
                Matcher m_account = p_account.matcher(a);
                Matcher m_accout_student = p_account_student.matcher(a);
                if (m_account.find()) {
    
     //教师匹配的话
                    try {
    
    
                    //将输入的密码与account_password中的密码比较
              
                        this.test.get_sql_connection();
                        this.test.ps = this.test.ct.prepareStatement("select  password  from account_password  where account=? and password=?");
                        this.test.ps.setString(1, a);
                        this.test.ps.setString(2, this.md5.getMd5_String(b));
                        this.test.rs = this.test.ps.executeQuery();
                        flag = this.test.rs.next();
                    } catch (Exception e) {
    
    
                    } finally {
    
    
                        this.test.close();
                    }
                    if (flag == true) {
    
    
                        this.Login_time();
                        if (this.windows == null) {
    
    
                            this.windows = new Windows(this, this.test);
                            //传引用
                        }
                        this.windows.setVisible(true);
                        this.setVisible(false);
                        System.out.println("ok is login ");
                    } else {
    
    
                        JOptionPane.showMessageDialog(this, "密码或者用户名不正确");
                    }
                }  //教师匹配的话
                
                //学生匹配的话
                else if (m_accout_student.find()) {
    
    
                    try {
    
    
                        this.test.get_sql_connection();
                        this.test.ps = this.test.ct.prepareStatement("select  password  from account_password  where account=? and password=?");
                        this.test.ps.setString(1, a);
                        this.test.ps.setString(2, this.md5.getMd5_String(b));
                        this.test.rs = this.test.ps.executeQuery();
                        flag = this.test.rs.next();
                    } catch (Exception e) {
    
    
                    } finally {
    
    
                        this.test.close();
                    }
                    if (flag == true) {
    
    
                    // 如果用户名以及密码正确的话
                        this.Login_time();
                        if (this.tableText == null) {
    
    
                            this.tableText = new TableText(this,this.jt_account.getText());
                        }    //传学号
                        this.tableText.setVisible(true);
                        this.setVisible(false);
                        System.out.println("ok is login ");
                    } else {
    
    
                        JOptionPane.showMessageDialog(this, "密码或者用户名不正确或者账号不存在");
                    }
                }
                else {
    
    
                    JOptionPane.showMessageDialog(this, "account 格式错误!!");
                }
                this.jt_password.setText("");
                this.jt_account.setText("");
            }
        if(actionEvent.getSource().equals(this.jb_register)){
    
    
        // 如果是登录的话 ,先看验证是否正确
  if(this.register==null){
    
    
      this.register= new Register(this,this.test);
  }
 this.setVisible(false); this.register.setVisible(true);
        }
    }
}

(3) 页面展示
在这里插入图片描述

3、注册页面
(1) 这个页面虽然小但是含有的东西较多,首先要检查注册的账号在Studen表中有没有出现,没有的话不允许注册。存在的话,检查是否重复,不重复的话允许注册。为了增强安全性,加入了导入图片以及验证码的内容。
(2)具体代码:


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static javax.swing.JOptionPane.WARNING_MESSAGE;
public class Register extends JFrame implements ActionListener {
    
    
   //属性
    private JLabel jl_account,jl_password,jl_img,jl_img_put,jl_code;//放置图片的组件
    private  JButton jb_submit,jb_return; //传递按钮 ,返回按钮
    private  ImageIcon img; //图片
     private  JTextField jt_account,jt_img,jt_code;//输入框,输入验证码
     private  Test test;
     public Login_in login_in;
     private JPasswordField jt_password;
     private  Md5 md5;//MD5算法
    //构造方法
  private  validCode validcode;
    public Register( Login_in login_in2 ,Test test){
    
    
        super("账户注册");
        //数据流
        this.test= test;
        this.login_in=login_in2;
        // 登录页面
      this.md5= new Md5();
        //组件定义
        //图片
        this.img= new ImageIcon(getClass().getResource("/photo/7.PNG"));//使用的是getResource
        this.img.setImage(this.img.getImage().getScaledInstance(80,100, Image.SCALE_DEFAULT));
        this.jb_submit= new JButton("注册"); this.jb_submit.addActionListener(this);
        this.jt_account=new JTextField(10);
        this.jt_password= new JPasswordField(10);
        this.jt_img= new JTextField(10);
        this.jl_account= new JLabel("输入账号");
        this.jl_password= new JLabel("输入密码"); this.jt_code= new JTextField(10);
        this.jl_code= new JLabel("输入验证码");  this.jb_return= new JButton("返回");
        this.jl_img= new JLabel("输入图片路径"); this.jl_img_put= new JLabel("上传的图片");
        this.validcode= new validCode();
        this.setSize(500,650);
        //添加组件
        this.getContentPane().setLayout(null);
        //验证码
        this.getContentPane().add(this.validcode);
        this.getContentPane().add(this.jb_submit); this.getContentPane().add(this.jt_account);
     this.getContentPane().add(this.jt_password);this.getContentPane().add(this.jt_img);        this.getContentPane().add(this.jl_account);this.getContentPane().add(this.jl_img);
         this.getContentPane().add(this.jl_password); this.getContentPane().add(this.jl_img_put); this.getContentPane().add(this.jl_code);
         this.getContentPane().add(this.jt_code); this.getContentPane().add(this.jb_return); this.jb_return.addActionListener(this);
        //设置组件位置
        this.jl_account.setBounds(80,100,100,30);
        this.jt_account.setBounds(200,100,150,30);
        this.jl_password.setBounds(80,150,100,30);
        this.jt_password.setBounds(200,150,150,30);
        this.jl_img.setBounds(80,200,100,30);
        this.jt_img.setBounds(200,200,150,30);
        this.jb_submit.setBounds(120,280,80,30);this.jb_submit.setContentAreaFilled(false);
        this.jb_return.setBounds(240,280,80,30); this.jb_return.setContentAreaFilled(false);
        this.jl_code.setBounds(80,240,80,30);
        this.jt_code.setBounds(200,240,150,30);
        this.validcode.setBounds(370,230,80,30);
        this.jl_img_put.setBounds(100,380,100,30);
        //设置页面的布局
        this.setSize(500,600);
        this.setDefaultCloseOperation(Windows.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    //画图
    public void paint(Graphics g){
    
    
    super.paint(g);//如果没有的话将会出错
      g.drawImage(this.img.getImage(),200,400,100,100,null);
    }
//验证码是不是正确
    public boolean is_right(){
    
    
        if(this.jt_code.getText().equals(this.validcode.getCode())){
    
    
            return true;
        }
        else
            return  false;
    }
    //检查账户是否相同
    public boolean is_same(String a){
    
    
        boolean flag=true;
        String sql="select password  from account_password  where account=?";
        this.test.get_sql_connection();
        try{
    
    
         this.test.ps= this.test.ct.prepareStatement(sql);
         this.test.ps.setString(1,a);
            this.test.rs=this.test.ps.executeQuery();
            flag=this.test.rs.next();
        }
        catch (Exception e){
    
    
            e.printStackTrace();
        }
        finally{
    
    
            this.test.close();
            //记得关闭
        }
        return flag;
    }
     //学号是不是存在
    public boolean is_zai(String a){
    
    
      boolean flag=false;
       
       this.test.get_sql_connection();
       try{
    
    
           String sql="select * from Student where Sno=?";
           this.test.ps=this.test.ct.prepareStatement(sql);
           this.test.ps.setString(1,a);
           this.test.rs = this.test.ps.executeQuery();
           flag = this.test.rs.next();
       }
       catch (Exception e){
    
    
           e.printStackTrace();
       }
       finally {
    
    
           this.test.close();
       }
        return flag;
    }
    //向account_password 中插入信息
    public void  is_register(String a, String b) throws IOException {
    
    
        FileInputStream fileInputStream = null;
        File f=null;
        this.test.get_sql_connection();
        System.out.println(this.jt_img.getText());
        String sql ="insert into account_password values(?,?,?)";
        try{
    
    
             f= new File(this.jt_img.getText());
            this.test.ps=this.test.ct.prepareStatement(sql);
            this.test.ps.setString(1,a);
            this.test.ps.setString(2,this.md5.getMd5_String(b));
            fileInputStream= new FileInputStream(f);
            this.test.ps.setBinaryStream(3,fileInputStream,(int)f.length());
            this.test.ps.executeUpdate();
            this.img = new ImageIcon(this.jt_img.getText());
            repaint();
            JOptionPane.showMessageDialog(this, "注册欧克!!!");
        }catch (Exception e){
    
    
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,"图片路径有误");
        }
        finally {
    
    
            this.test.close();
        }
        fileInputStream.close();
    }
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
    
    
     if(actionEvent.getSource().equals(this.jb_submit)){
    
    
         //首先判断验证码
         if(this.jt_code.getText().isEmpty()) JOptionPane.showMessageDialog(this,"验证码不能为空");
     //验证码不正确
     else  if(is_right()==false){
    
    
          JOptionPane.showMessageDialog(this,"输入正确的验证码!");
         }
     else if(jt_img.getText().isEmpty()){
    
    
         JOptionPane.showMessageDialog(this,"需要上传图片!");
         }
     //都正确的话  进行用户,密码的检验 要求密码只能是由[A-Za-z0-9]组成
     else {
    
    
         String exr=new String(this.jt_password.getPassword());
             Pattern p_password = Pattern.compile("^[A-Za-z0-9]{3,6}$");
             Matcher m_password;
             m_password = p_password.matcher(exr);
             //检查密码组合是不是有问题
             if(!m_password.find()){
    
    
                 JOptionPane.showMessageDialog(this,"密码是由大小写字母数字组成的3,6位组合的!");
             }
             //检查账户是否合理,需要访问student表判断是否存在如果没有不能创建,访问account_password表账户是否存在,存在的话,用户重复!!
             else {
    
    
                 if (this.is_zai(this.jt_account.getText()) == false) {
    
    
                     JOptionPane.showMessageDialog(this, "学号不存在");
                 }
                 //学号存在的话:
                 else {
    
    
                     String ex2= new String(this.jt_password.getPassword());
                     if (this.is_same(this.jt_account.getText()) == true) {
    
    
                         JOptionPane.showMessageDialog(this, "账号已经存在!!");
                     } else {
    
    
                         try {
    
    
                             this.is_register(this.jt_account.getText(),ex2);
                         } catch (IOException e) {
    
    
                             e.printStackTrace();
                         }
                         System.out.println(this.jt_img.getText());
                     }
                     //账户存在
                 }
                 //学号存在
             }
             //密码符合规范
       }
     //验证码正确,图片路径不为空
        // this.jt_account.setText("");this.jt_password.setText("");this.jt_img.setText(""); this.jt_code.setText("");
     }
     //如果是注册的话
     if(actionEvent.getSource().equals(this.jb_return )){
    
    
         //返回按钮 返回到login in页面
         this.jt_code.setText("");
         this.jt_password.setText("");
         this.jt_account.setText("");
         this.jt_img.setText("");
         //重新绘制图片
         this.img= new ImageIcon(getClass().getResource("/photo/7.PNG"));//使用的是getResource
         this.img.setImage(this.img.getImage().getScaledInstance(80,100, Image.SCALE_DEFAULT));
         repaint();
         
         this.login_in.setVisible(true);
         this.setVisible(false);
     }
    }
}

(3)页面展示:
在这里插入图片描述
3 学生模块
(1) 制作的系统是学生选课管理系统,因此对于学生用户而言SC是能够查询修改的,对于Student以及Course的内容是没有权限的。学生模块分为两个 页面,含有表格,按钮,JtestField等组件。因为Student具有查询的权限,因此分为两个页面。
(2) TableTest 页面:

public class TableText extends JFrame implements ActionListener {
    
    
     private MyDefaultTable table;
     protected  Login_in login_in; 
     // Login_in类用于返回登陆页面
     private ImageIcon img;
     protected  select_table se;
     private  JTable jTable;
     private  JScrollPane jScrollPane;
     //该学生的学号 ,由登录页面传递
     private String Sno;
     
     //按钮
     private JButton jb1_insert;
     private JButton jb3_delete;
     private  JButton jb4_select;
      private  JButton jb5_return ;
    // 输入框
    private  JLabel jl_select;
     private  JTextField insert1,delete1,select1;
     private JPanel jPanel_all;
     private JPanel jPanel;  public JPanel jPanel1,jPanel2,jPanel3;
     private  JLabel jLabel1,jl;//提示信息
    //连接数据的 类
    public Test test;
    //提示输入信息
    public JLabel jl1,jl3,jl5;
     public TableText(Login_in login_in,String s){
    
    
         this.login_in= login_in;
         this.img= new ImageIcon(getClass().getResource("/photo/10.jpg"));//导入图片
         this.jl= new JLabel(this.img);
         this.jl.setBounds(0,0,800,550);
         this.test= new Test();
         this.Sno=s;
         this.jPanel3= new JPanel();
         this.test.select("Course");
        String [] str1={
    
     "Cno","Cname","grade"};
        this.table= new MyDefaultTable(str1,22);
        //表格中添加信息
        for(int i=1;i<=this.test.con.size();i++){
    
    
            this.table.setValueAt(this.test.con.get(i-1),i-1,0);
            this.table.setValueAt(this.test.sno.get(i-1),i-1,1);
            this.table.setValueAt(this.test.Cname.get(i-1),i-1,2);
        }
        //这个就是在Test 类中提到的vector容器
        this.test.vector_free();
        this.jTable= new JTable(this.table);this.jScrollPane= new JScrollPane(this.jTable);
        //输入框
        this.insert1=new JTextField(20);this.delete1= new JTextField(20);
       this.select1=new JTextField(20);
        //提示输入信息
        this.jl1= new JLabel("选择的课程");
        this.jl3= new JLabel("选择的课程");
        this.jl5= new JLabel("选择的课程");
        this.jl_select= new JLabel("查看你所选的课程");
        //按钮
        this.jb1_insert= new JButton("插入");  this.jb3_delete= new JButton("删除");
        this.jb4_select= new JButton("查看"); this.jb5_return= new JButton("返回");
        this.jb1_insert.addActionListener(this);
        //注册监听器
          this.jb3_delete.addActionListener(this);
        this.jb4_select.addActionListener(this);      
        this.jb5_return.addActionListener(this);
        this.jPanel= new JPanel(); this.jPanel1= new JPanel();this.jPanel2=new JPanel();//四个不同的
        this.jLabel1= new JLabel("以上是可以选择的选课信息,你可以进行查询,修改,添加,删除操作,要选择的课程!!");
        this.jPanel_all= new JPanel();
        this.jPanel_all.setLayout(new GridLayout(5,1));
         this.jb5_return.setContentAreaFilled(false);
        this.jb4_select.setContentAreaFilled(false);
        this.jb3_delete.setContentAreaFilled(false);
         this.jb1_insert.setContentAreaFilled(false);
        //分为5行1列添加组件
       this.jPanel.add(this.jb1_insert);this.jPanel.add(this.jl1);this.jPanel.add(this.insert1);        this.jPanel1.add(this.jb3_delete);this.jPanel1.add(this.jl3);this.jPanel1.add(this.delete1);
        this.jPanel2.add(this.jl_select);
       this.jPanel2.add(this.jb4_select);//this.jPanel2.add(this.jl5);this.jPanel2.add(this.select1);
        this.jPanel3.add(this.jb5_return);
        this.setLayout(new GridLayout(2,1));
        this.setSize(800,550);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.getContentPane().add(this.jScrollPane);        this.jPanel_all.add(this.jLabel1);this.jPanel_all.add(this.jPanel); this.jPanel_all.add(this.jPanel1);this.jPanel_all.add(this.jPanel2);
        this.jPanel_all.add(this.jPanel3);
        //设置背景图片
         this.getLayeredPane().add(this.jl, new Integer(Integer.MIN_VALUE));
         ((JPanel)this.getContentPane()).setOpaque(false);
         this.jPanel1.setOpaque(false);
         this.jPanel_all.setOpaque(false); this.jPanel.setOpaque(false); this.jPanel2.setOpaque(false); this.jPanel3.setOpaque(false);
         this.jScrollPane.setOpaque(false); this.insert1.setOpaque(false); this.delete1.setOpaque(false); this.select1.setOpaque(false);
         this.jTable.setOpaque(false);
         this.jl_select.setOpaque(false);
         this.getContentPane().add(this.jPanel_all);
        this.setVisible(true);
        this.setDefaultCloseOperation(Windows.EXIT_ON_CLOSE);
    }
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
    
    
         // insert按钮
         
        if(actionEvent.getSource().equals(this.jb1_insert)) {
    
    
            String b = this.insert1.getText();
                try {
    
    
                    this.test.insert_SC_by_Stu(this.Sno, b);
                    JOptionPane.showMessageDialog(this, "insert 成功!!");
                } catch (Exception e) {
    
    
                   JOptionPane.showMessageDialog(this,"输入信息错误或者不能为空!!");
                }
        }
        //select 按钮
       if(actionEvent.getSource().equals(this.jb4_select)){
    
    
         if(this.se==null){
    
    
             this.se= new select_table(this);
         }
         this.se.into(this.Sno);
         this.setVisible(false);
        this.se.setVisible(true);
       }
        //delete 按钮
      if(actionEvent.getSource().equals(this.jb3_delete)) {
    
    
          int i;
          String b = this.delete1.getText();
              try {
    
    
                 i= this.test.delete_SC(this.Sno, b);
                 if(i==1) {
    
    
                     JOptionPane.showMessageDialog(this, "delete 成功!!!");
                 }
                 else {
    
    
           //因为空或者是不存在的元组直接在数据库中执行删除动作是没有问题的
                     JOptionPane.showMessageDialog(this,"输入不存在或者不能为空!");
                 }
                 } catch (Exception e) {
    
    
                  JOptionPane.showMessageDialog(this, "输入信息错误");
              }
          }
        //return 按钮
       if(actionEvent.getSource().equals(this.jb5_return)){
    
    
             this.setVisible(false);
             this.login_in.setVisible(true);
       }
    }
}

(3)select_table页面:

import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class select_table extends JFrame implements ActionListener {
    
    
   protected  MyDefaultTable table; protected  TableText tableText;
    private  JTable jTable;
    public Test test;
    // TEST类也可以叫做数据库类
    private JTextField jTextField;
    private DefaultTableCellRenderer render;
    private  JScrollPane jScrollPane;
    public ImageIcon img;
    private int count;
    public JLabel jl; public JButton jb;

   public select_table(TableText tableText){
    
    
       this.tableText=tableText;

       this.render= new DefaultTableCellRenderer();
       this.render.setOpaque(false);
       this.img= new ImageIcon(getClass().getResource("/photo/9.png"));//导入图片
       this.jTextField = new JTextField();
       this.jl= new JLabel(this.img);
       this.jl.setBounds(0,0,this.img.getIconWidth(),this.img.getIconHeight());
       this.test= new Test();
       this.jb= new JButton("return ");
    //   this.jPanel= new JPanel();
     // this.jPanel2= new JPanel();
       String []sc={
    
    "Sno","Cno","Cname"};
       this.table= new MyDefaultTable(sc,22);
       this.jTable= new JTable(this.table);this.jScrollPane= new JScrollPane(this.jTable);
       this.setSize(this.img.getIconWidth(),this.img.getIconHeight());
       //流布局
       this.setLayout(null);
       this.jb.addActionListener(this);
       this.jScrollPane.setBounds(100,this.img.getIconHeight()/2-40,350,260);
       this.jb.setBounds(450,this.img.getIconHeight()/2+190,80,30);
       this.jTextField.setBounds(225,this.img.getIconHeight()/2+230,80,30);
       //this.jPanel.setLayout(new GridLayout(2,1));
       //this.jPanel2.add(this.jb);
      //this.jPanel.add(this.jScrollPane);this.jPanel.add(this.jPanel2);
       this.getLayeredPane().add(this.jl, new Integer(Integer.MIN_VALUE));
       ((JPanel)this.getContentPane()).setOpaque(false);
       for(int i=0;i<=2;i++){
    
    
           this.jTable.getColumn(sc[i]).setCellRenderer(this.render);
       }//需要渲染
       this.jScrollPane.setOpaque(false);
       this.jTable.setOpaque(false);
       this.jScrollPane.getViewport().setOpaque(false);
       this.jTextField.setOpaque(false);
      // this.getContentPane().add(this.jPanel);
       this.jb.setContentAreaFilled(false);
       //this.jPanel.setOpaque(false); this.jPanel2.setOpaque(false);
       this.getContentPane().add(this.jTextField);
       this.getContentPane().add(this.jScrollPane);this.getContentPane().add(this.jb);
       this.setDefaultCloseOperation(Windows.EXIT_ON_CLOSE);
       this.setLocationRelativeTo(null);
       this.setVisible(true);
   }
public void into(String s){
    
    
// 需要将页面清空一下
       try{
    
    
           for( int i=0;i<22;i++){
    
    
               this.table.setValueAt("",i,0);
               this.table.setValueAt("",i,1);
               this.table.setValueAt("",i,2);
           }
          int i=0;
           this.test.get_sql_connection();
           this.test.ps=this.test.ct.prepareStatement("{call pro_tran(?)}");
           //这个是调用
           this.test.ps.setString(1,s);
           this.test.rs= this.test.ps.executeQuery();
           while (this.test.rs.next()){
    
    
               this.table.setValueAt(this.test.rs.getString(1),i,0);
               this.table.setValueAt(this.test.rs.getString(2),i,1);
               this.table.setValueAt(this.test.rs.getString(3),i,2);
           i++;
           this.count=i;
           }
       }catch (Exception e){
    
    
           e.printStackTrace();
       }
       finally {
    
    
           this.test.close();
       }
       this.jTextField.setText("共有"+this.count+"记录");
       //统计功能
}
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
    
    
       if(actionEvent.getSource().equals(this.jb)){
    
    
           //返回的按钮
        this.tableText.setVisible(true);
         this.setVisible(false);
       }
    }
}

(3)页面展示:
在这里插入图片描述
(这个是有背景图片的)
在这里插入图片描述(致敬一下欧拉)
四、教师模块
(1)自定义 Windows 页面 :这个类可以比作为中转站

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Base64;
import javax.swing.event.*;
public class Windows  extends JFrame implements ActionListener {
    
    
public Test test;
    public Login_in login_in =null;
    protected Cour_tea cour_tea;
   private  Stu_tea stu_tea;
   private  SC_tea sc_tea;
   private ImageIcon img;

    public JButton jb_back;
    public JButton jb_Student,jb_SC,jb_Course;
    // 容器
    //图片
    public ImageIcon Icon ; public JLabel jl1;
    // 提示信息
    private JLabel jl_Student,jl_SC,jl_Course,jl;
    public Windows(Login_in login_in ,Test test){
    
    
        super("ADMIN");
        this.login_in=login_in;
       this.test= test;
        this.img= new ImageIcon(getClass().getResource("/photo/14.jpg"));//导入图片
        this.jl= new JLabel(this.img);
        this.jl.setBounds(0,0,800,600);
        //定义图片
        //this.Icon= new ImageIcon(getClass().getResource("/photo/5.jpg"));//使用的是getResource
       // this.Icon.setImage(this.Icon.getImage().getScaledInstance(500,300,Image.SCALE_DEFAULT));
        this.jl1= new JLabel();
       // this.jl1.setIcon(this.Icon);//设置图片位置
        //按钮
        this.jb_back= new JButton("return");
        this.jb_back.addActionListener(this);

        this.jb_SC= new JButton("SC "); this.jb_Student= new JButton("Student");this.jb_Course= new JButton("Course");
        this.jb_SC.addActionListener(this); this.jb_Course.addActionListener(this); this.jb_Student.addActionListener(this);
        this.jb_back.addActionListener(this);

         //提示信息
        this.jl_Course= new JLabel("Course 表"); this.jl_SC= new JLabel("SC 表");this.jl_Student= new JLabel("Student 表");
       //设置尺寸大小 布局方式

        this.setSize(800,600);
        this.getLayeredPane().add(this.jl, new Integer(Integer.MIN_VALUE));
        ((JPanel)this.getContentPane()).setOpaque(false);
        this.setLayout(null);
        this.jl_SC.setBounds(480,150,80,30); this.jl_SC.setOpaque(false);
        this.jb_SC.setBounds(600,150,80,30);
        this.jl_Student.setBounds(480,200,80,30);this.jl_Student.setOpaque(false);
        this.jb_Student.setBounds(600,200,80,30);
        this.jl_Course.setBounds(480,250,80,30); this.jl_Course.setOpaque(false);
        this.jb_Course.setBounds(600,250,80,30);
        this.jb_back.setBounds(550,300,80,30);

        // 容器添加组件
        this.jb_back.setContentAreaFilled(false); this.jb_Student.setContentAreaFilled(false);
        this.jb_Course.setContentAreaFilled(false); this.jb_SC.setContentAreaFilled(false);
         //this.getContentPane().add(this.jl1);
        this.getContentPane().add(this.jb_SC);this.getContentPane().add(this.jb_Student);this.getContentPane().add(this.jb_Course);this.getContentPane().add(this.jb_back);
         this.getContentPane().add(this.jl_Course);this.getContentPane().add(this.jl_Student);
         this.getContentPane().add(this.jl_SC);
        this.setLocationRelativeTo(null);
         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
    
    
 if(actionEvent.getSource().equals(this.jb_back)){
    
    
  // 返回主页面
     this.setVisible(false);
     this.login_in.setVisible(true);
 }
 // SC页面
 if(actionEvent.getSource().equals(this.jb_SC)){
    
    
     if(this.sc_tea==null){
    
    
         this.sc_tea=new SC_tea(this,this.test);
     }
     else
         this.sc_tea.setVisible(true);
     this.setVisible(false);
 }
 //Student 页面
if(actionEvent.getSource().equals(this.jb_Student)){
    
    
    if(this.stu_tea==null){
    
    
      this.stu_tea=new Stu_tea(this,this.test);
    }
    else this.stu_tea.setVisible(true);
    this.setVisible(false);
}
//Course 页面
if(actionEvent.getSource().equals(this.jb_Course)){
    
    
    if(this.cour_tea==null){
    
    
        this.cour_tea= new Cour_tea(this,this.test);
    }
    else this.cour_tea.setVisible(true);
    this.setVisible(false);
}
    }
}

在这里插入图片描述

(2)SC页面 教师对学生的选课进行管理:除了基本的功能还具有统计排序功能

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class SC_tea extends JFrame implements ActionListener {
    
    
    //表格信息
    public Windows windows;
    private MyDefaultTable table;
    private  JTable jTable;
    private  JScrollPane jScrollPane;
    private DefaultTableCellRenderer render;
    private Test test;
    private JTextField jTextField;
    private ImageIcon img;
    private int count;
    //按钮
    protected JButton jb_insert ,jb_select ,jb_update,jb_delete,jb_back,jb_order,jb_select2,jb_select_sc;
    //布局容器
    protected  JPanel jp_all,jp_insert ,jp_update,jp_delete,jp_select,jp_back,jp_1;
    //输入信息
    protected  JTextField jt_insert,jt_insert2,jt_insert3;
    protected  JTextField jt_update,jt_update2,jt_update3;
    protected  JTextField jt_delete,jt_delete2,jt_delete3;
    protected  JTextField jt_select,jt_order,jt_select2;
    //提示信息
    protected  JLabel jl_insert,jl_insert2,jl_insert3,jl;
    protected  JLabel jl_update,jl_update2,jl_update3;
    protected  JLabel jl_select,jl_order,jl_select2;
    protected  JLabel jl_delete,jl_delete2,jl_delete3;
    //构造
    public SC_tea(Windows windows, Test test){
    
    
       //表格
        super("SC");

        this.windows=windows;
        this.test=test;
        this.render= new DefaultTableCellRenderer();
        this.render.setOpaque(false);
        this.img= new ImageIcon(getClass().getResource("/photo/12.jpg"));//导入图片
        this.jl= new JLabel(this.img);
        this.jl.setBounds(0,0,1000,700);
        String []str={
    
    "Sno","Cno","Grade"};
        this.table=new MyDefaultTable(str,22);
        this.jb_select2= new JButton("查询课程");
        this.jb_select_sc=new JButton("查看所有");
        this.jTable= new JTable(this.table);
        this.jScrollPane= new JScrollPane(this.jTable);
        //按钮
        this.jb_delete= new JButton("删除");this.jb_insert= new JButton("插入");
        this.jb_select= new JButton("查看学生");this.jb_update= new JButton("更新");
        this.jb_update.addActionListener(this); this.jb_select.addActionListener(this);
        this.jb_insert.addActionListener(this);this.jb_delete.addActionListener(this);
        this.jb_back= new JButton("return"); this.jb_back.addActionListener(this);
        this.jb_order= new JButton("排序");this.jb_order.addActionListener(this);
        this.jb_select_sc.addActionListener(this);
        this.jb_select2.addActionListener(this);
        //输入信息 提示信息
         // insert
         this.jt_insert= new JTextField(10);this.jt_insert2= new JTextField(10);this.jt_insert3= new JTextField(10);
         this.jl_insert = new JLabel("学号"); this.jl_insert2= new JLabel("选课编号"); this.jl_insert3= new JLabel("成绩");
          this.jt_insert.setOpaque(false); this.jt_insert2.setOpaque(false); this.jt_insert3.setOpaque(false);
          this.jl_insert.setOpaque(false); this.jl_insert2.setOpaque(false); this.jl_insert3.setOpaque(false);
            this.jTextField= new JTextField(10);
         // update
          this.jt_update= new JTextField(10);this.jt_update2= new JTextField(10);this.jt_update3= new JTextField(10);
          this.jl_update = new JLabel("学号"); this.jl_update2= new JLabel("选课编号");this.jl_update3= new JLabel("成绩");
          this.jt_update.setOpaque(false); this.jt_update2.setOpaque(false); this.jt_update3.setOpaque(false);
          this.jl_update.setOpaque(false); this.jl_update2.setOpaque(false); this.jl_update3.setOpaque(false);
          // select
         this.jt_select = new JTextField(10); this.jl_select2= new JLabel("输入课程编号"); this.jt_select2= new JTextField(10);
         this.jl_select= new JLabel("学号"); this.jt_order= new JTextField(10); this.jl_order=new JLabel("输入课程编号");
         this.jt_select.setOpaque(false); this.jl_select2.setOpaque(false); this.jt_select2.setOpaque(false);
         this.jl_select.setOpaque(false); this.jt_order.setOpaque(false); this.jl_order.setOpaque(false);
         // delete
         this.jt_delete= new JTextField(10);this.jt_delete2= new JTextField(10);this.jt_delete3= new JTextField(10);
         this.jl_delete= new JLabel("学号");this.jl_delete2= new JLabel("选课编号");this.jl_delete3= new JLabel("成绩");
         this.jt_delete.setOpaque(false); this.jt_delete2.setOpaque(false); this.jt_delete3.setOpaque(false);
         this.jl_delete.setOpaque(false); this.jl_delete2.setOpaque(false); this.jl_delete3.setOpaque(false);
        //容器
        this.jp_all= new JPanel();this.jp_insert= new JPanel();this.jp_select= new JPanel();this.jp_update= new JPanel();
        this.jp_delete= new JPanel(); this.jp_back=new JPanel(); this.jp_1= new JPanel();
        //布局
        this.setSize(1000,700);
        this.setLayout(new GridLayout(2,1));
         this.getContentPane().add(this.jScrollPane);
        this.getLayeredPane().add(this.jl, new Integer(Integer.MIN_VALUE));
        ((JPanel)this.getContentPane()).setOpaque(false);
        for(int i=0;i<=2;i++){
    
    
            this.jTable.getColumn(str[i]).setCellRenderer(this.render);
        }
        this.jScrollPane.setOpaque(false);
        this.jTable.setOpaque(false);
        this.jScrollPane.getViewport().setOpaque(false);
         this.jp_all.setLayout(new GridLayout(6,1));
         // jp_insert
        this.jp_insert.setOpaque(false); this.jTextField.setOpaque(false);
        this.jp_insert.add(this.jTextField);        this.jp_insert.add(this.jb_insert);this.jp_insert.add(this.jl_insert);this.jp_insert.add(this.jt_insert);        this.jp_insert.add(this.jl_insert2);this.jp_insert.add(this.jt_insert2);this.jp_insert.add(this.jl_insert3);this.jp_insert.add(this.jt_insert3);
        // jp_update
        this.jp_update.setOpaque(false);       this.jp_update.add(this.jb_update);this.jp_update.add(this.jl_update);this.jp_update.add(this.jt_update);this.jp_update.add(this.jl_update2);
        this.jp_update.add(this.jt_update2);this.jp_update.add(this.jl_update3);this.jp_update.add(this.jt_update3);
        // jp_delete
        this.jp_delete.setOpaque(false);       this.jp_delete.add(this.jb_delete);this.jp_delete.add(this.jl_delete);this.jp_delete.add(this.jt_delete);       this.jp_delete.add(this.jl_delete2);this.jp_delete.add(this.jt_delete2);
        this.jp_delete.add(this.jl_delete3);this.jp_delete.add(this.jt_delete3);
        //jp_select
        this.jp_select.setOpaque(false);
        this.jp_select.add(this.jb_select);this.jp_select.add(this.jl_select);this.jp_select.add(this.jt_select);
         this.jp_select.add(this.jb_select2);this.jp_select.add(this.jl_select2);this.jp_select.add(this.jt_select2);
       //
        this.jp_back.setOpaque(false);
       this.jp_back.add(this.jb_order); this.jp_back.add(this.jl_order);this.jp_back.add(this.jt_order);
       //
        this.jp_1.setOpaque(false);
        this.jp_1.add(this.jb_select_sc); this.jp_1.add(this.jb_back);
       //布局
        this.jp_all.setOpaque(false);
         this.jp_all.add(this.jp_insert);this.jp_all.add(this.jp_update);this.jp_all.add(this.jp_delete);this.jp_all.add(this.jp_select);this.jp_all.add(this.jp_back);
        this.jp_all.add(this.jp_1);
        //jb_insert ,jb_select ,jb_update,jb_delete,jb_back,jb_order,jb_select2,jb_select_sc;
        this.jb_insert.setContentAreaFilled(false); this.jb_select.setContentAreaFilled(false);
        this.jb_update.setContentAreaFilled(false); this.jb_delete.setContentAreaFilled(false);
        this.jb_back.setContentAreaFilled(false); this.jb_order.setContentAreaFilled(false);
        this.jb_select2.setContentAreaFilled(false); this.jb_select_sc.setContentAreaFilled(false);
          //设置
         this.getContentPane().add(this.jp_all);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(Windows.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    //在table添加数据
    public void into (){
    
    
        try{
    
    
            int i=0;
            this.test.get_sql_connection();
            this.test.ps=this.test.ct.prepareStatement("select * from SC ");
            this.test.rs= this.test.ps.executeQuery();
            for (int j=i;j<22;j++){
    
    
                this.table.setValueAt(null,i,0);
                this.table.setValueAt(null,i,1);
                this.table.setValueAt(null,i,2);
            }
            while (this.test.rs.next()){
    
    
                this.table.setValueAt(this.test.rs.getString(1),i,0);
                this.table.setValueAt(this.test.rs.getString(2),i,1);
                this.table.setValueAt(this.test.rs.getString(3),i,2);
                i++;
                this.count=i;
            }

        }catch (Exception e){
    
    
            e.printStackTrace();
        }
        finally {
    
    
            this.test.close();
        }
        this.jTextField.setText("");
        this.jTextField.setText("共有"+this.count+"记录");
    }
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
    
    
  // insert
        if(actionEvent.getSource().equals(this.jb_insert)){
    
    
        String a=this.jt_insert.getText();
        String b= this.jt_insert2.getText();
        String c= this.jt_insert3.getText();
            try {
    
    
                this.test.insert_SC_by_tea(a,b,c);
                JOptionPane.showMessageDialog(this,"加入成功");
            } catch (Exception e) {
    
    
                JOptionPane.showMessageDialog(this,"检查输入!!");
            }
  }
        //delete
        if(actionEvent.getSource().equals(this.jb_delete)){
    
    
           int i;
            String a=this.jt_delete.getText();
           String b=this.jt_delete2.getText();
            try {
    
    
                i=this.test.delete_SC(a,b);
               if(i==1){
    
    
                   JOptionPane.showMessageDialog(this,"删除成功!!");
               }
                else {
    
    
                    JOptionPane.showMessageDialog(this,"信息错误!!");
               }
            } catch (SQLException e) {
    
    
              JOptionPane.showMessageDialog(this,"检查输入!!");
            }
        }
        // update
        if(actionEvent.getSource().equals(this.jb_update)){
    
    
            String a=this.jt_update.getText();
            String b=this.jt_update2.getText();
            String c=this.jt_update3.getText();
            System.out.println(c);
            try {
    
    
                if(this.test.update_sc_by_tea(a,b,c)==1){
    
    
                    JOptionPane.showMessageDialog(this,"更新成功!");
                }
                else{
    
    
                    JOptionPane.showMessageDialog(this,"检查输入!");
                }
            } catch (Exception e) {
    
    
             JOptionPane.showMessageDialog(this,"ERROR 检查输入!!");
            }
        }
        //学号查询
        if(actionEvent.getSource().equals(this.jb_select)){
    
    
            String a=this.jt_select.getText();
            try{
    
    
                for(int j=0;j<22;j++){
    
    
                    this.table.setValueAt(null,j,0);
                    this.table.setValueAt(null,j,1);
                    this.table.setValueAt(null,j,2);
                }
                int i=0;
                this.test.get_sql_connection();
                this.test.ps=this.test.ct.prepareStatement("select * from SC where Sno ="+a);
                this.test.rs= this.test.ps.executeQuery();
                while (this.test.rs.next()){
    
    
                    this.table.setValueAt(this.test.rs.getString(1),i,0);
                    this.table.setValueAt(this.test.rs.getString(2),i,1);
                    this.table.setValueAt(this.test.rs.getString(3),i,2);
                    i++;
                 this.count=i;
                }
            }catch (Exception e){
    
    
              JOptionPane.showMessageDialog(this,"检查输入!!");
            }
            finally {
    
    
                this.test.close();
            }
            this.jTextField.setText("");
            //统计功能
            this.jTextField.setText("共有"+this.count+"记录");
        }
        //课程查询
        if(actionEvent.getSource().equals(this.jb_select2)){
    
    
            String a=this.jt_select2.getText();
            try{
    
    
                int i=0;
                this.test.get_sql_connection();
                this.test.ps=this.test.ct.prepareStatement("select * from SC where Cno ="+a);
                this.test.rs= this.test.ps.executeQuery();
                for(int j=i;j<22;j++){
    
    
                    this.table.setValueAt(null,j,0);
                    this.table.setValueAt(null,j,1);
                    this.table.setValueAt(null,j,2);
                }
                while (this.test.rs.next()){
    
    
                   this.table.setValueAt(this.test.rs.getString(1),i,0);
                    this.table.setValueAt(this.test.rs.getString(2),i,1);
                    this.table.setValueAt(this.test.rs.getString(3),i,2);
                    i++;
                }
              this.count=i;
            }catch (Exception e){
    
    
               JOptionPane.showMessageDialog(this,"检查输入!!!!");
            }
            finally {
    
    
                this.test.close();
            }
            this.jTextField.setText("共有"+this.count+"记录");
        }
        //排序
        if(actionEvent.getSource().equals(this.jb_order)){
    
    
            String a=this.jt_order.getText();
            try{
    
    
                int i=0;
                this.test.get_sql_connection();
                this.test.ps=this.test.ct.prepareStatement("select * from SC where Cno ="+a+" order by Grade");
                this.test.rs= this.test.ps.executeQuery();
                while (this.test.rs.next()){
    
    
                    this.table.setValueAt(this.test.rs.getString(1),i,0);
                    this.table.setValueAt(this.test.rs.getString(2),i,1);
                    this.table.setValueAt(this.test.rs.getString(3),i,2);
                    i++;
                }
                for(int j=i;j<22;j++){
    
    
                    this.table.setValueAt(null,j,0);
                    this.table.setValueAt(null,j,1);
                    this.table.setValueAt(null,j,2);
                }
            }catch (Exception e){
    
    
               JOptionPane.showMessageDialog(this,"检查输入!!");
            }
            finally {
    
    
                this.test.close();
            }
            this.jTextField.setText("");
        }
        //查看所有选课记录
  if(actionEvent.getSource().equals(this.jb_select_sc)){
    
    
      this.into();
  }
  //返回
  if(actionEvent.getSource().equals(this.jb_back)){
    
    
     this.windows.setVisible(true);
      this.setVisible(false);
  }
}
}

在这里插入图片描述
(3)Student 表学生的属性,管理员教师可以管理 这个页面相比之较为简单。

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class Stu_tea extends JFrame implements ActionListener {
    
    
     public Windows windows;
    private MyDefaultTable table;
    private ImageIcon img;
    private DefaultTableCellRenderer render;
    private  JTable jTable;
    private  JScrollPane jScrollPane;
    private Test test;
    public JButton jb_delete,jb_select,jb_update,jb_insert,jb_back;
    //布局容器
    protected  JPanel jp_all,jp_insert ,jp_update,jp_delete,jp_select;
    //输入信息
    protected  JTextField jt_insert,jt_insert2,jt_insert3;
    protected  JTextField jt_update,jt_update2,jt_update3;
    protected  JTextField jt_delete,jt_delete2,jt_delete3;

    //提示信息
    protected  JLabel jl_insert,jl_insert2,jl_insert3,jl;
    protected  JLabel jl_update,jl_update2,jl_update3;

    protected  JLabel jl_delete,jl_delete2,jl_delete3;
    public Stu_tea(Windows windows,Test test){
    
    
        super("Student");
        this.render= new DefaultTableCellRenderer();
        this.render.setOpaque(false);
        this.img= new ImageIcon(getClass().getResource("/photo/13.jpg"));//导入图片
        this.jl= new JLabel(this.img);
        this.jl.setBounds(0,0,1000,700);
          this.windows=windows;
        this.test=  test;
        String []str={
    
    "学号","姓名","年龄"};
        this.table=new MyDefaultTable(str,22);
        this.jTable= new JTable(this.table);
        this.jScrollPane= new JScrollPane(this.jTable);
        //按钮
        this.jb_delete= new JButton("删除");this.jb_insert= new JButton("插入");
        this.jb_select= new JButton("查看");this.jb_update= new JButton("更新");
        this.jb_update.addActionListener(this); this.jb_select.addActionListener(this);
        this.jb_insert.addActionListener(this);this.jb_delete.addActionListener(this);
        this.jb_back= new JButton("return"); this.jb_back.addActionListener(this);
        // insert
        this.jt_insert= new JTextField(10);this.jt_insert2= new JTextField(10);this.jt_insert3= new JTextField(10);
        this.jl_insert = new JLabel("学号"); this.jl_insert2= new JLabel("姓名"); this.jl_insert3= new JLabel("年龄");
        this.jt_insert.setOpaque(false); this.jt_insert2.setOpaque(false); this.jt_insert3.setOpaque(false);
        this.jl_insert.setOpaque(false); this.jl_insert2.setOpaque(false); this.jl_insert3.setOpaque(false);
        // update
        this.jt_update= new JTextField(10);this.jt_update2= new JTextField(10);this.jt_update3= new JTextField(10);
        this.jl_update = new JLabel("学号"); this.jl_update2= new JLabel("姓名");this.jl_update3= new JLabel("年龄");
        this.jt_update.setOpaque(false); this.jt_update2.setOpaque(false); this.jt_update3.setOpaque(false);
        this.jl_update.setOpaque(false); this.jl_update2.setOpaque(false); this.jl_update3.setOpaque(false);        // select
        // delete
        this.jt_delete= new JTextField(10);this.jt_delete2= new JTextField(10);this.jt_delete3= new JTextField(10);
        this.jl_delete= new JLabel("学号");this.jl_delete2= new JLabel("姓名");this.jl_delete3= new JLabel("年龄");
        this.jt_delete.setOpaque(false); this.jt_delete2.setOpaque(false); this.jt_delete3.setOpaque(false);
        this.jl_delete.setOpaque(false); this.jl_delete2.setOpaque(false); this.jl_delete3.setOpaque(false);
        //容器
        this.jp_all= new JPanel();this.jp_insert= new JPanel();this.jp_select= new JPanel();this.jp_update= new JPanel();
        this.jp_delete= new JPanel();
        //布局
        this.setSize(1000,700);
        this.setLayout(new GridLayout(2,1));
        this.getContentPane().add(this.jScrollPane);
        this.jp_all.setLayout(new GridLayout(4,1));
        // jp_insert
        this.jp_insert.add(this.jb_insert);this.jp_insert.add(this.jl_insert);this.jp_insert.add(this.jt_insert);
        this.jp_insert.add(this.jl_insert2);this.jp_insert.add(this.jt_insert2);this.jp_insert.add(this.jl_insert3);this.jp_insert.add(this.jt_insert3);
        // jp_update
        this.jp_update.add(this.jb_update);this.jp_update.add(this.jl_update);this.jp_update.add(this.jt_update);this.jp_update.add(this.jl_update2);
        this.jp_update.add(this.jt_update2);this.jp_update.add(this.jl_update3);this.jp_update.add(this.jt_update3);
        // jp_delete
        this.jp_delete.add(this.jb_delete);this.jp_delete.add(this.jl_delete);this.jp_delete.add(this.jt_delete);
        this.jp_delete.add(this.jl_delete2);this.jp_delete.add(this.jt_delete2);
        this.jp_delete.add(this.jl_delete3);this.jp_delete.add(this.jt_delete3);
        //jp_select
        this.jp_select.add(this.jb_select);
      this.jp_select.add(this.jb_back);
        //布局
        this.jp_all.setOpaque(false);
        this.jp_insert.setOpaque(false); this.jp_update.setOpaque(false); this.jp_delete.setOpaque(false);
        this.jp_select.setOpaque(false);
        this.jp_all.add(this.jp_insert);this.jp_all.add(this.jp_update);this.jp_all.add(this.jp_delete);this.jp_all.add(this.jp_select);
        //jb_delete,jb_select,jb_update,jb_insert,jb_back; 组件为空

        this.jb_delete.setContentAreaFilled(false); this.jb_back.setContentAreaFilled(false); this.jb_insert.setContentAreaFilled(false);
        this.jb_select.setContentAreaFilled(false); this.jb_update.setContentAreaFilled(false);
        //设置
        this.getLayeredPane().add(this.jl, new Integer(Integer.MIN_VALUE));
        ((JPanel)this.getContentPane()).setOpaque(false);
        for(int i=0;i<=2;i++){
    
    
            this.jTable.getColumn(str[i]).setCellRenderer(this.render);
        }
        this.jScrollPane.setOpaque(false);
        this.jTable.setOpaque(false);
        this.jScrollPane.getViewport().setOpaque(false);
        this.getContentPane().add(this.jp_all);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(Windows.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
    
    
  //insert 功能
   if(actionEvent.getSource().equals(this.jb_insert)) {
    
    
       try {
    
    
           String a = this.jt_insert.getText();
           String b = this.jt_insert2.getText();
           String c = this.jt_insert3.getText();
           this.test.insert_by_tea(a, b, c, "Student");
       JOptionPane.showMessageDialog(this,"ok!");
       }
       catch (Exception e){
    
    
           e.printStackTrace();
           JOptionPane.showMessageDialog(this,"请检查输入信息!!");
       }
   }
   // delete 功能
        if(actionEvent.getSource().equals(this.jb_delete)) {
    
    
            String a = this.jt_delete.getText();
            if (a.equals("")) {
    
    
               JOptionPane.showMessageDialog(this,"ERROR 检查输入!!");
            } else {
    
    
                try {
    
    
                    this.test.delete(a);
                    JOptionPane.showMessageDialog(this, "delete ok!!!");
                } catch (Exception e) {
    
    
                    JOptionPane.showMessageDialog(this, "ERROR!!!");
                }
            }
        }
    // update
       if(actionEvent.getSource().equals(this.jb_update)){
    
    
      String a =this.jt_update.getText();
      String b=this.jt_update2.getText();
      String c=this.jt_update3.getText();
           try {
    
    
               if(this.test.update_Student_tea(a,b,c)==1){
    
    
                   JOptionPane.showMessageDialog(this,"update ok!!");
               }
               else {
    
    
                   JOptionPane.showMessageDialog(this,"检查输入!");
               }
           } catch (Exception e) {
    
    
              JOptionPane.showMessageDialog(this,"ERROR!!! 检查输入信息");
           }
       }
       // select
       if(actionEvent.getSource().equals(this.jb_select)){
    
    
           try{
    
    
               int i=0;
               this.test.get_sql_connection();
               this.test.ps=this.test.ct.prepareStatement("select * from Student");
               this.test.rs= this.test.ps.executeQuery();
               while (this.test.rs.next()){
    
    
                   this.table.setValueAt(this.test.rs.getString(1),i,0);
                   this.table.setValueAt(this.test.rs.getString(2),i,1);
                   this.table.setValueAt(this.test.rs.getString(3),i,2);
                   i++;
               }
           }catch (Exception e){
    
    
              JOptionPane.showMessageDialog(this,"检查输入信息");
           }
           finally {
    
    
               this.test.close();
           }
       }
        if(actionEvent.getSource().equals(this.jb_back)){
    
    
            //返回
              this.setVisible(false);
              this.windows.setVisible(true);
        }
    }
}

在这里插入图片描述
(4)Course页面 用于记录课程

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class Cour_tea extends JFrame implements ActionListener {
    
    
   public Windows windows;
    private MyDefaultTable table;
    private  JTable jTable;
    private  JScrollPane jScrollPane;
    private ImageIcon img;
    private Test test;
    private  DefaultTableCellRenderer render;
    public JButton jb_delete,jb_select,jb_update,jb_insert,jb_back;
    //布局容器
    protected  JPanel jp_all,jp_insert ,jp_update,jp_delete,jp_select;
    //输入信息
    protected  JTextField jt_insert,jt_insert2,jt_insert3;
    protected  JTextField jt_update,jt_update2,jt_update3;
    protected  JTextField jt_delete,jt_delete2,jt_delete3;
    //提示信息
    protected  JLabel jl_insert,jl_insert2,jl_insert3,jl;
    protected  JLabel jl_update,jl_update2,jl_update3;
    protected  JLabel jl_delete,jl_delete2,jl_delete3;
    public Cour_tea(Windows windows ,Test test){
    
    
        super(" Course ");
        this.windows= windows;
        this.test=  test;
        this.render= new DefaultTableCellRenderer();
        this.render.setOpaque(false);
        this.img= new ImageIcon(getClass().getResource("/photo/11.jpg"));//导入图片
        this.jl= new JLabel(this.img);
        this.jl.setBounds(0,0,1200,700);
        String []str={
    
    "课程编号","课程名","学分"};
        this.table=new MyDefaultTable(str,22);
        this.jTable= new JTable(this.table);
        this.jScrollPane= new JScrollPane(this.jTable);
        //按钮
        this.jb_delete= new JButton("删除");this.jb_insert= new JButton("插入");
        this.jb_select= new JButton("查看");this.jb_update= new JButton("更新");
        this.jb_update.addActionListener(this); this.jb_select.addActionListener(this);
        this.jb_insert.addActionListener(this);this.jb_delete.addActionListener(this);
        this.jb_back= new JButton("return"); this.jb_back.addActionListener(this);
        // insert
        this.jt_insert= new JTextField(10);this.jt_insert2= new JTextField(10);this.jt_insert3= new JTextField(10);
        this.jl_insert = new JLabel("课程编号"); this.jl_insert2= new JLabel("课程名"); this.jl_insert3= new JLabel("学分");
        this.jt_insert.setOpaque(false); this.jt_insert2.setOpaque(false); this.jt_insert3.setOpaque(false);
        this.jl_insert.setOpaque(false); this.jl_insert2.setOpaque(false); this.jl_insert3.setOpaque(false);
        // update
        this.jt_update= new JTextField(10);this.jt_update2= new JTextField(10);this.jt_update3= new JTextField(10);
        this.jl_update = new JLabel("课程编号"); this.jl_update2= new JLabel("课程名");this.jl_update3= new JLabel("学分");
        this.jt_update.setOpaque(false); this.jt_update2.setOpaque(false); this.jt_update3.setOpaque(false);
        this.jl_update.setOpaque(false); this.jl_update2.setOpaque(false); this.jl_update3.setOpaque(false);
        // select
        // delete
        this.jt_delete= new JTextField(10);this.jt_delete2= new JTextField(10);this.jt_delete3= new JTextField(10);
        this.jl_delete= new JLabel("课程编号");this.jl_delete2= new JLabel("课程名");this.jl_delete3= new JLabel("学分");
        this.jt_delete.setOpaque(false); this.jt_delete2.setOpaque(false); this.jt_delete3.setOpaque(false);
        this.jl_delete.setOpaque(false); this.jl_delete2.setOpaque(false); this.jl_delete3.setOpaque(false);
        //容器
        this.jp_all= new JPanel();this.jp_insert= new JPanel();this.jp_select= new JPanel();this.jp_update= new JPanel();
        this.jp_delete= new JPanel();
        //布局
        this.setSize(1200,700);
        this.setLayout(new GridLayout(2,1));
        this.getContentPane().add(this.jScrollPane);
        this.jp_all.setLayout(new GridLayout(4,1));
        // jp_insert
        this.jp_insert.setOpaque(false);
    this.jp_insert.add(this.jb_insert);this.jp_insert.add(this.jl_insert);this.jp_insert.add(this.jt_insert);
        this.jp_insert.add(this.jl_insert2);this.jp_insert.add(this.jt_insert2);this.jp_insert.add(this.jl_insert3);this.jp_insert.add(this.jt_insert3);
        // jp_update
        this.jp_update.setOpaque(false);
        this.jp_update.add(this.jb_update);this.jp_update.add(this.jl_update);this.jp_update.add(this.jt_update);this.jp_update.add(this.jl_update2);
        this.jp_update.add(this.jt_update2);this.jp_update.add(this.jl_update3);this.jp_update.add(this.jt_update3);
        // jp_delete
        this.jp_delete.setOpaque(false);
        this.jp_delete.add(this.jb_delete);this.jp_delete.add(this.jl_delete);this.jp_delete.add(this.jt_delete);
        this.jp_delete.add(this.jl_delete2);this.jp_delete.add(this.jt_delete2);      this.jp_delete.add(this.jl_delete3);this.jp_delete.add(this.jt_delete3);
        //jp_select
        this.jp_select.setOpaque(false);
        this.jp_select.add(this.jb_select);
        this.jp_select.add(this.jb_back);
        //布局
        this.jp_all.setOpaque(false);       this.jp_all.add(this.jp_insert);this.jp_all.add(this.jp_update);this.jp_all.add(this.jp_delete);this.jp_all.add(this.jp_select);
      //jb_delete,jb_select,jb_update,jb_insert,jb_back;
         this.jb_delete.setContentAreaFilled(false); this.jb_select.setContentAreaFilled(false);
         this.jb_update.setContentAreaFilled(false); this.jb_insert.setContentAreaFilled(false);
         this.jb_back.setContentAreaFilled(false);
        //设置
        this.getLayeredPane().add(this.jl, new Integer(Integer.MIN_VALUE));
        ((JPanel)this.getContentPane()).setOpaque(false);
        for(int i=0;i<=2;i++){
    
    
            this.jTable.getColumn(str[i]).setCellRenderer(this.render);
        }
        this.jScrollPane.setOpaque(false);
        this.jTable.setOpaque(false);
        this.jScrollPane.getViewport().setOpaque(false);
        this.getContentPane().add(this.jp_all);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(Windows.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
    
    
        if(actionEvent.getSource().equals(this.jb_insert)) {
    
    
            try {
    
    
                String a = this.jt_insert.getText();
                String b = this.jt_insert2.getText();
                String c = this.jt_insert3.getText();
                this.test.insert_by_tea(a, b, c, "Course");
               JOptionPane.showMessageDialog(this,"插入成功!");
            }
            catch (Exception e){
    
    
                e.printStackTrace();
                JOptionPane.showMessageDialog(this,"ERROR!!");
            }
        }
        // delete 功能
        if(actionEvent.getSource().equals(this.jb_delete)) {
    
    
            String a = this.jt_delete.getText();
            int i;
            if (a.equals("")) {
    
    
                JOptionPane.showMessageDialog(this, "不能为空!!");
            } else {
    
    
                try {
    
    
                   i= this.test.delete_Course(a);
                   if(i==1){
    
    
                       JOptionPane.showMessageDialog(this, "delete ok!!!");
                   }
                   else {
    
    
                       JOptionPane.showMessageDialog(this,"没有该信息记录!!");
                   }
                } catch (Exception e) {
    
    
                    JOptionPane.showMessageDialog(this, "ERROR!!!");
                }
            }
        }
        // update
        if(actionEvent.getSource().equals(this.jb_update)) {
    
    
            try {
    
    
                String a = this.jt_update.getText();
                String b = this.jt_update2.getText();
                String c = this.jt_update3.getText();

               if( this.test.update_Course_tea(a, b, c)==1){
    
    
                    JOptionPane.showMessageDialog(this, "update ok!!");
                }
                else {
    
    
                    JOptionPane.showMessageDialog(this,"检查输入!");
                }
            } catch (Exception e) {
    
    
                 JOptionPane.showMessageDialog(this,"ERROR 检查输入信息");
            }
        }
        // select
        if(actionEvent.getSource().equals(this.jb_select)){
    
    
            for(int j=0;j<=18;j++){
    
    
                this.table.setValueAt("",j,0);
                this.table.setValueAt("",j,1);
                this.table.setValueAt("",j,2);
            }
            try{
    
    
                int i=0;
                this.test.get_sql_connection();
                this.test.ps=this.test.ct.prepareStatement("select * from Course");
                this.test.rs= this.test.ps.executeQuery();
                while (this.test.rs.next()){
    
    
                   this.table.setValueAt(this.test.rs.getString(1),i,0);
                   this.table.setValueAt(this.test.rs.getString(2),i,1);
                   this.table.setValueAt(this.test.rs.getString(3),i,2);
                    i++;
                }
            }catch (Exception e){
    
    
                JOptionPane.showMessageDialog(this,"检查输入信息!!!");
            }
            finally {
    
    
                this.test.close();
            }
        }
        if(actionEvent.getSource().equals(this.jb_back)){
    
    
            //返回
            this.setVisible(false);
            this.windows.setVisible(true);
        }
    }
}

在这里插入图片描述

五、程序讲解以及下载地址

视频讲解
下载地址具体的说明在README.md文件中

六、参考文档及素材来源

https://blog.csdn.net/wyf2017/java/article/details/78831744

图片来源于wallheaven

七、实验总结

这个实验应用到了数据库的基本知识以及Java的一些内容,我个人觉得最大的问题是代码的重用率不高,不过自己写一个这样的程序收获还不小的。

猜你喜欢

转载自blog.csdn.net/weixin_44724691/article/details/106526109