1. 程式人生 > 實用技巧 >Jming 圖書管理系統 SQL server

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