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

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

- mysql数据库建表:

1.book表

2.bs借书记录表

3.std学生表

4.dl登录用户表

.

- 列表内容

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");
    }

  }
}

 

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

(0)

相关推荐

  • 一个简陋的java图书管理系统

    本文代码为原创一个简陋的管理系统,只做功能的测试.并没有去完善所有应有的功能,只做了输入输出查找,仅供参考! 菜单部分: import java.util.Scanner; public class Menu { int Min = 1; int Max = 3; public void getMenu(){ System.out.println("1.显示/2.输入/3.查找"); } public void getFindMenu(){ System.out.println(&qu

  • 图书管理系统java版

    本文的目的就是通过图书管理系统掌握数据库编程技术,能正确连接数据库,能对数据库中信息进行查询.插入.删除.修改. 内容:在数据库中创建一张书目信息表,包括书名.作者.出版社.出版日期.书号.价格字段.设计一个GUI界面进行书目管理.在该界面上有四个选项卡,分别是查询.插入.删除.修改.点击查询选项卡,出现的界面上有书名.作者.出版社.书号四个文本框,一个按钮和一个只读文本区.文本框内容可以为空,输入相应的查询信息后(例如根据书名查询可以仅输入书名),点击界面上的"查询"按钮,可以在界面

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

    本文实例为大家分享了java GUI学生图书管理的具体代码,供大家参考,具体内容如下 - mysql数据库建表: 1.book表 2.bs借书记录表 3.std学生表 4.dl登录用户表 . - 列表内容 1.databd.java //程序入口及登录验证 import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; import javax.swing.border.EmptyBorde

  • 用Java实现小球碰壁反弹的简单实例(算法十分简单)

    核心代码如下: if(addX){ x+=3; }else{ x-=3; } if(addY){ y+=6; }else{ y-=6; } if(x<=0||x>=(width-50)){ addX=!addX; } if(y<=0||y>=(height-50)){ addY=!addY; } 根据x和y递增的值,来决定角度. 以上这篇用Java实现小球碰壁反弹的简单实例(算法十分简单)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • java判断中文字符串长度的简单实例

    话不多说,上代码: /** * 获取字符串的长度,如果有中文,则每个中文字符计为2位 * @param value 指定的字符串 * @return 字符串的长度 */ public static int length(String value) { int valueLength = 0; String chinese = "[\u0391-\uFFE5]"; /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */ for (int i = 0; i <

  • JAVA按字节读取文件的简单实例

    JAVA的IO流一直都是我比较头疼的部分(我没有系统学过JAVA,一般需要实现什么功能再去看文档). 最近遇到一个需求:一个字节一个字节地读取一个文件.网上很多方法,代码一大堆.我在这里和大家分享一个简单的办法(至少对我的需求是有效的). File file= new File(fileName); //filename为 文件目录,请自行设置 InputStream in= null; byte[] bytes= null; in = new FileInputStream(file); //

  • java 实现音乐播放器的简单实例

    java 实现音乐播放器的简单实例 实现效果图: 代码如下 package cn.hncu.games; import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Font; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.Mou

  • Java GUI实现学生成绩管理系统

    学习java有一年多了,一直在做web方面的练习,在一个项目中发现需要用到GUI的相关知识,结果没法做出来,网上这方面的文章又不是很多,所有只好自己硬着头皮从头再学一遍了,不过学习后发现,其实GUI是非常有趣的,他并不像WEB程序一样依赖互联网,而且还有许多布局和android相差不是很大,这才发现自己竟又爱上GUI的开发了,不多说了,直接上代码吧,相信有过android或相关界面开发的都明白这其中的道理. 先看看效果吧 1.登录主界面 package edu.gzu.stuManager; i

  • java GUI编程之监听操作实例分析

    本文实例讲述了java GUI编程之监听操作.分享给大家供大家参考,具体如下: 当点击Frame中的component组件时,会产生相应的效果,但是相应的其必须进行监听,确定是那个对象,那种操作,但是如果用cup进行主动的监听就会消耗大量的资源,所以有了被动的监听,即对应的事件发生后会自动的执行相关的代码. Button监听: instance 1: import java.awt.*; import java.awt.event.*; public class MoniterStart { p

  • Eclipse+Java+Swing实现学生成绩管理系统的实例代码

    一.系统介绍 本系统实现了用户登录,实现了对学生成绩的增删改查,实现了用户修改密码功能,采用MD5加密算法,数据库使用Mysql8.0.13,界面良好. 二.系统展示 1.登录页 2.主页面 3.查询学生成绩 4.修改学生成绩 5.添加学生成绩 6.修改系统密码 三.系统实现 Student.java package com.sjsq.model; import java.util.ArrayList; import java.util.List; /** * 学生实体类 * @author s

  • Java实现跳跃表(skiplist)的简单实例

    跳跃链表是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作需要O(log n)平均时间),并且对并发算法友好. 基本上,跳跃列表是对有序的链表增加上附加的前进链接,增加是以随机化的方式进行的,所以在列表中的查找可以快速的跳过部分列表(因此得名).所有操作都以对数随机化的时间进行. 实现原理: 跳跃表的结构是:假如底层有10个节点, 那么底层的上一层理论上就有5个节点,再上一层理论上就有2个或3个节点,再上一层理论上就有1个节点.所以从这里可以看出每一层的节点个数为其下

  • Java 数据库连接池详解及简单实例

    Java 数据库连接池详解 数据库连接池的原理是: 连接池基本的思想是在系统初始化的时候,将数据库连接作为对象存储在内存中,当用户需要访问数据库时,并非建立一个新的连接,而是从连接池中取出一个已建立的空闲连接对象.使用完毕后,用户也并非将连接关闭,而是将连接放回连接池中,以供下一个请求访问使用.而连接的建立.断开都由连接池自身来管理.同时,还可以通过设置连接池的参数来控制连接池中的初始连接数.连接的上下限数以及每个连接的最大使用次数.最大空闲时间等等.也可以通过其自身的管理机制来监视数据库连接的

随机推荐