javaGUI实现学生图书管理简单实例

本文实例为大家分享了java GUI学生图书管理的具体代码,供大家参考,具体内容如下

站在用户的角度思考问题,与客户深入沟通,找到阿拉尔网站设计与阿拉尔网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、网站建设、企业官网、英文网站、手机端网站、网站推广、国际域名空间、雅安服务器托管、企业邮箱。业务覆盖阿拉尔地区。

- MySQL数据库建表:

1.book表

java GUI实现学生图书管理简单实例

2.bs借书记录表

java GUI实现学生图书管理简单实例

3.std学生表

java GUI实现学生图书管理简单实例

4.dl登录用户表

.java GUI实现学生图书管理简单实例

- 列表内容

1.databd.java //程序入口及登录验证

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class databd extends JFrame implements ActionListener{  
  static frameDemo a=null;
  String getuser="";
  String getpd="";
  String sql = "select * from dl where id='";
  String sql1 = "select password from dl where password='";
  JButton btnNewButton = new JButton("登录");
  JButton btnNewButton_1 = new JButton("注册"); 
  JTextField show=new JTextField("\t  请选择按钮",10);  
  private JPanel contentPane;
  private JTextField userField;
  private JPasswordField pwdField;
  private Statement statement = null; //查询账号
  private Statement statement2 = null; //注册账户  
  private static databd frame;      //
  ResultSet rst2=null;
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          frame = new databd();
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }
  public databd() {
    String driver = "com.mysql.jdbc.Driver";   
    String url = "jdbc:mysql://localhost:8088/library?useUnicode=true&characterEncoding=utf-8&useSSL=false"; 
    String user = "root";        
    String password = "11111";
    try{
    Class.forName(driver);    
    Connection conn = DriverManager.getConnection(url, user, password);
    Connection conn2 = DriverManager.getConnection(url, user, password);
    statement = conn.createStatement();
    statement2 = conn2.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    rst2=statement2.executeQuery("select * from dl");
    show.setEditable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(250, 200);       
    setLocationRelativeTo(null);  

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(null);
    add(contentPane);

    JLabel lblUsername = new JLabel("账号");
    lblUsername.setBounds(12, 13, 54, 15);
    contentPane.add(lblUsername);

    JLabel lblPassword = new JLabel("密码");
    lblPassword.setBounds(12, 38, 54, 15);
    contentPane.add(lblPassword);

    userField = new JTextField();
    userField.setBounds(76, 10, 144, 21);
    contentPane.add(userField);
    userField.setColumns(10);

    pwdField = new JPasswordField();
    pwdField.setEchoChar('*');  //密码回显字符
    pwdField.setBounds(76, 35, 144, 21);
    contentPane.add(pwdField);
    pwdField.setColumns(10);
    add(show,BorderLayout.SOUTH);

    btnNewButton.addActionListener(this); //登录
    btnNewButton.setBounds(10, 92, 93, 23);
    contentPane.add(btnNewButton);

    btnNewButton_1.addActionListener(this); //注册
    btnNewButton_1.setBounds(127, 92, 93, 23);
    contentPane.add(btnNewButton_1);

    show.addActionListener(this);
    }catch(ClassNotFoundException e){System.out.print("不能找到驱动器");
    }catch(Exception e){System.out.print("出现错误");e.printStackTrace();}

    } 
  public void actionPerformed(ActionEvent e) {
    getuser=userField.getText().trim()+"'";
    String pd=String.valueOf(pwdField.getPassword());
    try{        
      ResultSet rs=statement.executeQuery(sql+getuser);
      if(e.getSource()==btnNewButton){ //登录
         if(rs.next()){          
          if(rs.getString(2).equals(pd)){
            frame.setVisible(false);
            JOptionPane.showMessageDialog(null, "登录成功");
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            a=frameDemo.a; //生成静态对象
            }else show.setText("\t  密码错误");
          }
          else 
          show.setText("\t  账号不存在");
        }
      else if(e.getSource()==btnNewButton_1){ //注册
          if(userField.getText().equals("")){
            show.setText("\t 注册账号不能为空");}
          else {
            if((String.valueOf(pwdField.getPassword())).equals(""))
              show.setText("\t 注册密码不能为空");
            else {register();}
            }
      }
    }catch(Exception c){c.printStackTrace();}         
   }
   private void register(){  
     String name=userField.getText(),
         passwd=String.valueOf(pwdField.getPassword());
     try{
     rst2.moveToInsertRow();
     rst2.updateString(1, name);
     rst2.updateString(2, passwd);
     rst2.insertRow();
     rst2.moveToCurrentRow();
     show.setText("\t  注册成功");
     }catch(Exception e){show.setText("\t  注册失败");}
   }
}

2.frameDemo //主框架,处理事件交给addinformation类

package database;
import javax.swing.*;
import javax.swing.border.TitledBorder;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class frameDemo extends JFrame implements ActionListener{
  public static frameDemo a=new frameDemo(); 
  JPanel panel=new JPanel(),
      panel1=new JPanel(),
      panel2=new JPanel(),
      panel3=new JPanel(),
      panel4=new JPanel(),
      panel5=new JPanel();
  String[] adds={"请选择操作","添加馆藏记录","添加借书记录","添加学生记录"};
  String[] deletes={"请选择操作","删除馆藏记录","删除借书记录","删除学生记录"};
  String[] changes={"请选择操作","修改库存记录","修改应还日期"};
  String[] querys={"请选择操作","查询书籍","查询书号","查询学生借书记录","查询学生信息"};
  String[] shows={"请选择操作","显示所有馆藏记录","显示借书记录","显示学生记录"};
  String[] record=new String[4];
  JTextArea ta=new JTextArea("-->欢迎使用系统\n",11,60);
  JScrollPane pane=new JScrollPane(ta);
  Connection conn=null;
  Statement stmt=null;
  Statement stmt1=null;
  Statement stmt2=null;
  ResultSet rst=null;
  ResultSet rst1=null;
  ResultSet rst2=null;
  static boolean key;
  private JFrame big=new JFrame("请选择操作");
  private JComboBox add=new JComboBox(adds);
  private JComboBox delete=new JComboBox(deletes);
  private JComboBox change=new JComboBox(changes);
  private JComboBox query=new JComboBox(querys);
  private JComboBox show=new JComboBox(shows);
  public frameDemo(){
    super("图书馆管理系统");
    try{
      Class.forName("com.mysql.jdbc.Driver");
      String url="jdbc:mysql://localhost:8088/library?useUnicode=true&characterEncoding=utf-8&useSSL=false";
      String user="root";
      String password="11111";
      conn=DriverManager.getConnection(url, user, password);     
      panel4.setLayout(new GridLayout(3,1,30,10));
      panel4.setBorder(new TitledBorder("选择管理:"));
      panel1.add(new JLabel("添加记录:"));
      panel1.add(add);
      panel1.add(new JLabel("删除记录:"));
      panel1.add(delete);
      panel2.add(new JLabel("修改记录:"));
      panel2.add(change);
      panel2.add(new JLabel("查询记录:"));
      panel2.add(query);
      panel3.add(new JLabel("显示记录:"));
      panel3.add(show);
      panel4.add(panel1);panel4.add(panel2);panel4.add(panel3);    
      panel5.add(pane);
      panel.add(panel5,BorderLayout.SOUTH);
      panel.add(panel4,BorderLayout.CENTER); 
      add(panel);
      //pack();
      stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);//可滚可更      
      stmt1=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);     
      stmt2=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

      add.addActionListener(this);
      delete.addActionListener(this);
      change.addActionListener(this);
      query.addActionListener(this);
      show.addActionListener(this);

      setSize(470,440);
      //setResizable(false);
      setLocationRelativeTo(null);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }catch(Exception e){e.printStackTrace();}

  }
  public void actionPerformed(ActionEvent ae){
    try{
      //if(key){   //数据库非空
       if(add.getSelectedIndex()==1){         
        ta.append("-->添加馆藏记录\n");
        addinformation a1=new addinformation("图书编号", "书名", "库存",1);       
       }
       else if(add.getSelectedIndex()==2){
        ta.append("-->添加借书记录\n");
        addinformation a1=new addinformation("图书编号", "学号",2);
       }
       else if(add.getSelectedIndex()==3){
         ta.append("-->添加学生记录\n");
         addinformation a1=new addinformation("学生学号", "学生姓名",3);
       }
       add.setSelectedItem("请选择操作");

       if(delete.getSelectedIndex()==1){
        ta.append("-->删除馆藏记录\n");
        addinformation a1=new addinformation("图书编号",4);
       }
       else if(delete.getSelectedIndex()==2){
         ta.append("-->删除借书记录\n");
         addinformation a1=new addinformation("图书编号","学生学号",5);
       }
       else if(delete.getSelectedIndex()==3){
        ta.append("-->删除学生记录\n");
        addinformation a1=new addinformation("学生学号",6);
       }
       delete.setSelectedItem("请选择操作");

       if(change.getSelectedIndex()==1){
        ta.append("-->修改库存记录\n");
        addinformation a1=new addinformation("图书编号","库存数目",7);
       }
       else if(change.getSelectedIndex()==2){
        ta.append("-->修改应还日期\n");
        addinformation a1=new addinformation("图书编号","学生学号","应还日期",8);  
       }
       change.setSelectedItem("请选择操作");

       if(query.getSelectedIndex()==1){
        ta.append("-->按书名查询\n");
        addinformation a1=new addinformation("书名",9);  
       }
       else if(query.getSelectedIndex()==2){
        ta.append("-->按书籍编号查询\n");
        addinformation a1=new addinformation("书籍编号",10);
       }
       else if(query.getSelectedIndex()==3){
        ta.append("-->查询学生借书记录\n");
        addinformation a1=new addinformation("学生学号",11);
       }
       else if(query.getSelectedIndex()==4){
        ta.append("-->查询学生信息\n");
        addinformation a1=new addinformation("学生学号",12);  
       }
       query.setSelectedItem("请选择操作");

       if(show.getSelectedIndex()==1){
         ta.append("-->显示所有馆藏记录\n");
         try{
          rst=stmt.executeQuery("SELECT * FROM book");
          rst.first();
          ta.append("------------------------------------\n");
          int id=rst.getInt(1);
          String name=rst.getString(2);
          int num=rst.getInt(3); 
          ta.append("\t书籍编号\t书籍名称\t在馆库存\n");
          ta.append("\t"+id+"\t"+name+"\t"+num+"\n");
          while(rst.next()){
              id=rst.getInt(1);
              name=rst.getString(2);
              num=rst.getInt(3);
              ta.append("\t"+id+"\t"+name+"\t"+num+"\n");
            }//执行后游标指向最后一行的后一个位置
            rst.first();
            ta.append("------------------------------------\n");
          }catch(Exception e){ta.append("-->显示错误");}
       }
       else if(show.getSelectedIndex()==2){
         ta.append("-->显示借书记录\n");        
         rst1=stmt1.executeQuery("SELECT * FROM bs");
         Calendar calendar =new GregorianCalendar();
         try{
          rst1.first();
          ta.append("----------------------------------------------------\n");
          int bookid=rst1.getInt(2);
          int stdid=rst1.getInt(3);
          Date x=rst1.getDate(4);     
           calendar.setTime(x);
           calendar.add(calendar.DATE, 30);
           java.util.Date utilDate = (java.util.Date)calendar.getTime();
           utilDate = (java.util.Date)calendar.getTime();
           Date newDate =new Date(utilDate.getTime());  
          ta.append("\t书籍编号\t学生学号\t借书日期\t\t应还日期\n");
          ta.append("\t"+bookid+"\t"+stdid+"\t"+x+"\t"+newDate+"\n");       
          while(rst1.next()){
              bookid=rst1.getInt(2);
              stdid=rst1.getInt(3);
              x=rst1.getDate(4);
              calendar.setTime(x);
              calendar.add(calendar.DATE, 30);
              utilDate = (java.util.Date)calendar.getTime();
              utilDate = (java.util.Date)calendar.getTime();
              newDate =new Date(utilDate.getTime());
              ta.append("\t"+bookid+"\t"+stdid+"\t"+x+"\t"+newDate+"\n");
            }//执行后游标指向最后一行的后一个位置
            rst1.first();
            ta.append("----------------------------------------------------\n");
          }catch(Exception e){ta.append("-->显示错误");e.printStackTrace();}
       }
       else if(show.getSelectedIndex()==3){
         ta.append("-->显示学生记录\n");
         try{
          rst2=stmt2.executeQuery("SELECT * FROM std");
          rst2.first();
          ta.append("------------------------------------\n");
          int id=rst2.getInt(1);
          String name=rst2.getString(2);
          ta.append("\t学生学号\t学生姓名\n");
          ta.append("\t"+id+"\t"+name+"\t"+"\n");
          while(rst2.next()){
              id=rst2.getInt(1);
              name=rst2.getString(2);
              ta.append("\t"+id+"\t"+name+"\t"+"\n");
            }//执行后游标指向最后一行的后一个位置
          rst2.first();
          ta.append("------------------------------------\n");
          }catch(Exception e){ta.append("-->显示错误");}
       }
       show.setSelectedItem("请选择操作");
       ta.setCaretPosition(ta.getText().length()); //滚动条保持在最后一条    
     }catch(Exception e){e.printStackTrace();} 
  }
  public void showMessage(){
    try{
      int a=rst.getRow();
      ta.append(a+"\n");
      rst.first();
      ta.append("---------------------------\n");
      String id=rst.getString(1),
          name=rst.getString(2),
          brand=rst.getString(3),
          price=rst.getString(4),
          stock=rst.getString(5);     
      ta.append(id+" "+name+" "+brand+" "+price+" "+stock+"\n");
      while(rst.next()){
        id=rst.getString(1);
        name=rst.getString(2);
        brand=rst.getString(3);
        price=rst.getString(4);
        stock=rst.getString(5);
        ta.append(id+" "+name+" "+brand+" "+price+" "+stock+"\n");
      }//执行后游标指向最后一行的后一个位置
      rst.absolute(a);
      ta.append("---------------------------\n");
    }catch(Exception e){ta.append("-->显示错误");}
  }
// public static void main(String[] args){   
//       try{
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     
//       }catch(Exception e){System.out.print("1111111111");}
//       a=new frameDemo();
//     }

  }

3.addinformation.java //处理事件

package database;
import javax.swing.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class addinformation extends JFrame implements ActionListener{
  JButton sure=new JButton("确定"),
      quxiao=new JButton("取消");
  JTextField jtf1=new JTextField(10),
      jtf2=new JTextField(10),
      jtf3=new JTextField(10);
  Connection conn=null;
  static{
    try{
    Class.forName("com.mysql.jdbc.Driver");
    }catch(Exception e){e.printStackTrace();}    
  }
  int x=0;
  public Connection getConnection() throws SQLException{
    if(conn==null){
      conn=DriverManager.getConnection("jdbc:mysql://localhost:8088/library?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","11111");
    }
    return conn;
  }
  addinformation(String s1,String s2,String s3,int m){
    JPanel p=new JPanel();
    p.setLayout(new FlowLayout()); 
    p.add(new JLabel(s1));
    p.add(jtf1);
    p.add(new JLabel(s2));
    p.add(jtf2);
    p.add(new JLabel(s3));
    p.add(jtf3);  
    p.add(sure);
    p.add(quxiao);
    add(p);
    if(m==8){
      JOptionPane.showMessageDialog(null, "图书日期应为 XXXX-XX-XX 格式!","警告",JOptionPane.WARNING_MESSAGE);
    }
    setSize(500, 100); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       
    setLocationRelativeTo(null); 
    sure.addActionListener(this);
    quxiao.addActionListener(this);
    x=m;
  }
  addinformation(String s1,String s2,int m){   
    JPanel p=new JPanel();
    p.setLayout(new FlowLayout()); 
    p.add(new JLabel(s1));
    p.add(jtf1);
    p.add(new JLabel(s2));
    p.add(jtf2);  
    p.add(sure);
    p.add(quxiao);
    add(p);
    setSize(500, 100); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       
    setLocationRelativeTo(null); 
    sure.addActionListener(this);
    quxiao.addActionListener(this);
    x=m;
  }
  addinformation(String s1,int m){
    JPanel p=new JPanel();
    p.setLayout(new FlowLayout()); 
    p.add(new JLabel(s1));
    p.add(jtf1);
    if(m==4){
    JOptionPane.showMessageDialog(null, "若存在借阅记录则不能删除书籍信息!","警告",JOptionPane.WARNING_MESSAGE);
    }
    if(m==6){
    JOptionPane.showMessageDialog(null, "若学生存在借阅记录则不能删除该学生信息!","警告",JOptionPane.WARNING_MESSAGE);
    }
    p.add(sure);
    p.add(quxiao);
    add(p); 
    setSize(500, 80); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       
    setLocationRelativeTo(null); 
    sure.addActionListener(this);
    quxiao.addActionListener(this);
    x=m;
  }

  public void actionPerformed(ActionEvent ae){
    if(ae.getSource()==sure){
      if(x==1){  //添加 馆藏记录
        String sql="INSERT INTO book VALUES(?,?,?)";
        try(  
        Connection conn=getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql)){
        int id=Integer.parseInt(jtf1.getText());
        String name=jtf2.getText();
        int num=Integer.parseInt(jtf3.getText());
        stmt.setInt(1, id);
        stmt.setString(2, name);
        stmt.setInt(3, num);
        stmt.executeUpdate();
        frameDemo.a.ta.append("-->添加成功\n");      
        }catch(Exception x){frameDemo.a.ta.append("-->插入出错\n");x.printStackTrace();}
        addinformation.this.dispose(); 
      }
      if(x==2){  //添加借书记录
          String sql="INSERT INTO bs(bookid,stdid,date) VALUES(?,?,?)";
          String sql1="SELECT id,num FROM book WHERE id=?";
          String sql2="SELECT id FROM std WHERE id=?";
          try{
          Connection conn1=getConnection();
          PreparedStatement stmt1=conn1.prepareStatement(sql1,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          stmt1.setInt(1,Integer.parseInt(jtf1.getText()));          
          ResultSet rst1=stmt1.executeQuery();//找有没有这本书和库存是否为0
          if(rst1.next()&&(rst1.getInt(2)>0)){
            int n=rst1.getInt(2);n-=1;
            int m=rst1.getInt(1);
            rst1.updateInt(1, m);
            rst1.updateInt(2, n);
            rst1.updateRow();
            Connection conn2=getConnection();
            PreparedStatement stmt2=conn2.prepareStatement(sql2);
            stmt2.setInt(1,Integer.parseInt(jtf2.getText()));
            ResultSet rst2=stmt2.executeQuery();//有人
            if(rst2.next()){
              Connection conn=getConnection();
              PreparedStatement stmt=conn.prepareStatement(sql);
              int bookid=Integer.parseInt(jtf1.getText());
              int stdid=Integer.parseInt(jtf2.getText()); 
              stmt.setInt(1, bookid);
              stmt.setInt(2, stdid);
              stmt.setDate(3, new Date(System.currentTimeMillis()));
              stmt.executeUpdate();
              frameDemo.a.ta.append("-->添加成功\n");
            }
            else{frameDemo.a.ta.append("-->无该学生记录,不能添加借阅记录\n");}
          }else{frameDemo.a.ta.append("-->无此书或已全部借出\n");}              
          }catch(Exception x){frameDemo.a.ta.append("-->插入出错\n");x.printStackTrace();}
          addinformation.this.dispose();       
       }
      if(x==3){  //添加学生记录
        String sql="INSERT INTO std VALUES(?,?)";
        try(  
        Connection conn=getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql)){
        int id=Integer.parseInt(jtf1.getText());
        String name=jtf2.getText(); 
        stmt.setInt(1, id);
        stmt.setString(2, name);
        stmt.executeUpdate();
        frameDemo.a.ta.append("-->添加成功\n");    
        }catch(Exception x){frameDemo.a.ta.append("-->插入出错\n");x.printStackTrace();}
        addinformation.this.dispose();       
      }
      if(x==4){  //删除图书记录
        String sql="DELETE FROM book WHERE id=?";
        String sql1="SELECT * FROM bs WHERE bookid=?";
        try{
        Connection conn1=getConnection();
        PreparedStatement stmt1=conn1.prepareStatement(sql1);
        stmt1.setInt(1,Integer.parseInt(jtf1.getText()));
        ResultSet rst1=stmt1.executeQuery();
        if(rst1.next()){  //借阅记录中有书
          JOptionPane.showMessageDialog(null, "借阅记录中有该书的借阅记录!","警告",JOptionPane.WARNING_MESSAGE);
        }
        else{    //借阅记录无书,可以删掉
          Connection conn=getConnection();
          PreparedStatement stmt=conn.prepareStatement(sql);
          int id=Integer.parseInt(jtf1.getText());
          stmt.setInt(1, id);
          int t=stmt.executeUpdate();
          if(t==0){
          frameDemo.a.ta.append("-->删除出错\n");
          JOptionPane.showMessageDialog(null, "不存在该图书信息!","警告",JOptionPane.WARNING_MESSAGE);
          }
          else frameDemo.a.ta.append("-->删除记录成功\n");
         }          
        }catch(Exception x){frameDemo.a.ta.append("-->删除失败\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==5){  //删除借书记录
        String sql="DELETE FROM bs WHERE bookid=? AND stdid=?";
        String sql1="SELECT id,num FROM book WHERE id=?";
        try{      
        Connection conn=getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql);
        int bookid=Integer.parseInt(jtf1.getText());
        int stdid=Integer.parseInt(jtf2.getText());
        stmt.setInt(1, bookid);
        stmt.setInt(2, stdid);
        int t=stmt.executeUpdate();
        if(t!=0){
          Connection conn1=getConnection();
          PreparedStatement stmt1=conn1.prepareStatement(sql1,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          stmt1.setInt(1,Integer.parseInt(jtf1.getText()));  
          ResultSet rst1=stmt1.executeQuery();rst1.next();
          int m=rst1.getInt(1);
          int n=rst1.getInt(2);n+=1;
          rst1.updateInt(1, m);
          rst1.updateInt(2, n);
          rst1.updateRow();
          frameDemo.a.ta.append("-->删除借阅记录成功\n");
        }
        else{
          frameDemo.a.ta.append("-->删除出错\n");
          JOptionPane.showMessageDialog(null, "不存在这条借阅记录!","警告",JOptionPane.WARNING_MESSAGE);
        }           
        }catch(Exception x){frameDemo.a.ta.append("-->删除出错\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==6){ //删除学生记录
        String sql="DELETE FROM std WHERE id=?";
        String sql1="SELECT * FROM bs WHERE stdid=?";
        try {
          Connection conn1=getConnection();
          PreparedStatement stmt1=conn1.prepareStatement(sql1);
          stmt1.setInt(1,Integer.parseInt(jtf1.getText()));
          ResultSet rst1=stmt1.executeQuery();
          if(rst1.next()){  //借阅记录中有该人
            JOptionPane.showMessageDialog(null, "借阅记录中有该人的借阅记录!","警告",JOptionPane.WARNING_MESSAGE);
          }
          else{
            Connection conn=getConnection();
            PreparedStatement stmt=conn.prepareStatement(sql);
            int stdid=Integer.parseInt(jtf1.getText());
            stmt.setInt(1, stdid);
            int t=stmt.executeUpdate();
            if(t==0){
              frameDemo.a.ta.append("-->删除出错\n");
              JOptionPane.showMessageDialog(null, "不存在该学生记录!","警告",JOptionPane.WARNING_MESSAGE);
              }
            else frameDemo.a.ta.append("-->删除记录成功\n");
            }             
        }catch(Exception x){frameDemo.a.ta.append("-->删除出错\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==7){  //修改图书库存记录
        String sql="UPDATE book SET num=? WHERE id=?";
        try(  
        Connection conn=getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql)){
        int num=Integer.parseInt(jtf2.getText());
        int bookid=Integer.parseInt(jtf1.getText());
        stmt.setInt(1, num);
        stmt.setInt(2, bookid);
        int t=stmt.executeUpdate();
        if(t==0){
          frameDemo.a.ta.append("-->修改出错\n");
          JOptionPane.showMessageDialog(null, "不存在该图书记录!","警告",JOptionPane.WARNING_MESSAGE);
        }
        else frameDemo.a.ta.append("-->修改记录成功\n");    
        }catch(Exception x){frameDemo.a.ta.append("-->修改出错\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==8){  //修改应还日期
        String sql="UPDATE bs SET date=? WHERE bookid=? AND stdid=?";
        try(  
        Connection conn=getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql)){
        Date date = Date.valueOf(jtf3.getText());
        Calendar calendar =new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -30);
        java.util.Date utilDate = (java.util.Date)calendar.getTime();
        utilDate = (java.util.Date)calendar.getTime();
        Date newDate =new Date(utilDate.getTime());
        int stdid=Integer.parseInt(jtf2.getText());
        int bookid=Integer.parseInt(jtf1.getText());
        stmt.setDate(1, newDate);        
        stmt.setInt(2, bookid);
        stmt.setInt(3, stdid);
        int t=stmt.executeUpdate();
        if(t==0){
          frameDemo.a.ta.append("-->修改出错\n");
          JOptionPane.showMessageDialog(null, "不存在该借书记录!","警告",JOptionPane.WARNING_MESSAGE);
        }
        else frameDemo.a.ta.append("-->修改记录成功\n");    
        }catch(Exception x){frameDemo.a.ta.append("-->修改出错\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==9){  //按书名查询
        String sql="SELECT * from book WHERE name=?";
        ResultSet rst=null;
        try(  
        Connection conn=getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql)){
        String name=jtf1.getText();
        stmt.setString(1, name);        
        rst=stmt.executeQuery();
        if(rst.next()){
          frameDemo.a.ta.append("---------------------------------------\n");
          frameDemo.a.ta.append("\t图书编号\t书名\t在馆数\n");
          frameDemo.a.ta.append("\t"+rst.getInt(1)+"\t"+rst.getString(2)+"\t"+rst.getInt(3)+"\n");
          frameDemo.a.ta.append("---------------------------------------\n");
        }
        else{
          frameDemo.a.ta.append("-->查询出错\n");
          JOptionPane.showMessageDialog(null, "不存在该书!","警告",JOptionPane.WARNING_MESSAGE);  
        }
        }catch(Exception x){frameDemo.a.ta.append("-->查询出错\n");x.printStackTrace();}
        addinformation.this.dispose();
      }  
      if(x==10){  //按书籍编号查询
        String sql="SELECT * from book WHERE id=?";
        ResultSet rst=null;
        try(  
        Connection conn=getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql)){
        stmt.setInt(1, Integer.parseInt(jtf1.getText()));        
        rst=stmt.executeQuery();
        if(rst.next()){
          frameDemo.a.ta.append("---------------------------------------\n");
          frameDemo.a.ta.append("\t图书编号\t书名\t在馆数\n");
          frameDemo.a.ta.append("\t"+rst.getInt(1)+"\t"+rst.getString(2)+"\t"+rst.getInt(3)+"\n");
          frameDemo.a.ta.append("---------------------------------------\n");
        }
        else{
          frameDemo.a.ta.append("-->查询出错\n");
          JOptionPane.showMessageDialog(null, "不存在该书!","警告",JOptionPane.WARNING_MESSAGE);  
        }
        }catch(Exception x){frameDemo.a.ta.append("-->查询出错\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==11){  //按学生借书记录查询
        String sql="SELECT bookid,stdid,date from bs WHERE stdid=?";
        ResultSet rst=null;
        try(  
        Connection conn=getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql)){
        stmt.setInt(1, Integer.parseInt(jtf1.getText()));        
        rst=stmt.executeQuery();
        Calendar calendar =new GregorianCalendar();
        if(rst.next()){
          frameDemo.a.ta.append("----------------------------------------------------\n");
          frameDemo.a.ta.append("\t图书编号\t学生学号\t借书时间\t\t应还时间\n");
          Date x=rst.getDate(3);        
           calendar.setTime(x);
           calendar.add(calendar.DATE, 30);
           java.util.Date utilDate = (java.util.Date)calendar.getTime();
           utilDate = (java.util.Date)calendar.getTime();
           Date newDate =new Date(utilDate.getTime());
           frameDemo.a.ta.append("\t"+rst.getInt(1)+"\t"+rst.getInt(2)+"\t"+x+"\t"+newDate+"\n");
          while(rst.next()){       
           x=rst.getDate(3);
           calendar.setTime(x);
           calendar.add(calendar.DATE, 30);
           utilDate = (java.util.Date)calendar.getTime();
           utilDate = (java.util.Date)calendar.getTime();
           newDate =new Date(utilDate.getTime());
           frameDemo.a.ta.append("\t"+rst.getInt(1)+"\t"+rst.getInt(2)+"\t"+x+"\t"+newDate+"\n");
           } 
          frameDemo.a.ta.append("----------------------------------------------------\n");
          }
          else{
          frameDemo.a.ta.append("-->查询出错\n");
          JOptionPane.showMessageDialog(null, "不存在该借书记录!","警告",JOptionPane.WARNING_MESSAGE);  
         }
        }catch(Exception x){frameDemo.a.ta.append("-->查询出错\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==12){  //查询学生信息
        String sql="SELECT * from std WHERE id=?";
        ResultSet rst=null;
        try(  
        Connection conn=getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql)){
        stmt.setInt(1, Integer.parseInt(jtf1.getText()));        
        rst=stmt.executeQuery();
        if(rst.next()){
          frameDemo.a.ta.append("---------------------------------------\n");
          frameDemo.a.ta.append("\t学生学号\t学生姓名\t\n");      
          frameDemo.a.ta.append("\t"+rst.getInt(1)+"\t"+rst.getString(2)+"\n");
          frameDemo.a.ta.append("---------------------------------------\n");
          }
          else{
          frameDemo.a.ta.append("-->查询出错\n");
          JOptionPane.showMessageDialog(null, "不存在该学生!","警告",JOptionPane.WARNING_MESSAGE);   
         }
        }catch(Exception x){frameDemo.a.ta.append("-->查询出错\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
    }
    if(ae.getSource()==quxiao){
      addinformation.this.dispose();
      frameDemo.a.ta.append("-->操作取消\n");
    }

  }
}

java GUI实现学生图书管理简单实例 java GUI实现学生图书管理简单实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网页标题:javaGUI实现学生图书管理简单实例
URL网址:http://ybzwz.com/article/ggooep.html