亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java GUI實現學生圖書管理簡單實例

發布時間:2020-09-30 09:50:02 來源:腳本之家 閱讀:284 作者:wqdsb 欄目:編程語言

本文實例為大家分享了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實現學生圖書管理簡單實例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

郸城县| 新沂市| 长沙县| 辽阳县| 阜宁县| 营口市| 枣阳市| 崇左市| 嘉善县| 荔浦县| 阿巴嘎旗| 临桂县| 朝阳区| 淮阳县| 夏河县| 临沂市| 古蔺县| 合阳县| 酒泉市| 离岛区| 盈江县| 岳普湖县| 长宁区| 漠河县| 林甸县| 青河县| 梓潼县| 滨州市| 根河市| 清苑县| 金川县| 长垣县| 松江区| 蓬莱市| 望城县| 介休市| 阿坝县| 霍林郭勒市| 渭南市| 和林格尔县| 夹江县|