Jming 图书管理系统 SQL server
![]()

//读者模块 public class general extends JFrame implements ActionListener { JPanel jp1, jp2; JLabel jl1, jl2; JButton jb1, jb2,jb3; JTable jt; JScrollPane jsp; JTextField jtf; Model sm; Statement stat = null; PreparedStatement ps; Connection ct = null; ResultSet rs = null; public general() { jp1 = new JPanel(); jtf = new JTextField(10); jb1 = new JButton("查询"); jb1.addActionListener(this); jl1 = new JLabel("请输入图书号:"); jp1.add(jl1); jp1.add(jtf); jp1.add(jb1); jb2 = new JButton("浏览"); jb2.addActionListener(this); jb3 = new JButton("借阅"); jb3.addActionListener(this); jp2 = new JPanel(); jp2.add(jb2); jp2.add(jb3); sm = new Model(0); jt = new JTable(sm); jsp = new JScrollPane(jt); this.add(jsp); this.add(jp1, "North"); this.add(jp2, "South"); setTitle("图书管理系统"); setSize(800, 600); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { general test3 = new general(); } public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == jb1) { String no = this.jtf.getText().trim(); String sql = "select * from BOOK_LIST where BNO = '" + no + "' "; JOptionPane.showMessageDialog(null,"正在查询"); sm = new Model(sql,1); jt.setModel(sm); JOptionPane.showMessageDialog(null,"查询成功"); } else if(arg0.getSource() == jb2){ sm = new Model(1); jt.setModel(sm); } else if (arg0.getSource() == jb3) { JOptionPane.showMessageDialog(null,"开始进行借阅操作"); Borrow sa = new Borrow(this, "借阅图书", true); sm = new Model(2); JOptionPane.showMessageDialog(null, "借阅成功"); } } } /////////////////////////////////////////////////// //修改读者信息模块 /////////////////////////////////////////////////// public class Updata_manage_Stu extends JDialog implements ActionListener { JLabel jl1, jl2, jl3, jl4, jl5, jl6; JTextField jf1, jf2, jf3, jf4, jf5, jf6; JPanel jp1, jp2, jp3; JButton jb1, jb2; String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; private static String user; private static String password1; public Updata_manage_Stu(Frame owner, String title, boolean modal, Model sm, int rowNum) { super(owner, title, modal); jl1 = new JLabel("读者号"); jl2 = new JLabel("读者姓名"); jl3 = new JLabel("性别"); jl4 = new JLabel("年级"); jl5 = new JLabel("所在系"); jl6 = new JLabel("班级"); jf1 = new JTextField(10); jf1.setText((sm.getValueAt(rowNum, 0)).toString()); jf2 = new JTextField(10); jf2.setText((String) sm.getValueAt(rowNum, 1)); jf3 = new JTextField(10); jf3.setText(sm.getValueAt(rowNum, 2).toString()); jf4 = new JTextField(10); jf4.setText((sm.getValueAt(rowNum, 3)).toString()); jf5 = new JTextField(10); jf5.setText((String) sm.getValueAt(rowNum, 4)); jf6 = new JTextField(10); jf6.setText((String) sm.getValueAt(rowNum, 5)); jb1 = new JButton("修改"); jb1.addActionListener(this); jb2 = new JButton("取消"); jb2.addActionListener(this); jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jp1.setLayout(new GridLayout(6, 1)); jp2.setLayout(new GridLayout(6, 1)); jp3.add(jb1); jp3.add(jb2); jp1.add(jl1); jp1.add(jl2); jp1.add(jl3); jp1.add(jl4); jp1.add(jl5); jp1.add(jl6); jp2.add(jf1); jp2.add(jf2); jp2.add(jf3); jp2.add(jf4); jp2.add(jf5); jp2.add(jf6); this.add(jp1, BorderLayout.WEST); this.add(jp2, BorderLayout.CENTER); this.add(jp3, BorderLayout.SOUTH); this.setSize(300, 200); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == jb1) { Connection ct = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; ct = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 String strsql = "insert into REDER_LIST values(?,?,?,?,?,?)"; pstmt = ct.prepareStatement(strsql); pstmt.setString(1, jf1.getText()); pstmt.setString(2, jf2.getText()); pstmt.setString(3, jf3.getText()); pstmt.setString(4, jf4.getText()); pstmt.setString(5, jf5.getText()); pstmt.setString(6, jf6.getText()); pstmt.executeUpdate(); this.dispose(); } catch (Exception arg1) { arg1.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (pstmt != null) { pstmt.close(); pstmt = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception arg2) { arg2.printStackTrace(); } } } } } ///////////////////////////////////// //借书模块 //////////////////////////////////////// public class Borrow extends JDialog implements ActionListener { JLabel jl1, jl2, jl3, jl4; JTextField jf1, jf2, jf3, jf4, jf5, jf6; JPanel jp1, jp2, jp3; JButton jb1, jb2; int k=0; String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; private static String user; private static String password1; public Borrow (Frame owner, String title, boolean modal) { super(owner, title, modal); jl1 = new JLabel("图书号"); jl2 = new JLabel("读者编号"); jl3 = new JLabel("图书名称"); jl4 = new JLabel("借出时间"); jf1 = new JTextField(10); jf2 = new JTextField(10); jf3 = new JTextField(10); jf4 = new JTextField(10); jb1 = new JButton("借阅"); jb1.addActionListener(this); jb2 = new JButton("取消"); jb2.addActionListener(this); jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jp1.setLayout(new GridLayout(6, 1)); jp2.setLayout(new GridLayout(6, 1)); jp3.add(jb1); jp3.add(jb2); jp1.add(jl1); jp1.add(jl2); jp1.add(jl3); jp1.add(jl4); jp2.add(jf1); jp2.add(jf2); jp2.add(jf3); jp2.add(jf4); this.add(jp1, BorderLayout.WEST); this.add(jp2, BorderLayout.CENTER); this.add(jp3, BorderLayout.SOUTH); this.setSize(300, 200); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == jb1) { Connection ct = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; ct = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 String strsql = "insert into BORROW_LIST values(?,?,?,?)"; pstmt = ct.prepareStatement(strsql); pstmt.setString(1, jf1.getText()); pstmt.setString(2, jf2.getText()); pstmt.setString(3, jf3.getText()); pstmt.setString(4, jf4.getText()); pstmt.executeUpdate(); k=2; this.dispose(); } catch (Exception arg1) { if(jf1.equals(null)) JOptionPane.showMessageDialog(null,"请输入提示的全部信息"); this.dispose(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (pstmt != null) { pstmt.close(); pstmt = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception arg2) { this.dispose(); arg2.printStackTrace(); } } }else if (e.getSource() == jb2) this.dispose(); } } ///////////////////////////////////////////// //还书模块 ///////////////////////////////////////////// public class BACK_BOOK extends JDialog implements ActionListener { JLabel jl1, jl2, jl3, jl4, jl5, jl6; JTextField jf1, jf2, jf3, jf4, jf5, jf6; JPanel jp1, jp2, jp3; JButton jb1, jb2; int k=0; String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; private static String user; private static String password1; public BACK_BOOK (Frame owner, String title, boolean modal) { super(owner, title, modal); jl1 = new JLabel("图书号"); jl2 = new JLabel("读者编号"); jl3 = new JLabel("图书名称"); jl4 = new JLabel("归还时间"); jl5 = new JLabel("超期金额"); jf1 = new JTextField(10); jf2 = new JTextField(10); jf3 = new JTextField(10); jf4 = new JTextField(10); jf5 = new JTextField(10); jb1 = new JButton("归还"); jb1.addActionListener(this); jb2 = new JButton("取消"); jb2.addActionListener(this); jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jp1.setLayout(new GridLayout(6, 1)); jp2.setLayout(new GridLayout(6, 1)); jp3.add(jb1); jp3.add(jb2); jp1.add(jl1); jp1.add(jl2); jp1.add(jl3); jp1.add(jl4); jp1.add(jl5); jp2.add(jf1); jp2.add(jf2); jp2.add(jf3); jp2.add(jf4); jp2.add(jf5); this.add(jp1, BorderLayout.WEST); this.add(jp2, BorderLayout.CENTER); this.add(jp3, BorderLayout.SOUTH); this.setSize(300, 200); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == jb1) { Connection ct = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; ct = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 String strsql = "insert into BACK_LIST values(?,?,?,?,?)"; pstmt = ct.prepareStatement(strsql); pstmt.setString(1, jf1.getText()); pstmt.setString(2, jf2.getText()); pstmt.setString(3, jf3.getText()); pstmt.setString(4, jf4.getText()); pstmt.setString(5, jf5.getText()); pstmt.executeUpdate(); k=3; this.dispose(); } catch (Exception arg1) { if(jf1.equals(null)) JOptionPane.showMessageDialog(null,"请输入提示的全部信息"); this.dispose(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (pstmt != null) { pstmt.close(); pstmt = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception arg2) { this.dispose(); arg2.printStackTrace(); } } }else if (e.getSource() == jb2) this.dispose(); } } /////////////////////////////////////////////////////////// //登录模块 ///////////////////////////////////////////////////////// public class Landing extends JFrame{ private static int count=0; private static JButton bt1;//登陆按钮 private static JButton bt2;//退出按钮 private static JButton bt5;//注册按钮 private static JRadioButton bt3;//管理员按钮 private static JRadioButton bt4;//学生按钮 private static JLabel jl_1;//登录的版面 private static JFrame jf_1;//登录的框架 private static JTextField jtext1;//用户名 private static JPasswordField jtext2;//密码 private static JLabel jl_admin;//"用户名" private static JLabel jl_password;//"密码" private static JPanel bp; private static String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 private static String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; private static String user; private static String password1; private static String admin; private static String admin1; private static char[] password; private static boolean bt33; private static boolean bt44; public Landing (){ Font font =new Font("微软雅黑", Font.ITALIC, 20);//字体备选:PLAIN jf_1=new JFrame("登录界面"); jf_1.setSize(450, 400); jl_1=new JLabel(); jl_admin=new JLabel("用户名"); jl_admin.setBounds(20, 50, 60, 50); jl_admin.setFont(font); jl_password=new JLabel("密码"); jl_password.setBounds(20, 120, 60, 50); jl_password.setFont(font); bt1=new JButton("登录"); bt1.setBounds(45, 250, 100, 50); bt1.setFont(font); bt2=new JButton("退出"); bt2.setBounds(150, 250, 100, 50); bt2.setFont(font); bt5=new JButton("注册"); bt5.setBounds(255, 250, 100, 50); bt5.setFont(font); bp=new JPanel(); bt3=new JRadioButton(); bt3.setText("管理员"); bt3.setFont(font); bp.add(bt3); bt4=new JRadioButton(); bt4.setText("学生"); bt4.setFont(font); bp.add(bt4); ButtonGroup group=new ButtonGroup(); group.add(bt3); group.add(bt4); jtext1=new JTextField(10); jtext1.setBounds(150, 50, 250, 50); jtext1.setFont(font); jtext2=new JPasswordField(10); jtext2.setBounds(150, 120, 250, 50); jtext2.setFont(font); jl_1.add(jtext1); jl_1.add(jtext2); jl_1.add(jl_admin); jl_1.add(jl_password); jl_1.add(bt1); jl_1.add(bt2); jl_1.add(bt5); jf_1.add(jl_1); jf_1.add(bp,BorderLayout.SOUTH); jf_1.setVisible(true); jf_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Landing hl =new Landing(); ActionListener bt1_ls=new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(bt33) { hl.jf_1.dispose(); Main ml=new Main(); JOptionPane.showMessageDialog(null,"成功进入管理员模式"); }else if(bt44) { hl.jf_1.dispose(); general g1=new general(); JOptionPane.showMessageDialog(null,"成功进入读者模式"); }else { count++; JOptionPane.showMessageDialog(null,"输入错误!累计三次自动退出"); if(count==3){ JOptionPane.showMessageDialog(null,"连续输入错误三次!自动退出"); hl.jf_1.dispose(); } } } }; bt1.addActionListener(bt1_ls); ActionListener bt2_ls=new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }; bt2.addActionListener(bt2_ls); ActionListener bt3_ls=new ActionListener() { @Override public void actionPerformed(ActionEvent a) { admin=jtext1.getText(); password=jtext2.getPassword(); String str=String.valueOf(password); try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; } catch (ClassNotFoundException e1){ System.exit(0); } try { Connection con = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 Statement stmt = con.createStatement();// 创建SQL命令对象 ResultSet rs = stmt.executeQuery("SELECT * FROM GUL_LOGIN");// 返回SQL语句查询结果集(集合) // 循环输出每一条记录 while (rs.next()) { // 输出每个字段 if( admin.equals(rs.getString("SNAME"))&&str.equals(rs.getString("PASSWORD"))) { bt33=true; } } stmt.close();// 关闭命令对象连接 con.close();// 关闭数据库连接 } catch (SQLException e1) { e1.printStackTrace(); System.exit(0); } } }; bt3.addActionListener(bt3_ls); ActionListener bt4_ls=new ActionListener() { @Override public void actionPerformed(ActionEvent b) { admin=jtext1.getText(); password=jtext2.getPassword(); String str=String.valueOf(password); try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; } catch (ClassNotFoundException e1){ System.exit(0); } try { Connection con = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 Statement stmt = con.createStatement();// 创建SQL命令对象 ResultSet rs = stmt.executeQuery("SELECT * FROM STU_LOGIN");// 返回SQL语句查询结果集(集合) // 循环输出每一条记录 while (rs.next()) { // 输出每个字段 if( admin.equals(rs.getString("SNAME"))&&str.equals(rs.getString("PASSWORD"))) { bt44=true; } } stmt.close();// 关闭命令对象连接 con.close();// 关闭数据库连接 } catch (SQLException e1) { e1.printStackTrace(); System.exit(0); } } }; bt4.addActionListener(bt4_ls); ActionListener bt5_ls=new ActionListener() { @Override public void actionPerformed(ActionEvent b) { admin1 = jtext1.getText(); password = jtext2.getPassword(); String str = String.valueOf(password); try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; } catch (ClassNotFoundException e1) { System.exit(0); } if (!bt44) { try { Connection con = DriverManager.getConnection(connectDB, user, password1);// 连接数据库对象 Statement stmt = con.createStatement();// 创建SQL命令对象 stmt.execute("INSERT INTO STU_LOGIN VALUES ("+admin1+","+str+")");// 返回SQL语句查询结果集(集合) JOptionPane.showMessageDialog(null,"注册成功"); stmt.close();// 关闭命令对象连接 con.close();// 关闭数据库连接 } catch (SQLException e1) { e1.printStackTrace(); System.exit(0); } }else if (!bt33) { try { Connection con = DriverManager.getConnection(connectDB, user, password1);// 连接数据库对象 Statement stmt = con.createStatement();// 创建SQL命令对象 stmt.executeUpdate("INSERT INTO GUL_LOGIN VALUES ("+admin+","+str+")");// 返回SQL语句查询结果集(集合) // 循环输出每一条记录 stmt.close();// 关闭命令对象连接 con.close();// 关闭数据库连接 } catch (SQLException e1) { e1.printStackTrace(); System.exit(0); } } } }; bt5.addActionListener(bt5_ls); } } ///////////////////////////////////////////////////////// //管理员模块 /////////////////////////////////////////////////////// public class Main extends JFrame implements ActionListener { JPanel jp1, jp2; JLabel jl1, jl2; JButton jb1, jb2, jb3, jb4,jb5,jb6,jb7,jb8,jb9,jb10,jb11; JTable jt; JScrollPane jsp; JTextField jtf; JLabel k=new JLabel(); Model sm; private static String user; private static String password1; Statement stat = null; PreparedStatement ps; Connection ct = null; ResultSet rs = null; String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; public Main() { k.setText("欢迎进入图书管理系统"); jp1 = new JPanel(); jtf = new JTextField(10); jb1 = new JButton("查询"); jb1.addActionListener(this); jl1 = new JLabel("请输入图书号:"); jp1.add(k); jp1.add(jl1); jp1.add(jtf); jp1.add(jb1); jb2 = new JButton("添加"); jb2.addActionListener(this); jb3 = new JButton("修改"); jb3.addActionListener(this); jb4 = new JButton("删除"); jb4.addActionListener(this); jb5 = new JButton("浏览"); jb5.addActionListener(this); jb6=new JButton("借阅"); jb6.addActionListener(this); jb7=new JButton("还书"); jb7.addActionListener(this); jb8=new JButton("借阅记录"); jb8.addActionListener(this); jb9=new JButton("还书记录"); jb9.addActionListener(this); jb10=new JButton("读者记录"); jb10.addActionListener(this); jb11=new JButton("修改读者信息"); jb11.addActionListener(this); jp2 = new JPanel(); jp2.add(jb2); jp2.add(jb3); jp2.add(jb4); jp2.add(jb5); jp2.add(jb6); jp2.add(jb7); jp2.add(jb8); jp2.add(jb9); jp2.add(jb10); jp2.add(jb11); sm = new Model(0); jt = new JTable(sm); jsp = new JScrollPane(jt); this.add(jsp); this.add(jp1, "North"); this.add(jp2, "South"); setTitle("图书管理系统"); setSize(800, 600); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Main test3 = new Main(); } public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == jb1) { String name = this.jtf.getText().trim(); String sql = "select * from BOOK_LIST where BNO = '" + name + "' "; JOptionPane.showMessageDialog(null,"正在查询"); sm = new Model(sql,1); jt.setModel(sm); JOptionPane.showMessageDialog(null,"查询成功"); } else if (arg0.getSource() == jb2) { JOptionPane.showMessageDialog(null,"开始进行添加操作"); Add sa = new Add(this, "添加图书信息", true); JOptionPane.showMessageDialog(null,"添加成功"); sm = new Model(1); jt.setModel(sm); } else if (arg0.getSource() == jb4) { JOptionPane.showMessageDialog(null,"开始进行删除操作"); int rowNum = this.jt.getSelectedRow(); if (rowNum == -1) { JOptionPane.showMessageDialog(this, "请选中一行"); return; } String bookId = (String) sm.getValueAt(rowNum, 0); JOptionPane.showMessageDialog(this, "已成功删除图书号:" + bookId + "的信息"); try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; Connection ct = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 ps = ct.prepareStatement("delete from BOOK_LIST where BNO = ?"); ps.setString(1,bookId); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (ps != null) { ps.close(); ps = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception e) { e.printStackTrace(); } } sm = new Model(1); jt.setModel(sm); } else if (arg0.getSource() == jb3) { JOptionPane.showMessageDialog(null,"开始进行修改操作"); int rowNum = this.jt.getSelectedRow(); if (rowNum == -1) { JOptionPane.showMessageDialog(this, "请选择一行"); return; } String goodsId = (String) sm.getValueAt(rowNum, 0); try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; Connection ct = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 ps = ct.prepareStatement("delete from BOOK_LIST where BNO = ?"); ps.setString(1, goodsId); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (ps != null) { ps.close(); ps = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception e) { e.printStackTrace(); } } Update su = new Update(this, "修改图书信息", true, sm, rowNum); JOptionPane.showMessageDialog(null,"修改完成"); sm = new Model(1); jt.setModel(sm); } else if(arg0.getSource() == jb5){ sm = new Model(1); jt.setModel(sm); } else if (arg0.getSource() == jb6) { JOptionPane.showMessageDialog(null,"开始进行借阅操作"); Borrow sa = new Borrow(this, "借阅图书", true); if(sa.k==2) { sm = new Model(2); jt.setModel(sm); JOptionPane.showMessageDialog(null, "借阅成功"); }else JOptionPane.showMessageDialog(null, "图书已借出"); }else if (arg0.getSource() == jb7) { JOptionPane.showMessageDialog(null,"开始进行归还操作"); BACK_BOOK sa = new BACK_BOOK(this, "归还图书", true); sm = new Model(3); jt.setModel(sm); JOptionPane.showMessageDialog(null, "归还成功"); } else if(arg0.getSource() == jb8){ sm = new Model(2); jt.setModel(sm); } else if(arg0.getSource() == jb9){ sm = new Model(3); jt.setModel(sm); } else if(arg0.getSource() == jb10){//读者信息 sm = new Model(4); jt.setModel(sm); }else if (arg0.getSource() == jb11) { JOptionPane.showMessageDialog(null,"开始进行修改操作"); int rowNum = this.jt.getSelectedRow(); if (rowNum == -1) { JOptionPane.showMessageDialog(this, "请选择一行"); return; } String readId = (String) sm.getValueAt(rowNum, 0); try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; Connection ct = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 ps = ct.prepareStatement("delete from REDER_LIST where RNO = ?"); ps.setString(1, readId); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (ps != null) { ps.close(); ps = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception e) { e.printStackTrace(); } } Updata_manage_Stu su= new Updata_manage_Stu(this, "修改读者信息", true, sm, rowNum); JOptionPane.showMessageDialog(null,"修改完成"); sm = new Model(4); jt.setModel(sm); } } } ////////////////////////////////////////////////// //公共处理模块 ///////////////////////////////////////////////////// public class Model extends AbstractTableModel { Vector rowData, columnNames; String sql =null; String sql1=null; Statement stat = null; Connection ct = null; ResultSet rs = null; int i=1; public void init(String sql,int i) { if(i==1) if (sql.equals("")) { this.sql = "select * from BOOK_LIST"; }else this.sql=sql; if(i==2) if (sql.equals("")) { this.sql = "select * from BORROW_LIST"; } if(i==3) if (sql.equals("")) { this.sql = "select * from BACK_LIST"; } if(i==4) if (sql.equals("")) { this.sql = "select * from REDER_LIST"; } if(i==0||i==1){ columnNames = new Vector(); columnNames.add("图书号"); columnNames.add("图书名"); columnNames.add("图书价格"); columnNames.add("图书作者"); columnNames.add("图书入库时间"); columnNames.add("图书是否借出"); } if(i==2) { columnNames = new Vector(); columnNames.add("图书号"); columnNames.add("读者编号"); columnNames.add("图书名称"); columnNames.add("借出时间"); } if(i==3) { columnNames = new Vector(); columnNames.add("图书号"); columnNames.add("读者编号"); columnNames.add("图书名称"); columnNames.add("归还时间"); columnNames.add("超期金额"); } if(i==4) { columnNames = new Vector(); columnNames.add("读者号"); columnNames.add("读者姓名"); columnNames.add("性别"); columnNames.add("年级"); columnNames.add("所在系"); columnNames.add("班级"); } rowData = new Vector(); if(i==2)// 借阅之后显示借阅信息 { try { String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; String user; String password1; Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; Connection ct = DriverManager.getConnection(connectDB, user, password1);// 连接数据库对象 Statement stat = ct.createStatement();// 创建SQL命令对象 rs = stat.executeQuery(this.sql); while (rs.next()) { Vector hang = new Vector(); hang.add(rs.getString(1)); hang.add(rs.getString(2)); hang.add(rs.getString(3)); hang.add(rs.getString(4)); rowData.add(hang); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (stat != null) { stat.close(); stat = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception e) { e.printStackTrace(); } } //添加触发器 try { String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; String user; String password1; Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; Connection ct = DriverManager.getConnection(connectDB, user, password1);// 连接数据库对象 Statement stat = ct.createStatement();// 创建SQL命令对象 this.sql= "CREATE TRIGGER DB_BOOKBOOK_INSERT2 ON BORROW_LIST\n" + "AFTER INSERT AS\n" + "DECLARE @bno varchar(30);\n" + "SELECT @bno =bno \n" + "FROM inserted \n" + "\n" + "BEGIN\n" + "UPDATE BOOK_LIST \n" + "SET ISBORROW ='已借出'\n" + "WHERE BNO=@bno\n" + "END"; rs = stat.executeQuery(this.sql); } catch (Exception e) { } finally { try { if (rs != null) { rs.close(); rs = null; } if (stat != null) { stat.close(); stat = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception e) { e.printStackTrace(); } } } ///////////////////////////////// if(i==3)// 归还之后显示归还信息 { try { String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; String user; String password1; Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; Connection ct = DriverManager.getConnection(connectDB, user, password1);// 连接数据库对象 Statement stat = ct.createStatement();// 创建SQL命令对象 rs = stat.executeQuery(this.sql); while (rs.next()) { Vector hang = new Vector(); hang.add(rs.getString(1)); hang.add(rs.getString(2)); hang.add(rs.getString(3)); hang.add(rs.getString(4)); hang.add(rs.getString(5)); rowData.add(hang); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (stat != null) { stat.close(); stat = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception e) { e.printStackTrace(); } } //添加触发器 try { String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; String user; String password1; Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; Connection ct = DriverManager.getConnection(connectDB, user, password1);// 连接数据库对象 Statement stat = ct.createStatement();// 创建SQL命令对象 this.sql= "CREATE TRIGGER DB_BACK_LIST_INSERT1 ON BACK_LIST\n" + "AFTER INSERT AS\n" + "DECLARE @bno varchar(30);\n" + "SELECT @bno =bno \n" + "FROM inserted \n" + "\n" + "BEGIN\n" + "UPDATE BOOK_LIST \n" + "SET ISBORROW ='未借出'\n" + "WHERE BNO=@bno\n" + "END"; rs = stat.executeQuery(this.sql); } catch (Exception e) { } finally { try { if (rs != null) { rs.close(); rs = null; } if (stat != null) { stat.close(); stat = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception e) { e.printStackTrace(); } } } ////////////////////////////// if(i==1||i==4) //显示图书基本信息 try { String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; String user; String password1; Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; Connection ct = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 Statement stat = ct.createStatement();// 创建SQL命令对象 rs = stat.executeQuery(this.sql); while (rs.next()) { Vector hang = new Vector(); hang.add(rs.getString(1)); hang.add(rs.getString(2)); hang.add(rs.getString(3)); hang.add(rs.getString(4)); hang.add(rs.getString(5)); hang.add(rs.getString(6)); rowData.add(hang); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (stat != null) { stat.close(); stat = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception e) { e.printStackTrace(); } } } public Model(String sql,int i) { this.init(sql,i); } public Model(int i) { this.init("",i); } public int getRowCount() { // TODO Auto-generated method stub return this.rowData.size(); } public int getColumnCount() { // TODO Auto-generated method stub return this.columnNames.size(); } public Object getValueAt(int row, int column) { // TODO Auto-generated method stub return ((Vector) (this.rowData.get(row))).get(column); } public String getColumnName(int column) { // TODO Auto-generated method stub return (String) this.columnNames.get(column); } } //////////////////////////////////////////////////////// //更新数据模块 //////////////////////////////////////////////////////// public class Update extends JDialog implements ActionListener { JLabel jl1, jl2, jl3, jl4, jl5, jl6; JTextField jf1, jf2, jf3, jf4, jf5, jf6; JPanel jp1, jp2, jp3; JButton jb1, jb2; String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎 String connectDB = "jdbc:sqlserver://127.0.0.1:0;DatabaseName=db_tsgl"; private static String user; private static String password1; public Update(Frame owner, String title, boolean modal, Model sm, int rowNum) { super(owner, title, modal); jl1 = new JLabel("图书号"); jl2 = new JLabel("图书名称"); jl3 = new JLabel("图书价格"); jl4 = new JLabel("图书作者"); jl5 = new JLabel("入库时间"); jl6 = new JLabel("是否借出"); jf1 = new JTextField(10); jf1.setText((sm.getValueAt(rowNum, 0)).toString()); jf2 = new JTextField(10); jf2.setText((String) sm.getValueAt(rowNum, 1)); jf3 = new JTextField(10); jf3.setText(sm.getValueAt(rowNum, 2).toString()); jf4 = new JTextField(10); jf4.setText((sm.getValueAt(rowNum, 3)).toString()); jf5 = new JTextField(10); jf5.setText((String) sm.getValueAt(rowNum, 4)); jf6 = new JTextField(10); jf6.setText((String) sm.getValueAt(rowNum, 5)); jb1 = new JButton("修改"); jb1.addActionListener(this); jb2 = new JButton("取消"); jb2.addActionListener(this); jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jp1.setLayout(new GridLayout(6, 1)); jp2.setLayout(new GridLayout(6, 1)); jp3.add(jb1); jp3.add(jb2); jp1.add(jl1); jp1.add(jl2); jp1.add(jl3); jp1.add(jl4); jp1.add(jl5); jp1.add(jl6); jp2.add(jf1); jp2.add(jf2); jp2.add(jf3); jp2.add(jf4); jp2.add(jf5); jp2.add(jf6); this.add(jp1, BorderLayout.WEST); this.add(jp2, BorderLayout.CENTER); this.add(jp3, BorderLayout.SOUTH); this.setSize(300, 200); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == jb1) { Connection ct = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类 user = "sa";// 你自己创建的用户名字和密码!!!!!!!!!!!! password1 = "123456"; ct = DriverManager.getConnection(connectDB,user,password1);// 连接数据库对象 String strsql = "insert into BOOK_LIST values(?,?,?,?,?,?)"; pstmt = ct.prepareStatement(strsql); pstmt.setString(1, jf1.getText()); pstmt.setString(2, jf2.getText()); pstmt.setString(3, jf3.getText()); pstmt.setString(4, jf4.getText()); pstmt.setString(5, jf5.getText()); pstmt.setString(6, jf6.getText()); pstmt.executeUpdate(); this.dispose(); } catch (Exception arg1) { arg1.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (pstmt != null) { pstmt.close(); pstmt = null; } if (ct != null) { ct.close(); ct = null; } } catch (Exception arg2) { arg2.printStackTrace(); } } } } }


浙公网安备 33010602011771号