溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單實(shí)例

發(fā)布時(shí)間:2020-09-30 09:50:02 來(lái)源:腳本之家 閱讀:283 作者:wqdsb 欄目:編程語(yǔ)言

本文實(shí)例為大家分享了java GUI學(xué)生圖書(shū)管理的具體代碼,供大家參考,具體內(nèi)容如下

- mysql數(shù)據(jù)庫(kù)建表:

1.book表

java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單實(shí)例

2.bs借書(shū)記錄表

java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單實(shí)例

3.std學(xué)生表

java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單實(shí)例

4.dl登錄用戶表

.java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單實(shí)例

- 列表內(nèi)容

1.databd.java //程序入口及登錄驗(yàn)證

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("注冊(cè)"); 
  JTextField show=new JTextField("\t  請(qǐng)選擇按鈕",10);  
  private JPanel contentPane;
  private JTextField userField;
  private JPasswordField pwdField;
  private Statement statement = null; //查詢賬號(hào)
  private Statement statement2 = null; //注冊(cè)賬戶  
  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("賬號(hào)");
    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); //注冊(cè)
    btnNewButton_1.setBounds(127, 92, 93, 23);
    contentPane.add(btnNewButton_1);

    show.addActionListener(this);
    }catch(ClassNotFoundException e){System.out.print("不能找到驅(qū)動(dòng)器");
    }catch(Exception e){System.out.print("出現(xiàn)錯(cuò)誤");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; //生成靜態(tài)對(duì)象
            }else show.setText("\t  密碼錯(cuò)誤");
          }
          else 
          show.setText("\t  賬號(hào)不存在");
        }
      else if(e.getSource()==btnNewButton_1){ //注冊(cè)
          if(userField.getText().equals("")){
            show.setText("\t 注冊(cè)賬號(hào)不能為空");}
          else {
            if((String.valueOf(pwdField.getPassword())).equals(""))
              show.setText("\t 注冊(cè)密碼不能為空");
            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  注冊(cè)成功");
     }catch(Exception e){show.setText("\t  注冊(cè)失敗");}
   }
}

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={"請(qǐng)選擇操作","添加館藏記錄","添加借書(shū)記錄","添加學(xué)生記錄"};
  String[] deletes={"請(qǐng)選擇操作","刪除館藏記錄","刪除借書(shū)記錄","刪除學(xué)生記錄"};
  String[] changes={"請(qǐng)選擇操作","修改庫(kù)存記錄","修改應(yīng)還日期"};
  String[] querys={"請(qǐng)選擇操作","查詢書(shū)籍","查詢書(shū)號(hào)","查詢學(xué)生借書(shū)記錄","查詢學(xué)生信息"};
  String[] shows={"請(qǐng)選擇操作","顯示所有館藏記錄","顯示借書(shū)記錄","顯示學(xué)生記錄"};
  String[] record=new String[4];
  JTextArea ta=new JTextArea("-->歡迎使用系統(tǒng)\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("請(qǐng)選擇操作");
  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("圖書(shū)館管理系統(tǒng)");
    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){   //數(shù)據(jù)庫(kù)非空
       if(add.getSelectedIndex()==1){         
        ta.append("-->添加館藏記錄\n");
        addinformation a1=new addinformation("圖書(shū)編號(hào)", "書(shū)名", "庫(kù)存",1);       
       }
       else if(add.getSelectedIndex()==2){
        ta.append("-->添加借書(shū)記錄\n");
        addinformation a1=new addinformation("圖書(shū)編號(hào)", "學(xué)號(hào)",2);
       }
       else if(add.getSelectedIndex()==3){
         ta.append("-->添加學(xué)生記錄\n");
         addinformation a1=new addinformation("學(xué)生學(xué)號(hào)", "學(xué)生姓名",3);
       }
       add.setSelectedItem("請(qǐng)選擇操作");

       if(delete.getSelectedIndex()==1){
        ta.append("-->刪除館藏記錄\n");
        addinformation a1=new addinformation("圖書(shū)編號(hào)",4);
       }
       else if(delete.getSelectedIndex()==2){
         ta.append("-->刪除借書(shū)記錄\n");
         addinformation a1=new addinformation("圖書(shū)編號(hào)","學(xué)生學(xué)號(hào)",5);
       }
       else if(delete.getSelectedIndex()==3){
        ta.append("-->刪除學(xué)生記錄\n");
        addinformation a1=new addinformation("學(xué)生學(xué)號(hào)",6);
       }
       delete.setSelectedItem("請(qǐng)選擇操作");

       if(change.getSelectedIndex()==1){
        ta.append("-->修改庫(kù)存記錄\n");
        addinformation a1=new addinformation("圖書(shū)編號(hào)","庫(kù)存數(shù)目",7);
       }
       else if(change.getSelectedIndex()==2){
        ta.append("-->修改應(yīng)還日期\n");
        addinformation a1=new addinformation("圖書(shū)編號(hào)","學(xué)生學(xué)號(hào)","應(yīng)還日期",8);  
       }
       change.setSelectedItem("請(qǐng)選擇操作");

       if(query.getSelectedIndex()==1){
        ta.append("-->按書(shū)名查詢\n");
        addinformation a1=new addinformation("書(shū)名",9);  
       }
       else if(query.getSelectedIndex()==2){
        ta.append("-->按書(shū)籍編號(hào)查詢\n");
        addinformation a1=new addinformation("書(shū)籍編號(hào)",10);
       }
       else if(query.getSelectedIndex()==3){
        ta.append("-->查詢學(xué)生借書(shū)記錄\n");
        addinformation a1=new addinformation("學(xué)生學(xué)號(hào)",11);
       }
       else if(query.getSelectedIndex()==4){
        ta.append("-->查詢學(xué)生信息\n");
        addinformation a1=new addinformation("學(xué)生學(xué)號(hào)",12);  
       }
       query.setSelectedItem("請(qǐng)選擇操作");

       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書(shū)籍編號(hào)\t書(shū)籍名稱\t在館庫(kù)存\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");
            }//執(zhí)行后游標(biāo)指向最后一行的后一個(gè)位置
            rst.first();
            ta.append("------------------------------------\n");
          }catch(Exception e){ta.append("-->顯示錯(cuò)誤");}
       }
       else if(show.getSelectedIndex()==2){
         ta.append("-->顯示借書(shū)記錄\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書(shū)籍編號(hào)\t學(xué)生學(xué)號(hào)\t借書(shū)日期\t\t應(yīng)還日期\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");
            }//執(zhí)行后游標(biāo)指向最后一行的后一個(gè)位置
            rst1.first();
            ta.append("----------------------------------------------------\n");
          }catch(Exception e){ta.append("-->顯示錯(cuò)誤");e.printStackTrace();}
       }
       else if(show.getSelectedIndex()==3){
         ta.append("-->顯示學(xué)生記錄\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學(xué)生學(xué)號(hào)\t學(xué)生姓名\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");
            }//執(zhí)行后游標(biāo)指向最后一行的后一個(gè)位置
          rst2.first();
          ta.append("------------------------------------\n");
          }catch(Exception e){ta.append("-->顯示錯(cuò)誤");}
       }
       show.setSelectedItem("請(qǐng)選擇操作");
       ta.setCaretPosition(ta.getText().length()); //滾動(dòng)條保持在最后一條    
     }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");
      }//執(zhí)行后游標(biāo)指向最后一行的后一個(gè)位置
      rst.absolute(a);
      ta.append("---------------------------\n");
    }catch(Exception e){ta.append("-->顯示錯(cuò)誤");}
  }
// 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, "圖書(shū)日期應(yīng)為 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, "若存在借閱記錄則不能刪除書(shū)籍信息!","警告",JOptionPane.WARNING_MESSAGE);
    }
    if(m==6){
    JOptionPane.showMessageDialog(null, "若學(xué)生存在借閱記錄則不能刪除該學(xué)生信息!","警告",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("-->插入出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose(); 
      }
      if(x==2){  //添加借書(shū)記錄
          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();//找有沒(méi)有這本書(shū)和庫(kù)存是否為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("-->無(wú)該學(xué)生記錄,不能添加借閱記錄\n");}
          }else{frameDemo.a.ta.append("-->無(wú)此書(shū)或已全部借出\n");}              
          }catch(Exception x){frameDemo.a.ta.append("-->插入出錯(cuò)\n");x.printStackTrace();}
          addinformation.this.dispose();       
       }
      if(x==3){  //添加學(xué)生記錄
        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("-->插入出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose();       
      }
      if(x==4){  //刪除圖書(shū)記錄
        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()){  //借閱記錄中有書(shū)
          JOptionPane.showMessageDialog(null, "借閱記錄中有該書(shū)的借閱記錄!","警告",JOptionPane.WARNING_MESSAGE);
        }
        else{    //借閱記錄無(wú)書(shū),可以刪掉
          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("-->刪除出錯(cuò)\n");
          JOptionPane.showMessageDialog(null, "不存在該圖書(shū)信息!","警告",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){  //刪除借書(shū)記錄
        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("-->刪除出錯(cuò)\n");
          JOptionPane.showMessageDialog(null, "不存在這條借閱記錄!","警告",JOptionPane.WARNING_MESSAGE);
        }           
        }catch(Exception x){frameDemo.a.ta.append("-->刪除出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==6){ //刪除學(xué)生記錄
        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("-->刪除出錯(cuò)\n");
              JOptionPane.showMessageDialog(null, "不存在該學(xué)生記錄!","警告",JOptionPane.WARNING_MESSAGE);
              }
            else frameDemo.a.ta.append("-->刪除記錄成功\n");
            }             
        }catch(Exception x){frameDemo.a.ta.append("-->刪除出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==7){  //修改圖書(shū)庫(kù)存記錄
        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("-->修改出錯(cuò)\n");
          JOptionPane.showMessageDialog(null, "不存在該圖書(shū)記錄!","警告",JOptionPane.WARNING_MESSAGE);
        }
        else frameDemo.a.ta.append("-->修改記錄成功\n");    
        }catch(Exception x){frameDemo.a.ta.append("-->修改出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==8){  //修改應(yīng)還日期
        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("-->修改出錯(cuò)\n");
          JOptionPane.showMessageDialog(null, "不存在該借書(shū)記錄!","警告",JOptionPane.WARNING_MESSAGE);
        }
        else frameDemo.a.ta.append("-->修改記錄成功\n");    
        }catch(Exception x){frameDemo.a.ta.append("-->修改出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==9){  //按書(shū)名查詢
        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圖書(shū)編號(hào)\t書(shū)名\t在館數(shù)\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("-->查詢出錯(cuò)\n");
          JOptionPane.showMessageDialog(null, "不存在該書(shū)!","警告",JOptionPane.WARNING_MESSAGE);  
        }
        }catch(Exception x){frameDemo.a.ta.append("-->查詢出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose();
      }  
      if(x==10){  //按書(shū)籍編號(hào)查詢
        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圖書(shū)編號(hào)\t書(shū)名\t在館數(shù)\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("-->查詢出錯(cuò)\n");
          JOptionPane.showMessageDialog(null, "不存在該書(shū)!","警告",JOptionPane.WARNING_MESSAGE);  
        }
        }catch(Exception x){frameDemo.a.ta.append("-->查詢出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==11){  //按學(xué)生借書(shū)記錄查詢
        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圖書(shū)編號(hào)\t學(xué)生學(xué)號(hào)\t借書(shū)時(shí)間\t\t應(yīng)還時(shí)間\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("-->查詢出錯(cuò)\n");
          JOptionPane.showMessageDialog(null, "不存在該借書(shū)記錄!","警告",JOptionPane.WARNING_MESSAGE);  
         }
        }catch(Exception x){frameDemo.a.ta.append("-->查詢出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
      if(x==12){  //查詢學(xué)生信息
        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學(xué)生學(xué)號(hào)\t學(xué)生姓名\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("-->查詢出錯(cuò)\n");
          JOptionPane.showMessageDialog(null, "不存在該學(xué)生!","警告",JOptionPane.WARNING_MESSAGE);   
         }
        }catch(Exception x){frameDemo.a.ta.append("-->查詢出錯(cuò)\n");x.printStackTrace();}
        addinformation.this.dispose();
      }
    }
    if(ae.getSource()==quxiao){
      addinformation.this.dispose();
      frameDemo.a.ta.append("-->操作取消\n");
    }

  }
}

java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單實(shí)例 java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單實(shí)例

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI