Báo Cáo Hệ Thống Quản Lý Thư Viện Java Programing Languaage

27 99 0
Báo Cáo Hệ Thống Quản Lý Thư Viện  Java Programing Languaage

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Thư viện là một yếu tố căn bản và quan trọng, là thước đo đánh giá vai trò, chức năng, nhiệm vụ, hiệu quả đào tạo của đại học và không thể tách rời trường đại học với thư viện. Vì thế nhóm chúng em đã chọn đề tài ứng dụng ngôn ngữ java để xây dựng hệ thống “Quản lý thư viện” nhằm tìm hiểu sâu hơn về ngôn ngữ java trong lập trình và củng cố kiến thức, xây dựng các ứng dụng trong tương lai.

1 LỜI MỞ ĐẦU Ngày công nghệ thông tin đóng vai trò quan trọng đời sống hàng ngày Việc ứng dụng công nghệ thông tin vào lĩnh vực đời sống giúp hồn thành cơng việc nhanh hiệu Có nhiều cơng việc phát triển song song với phát triển công nghệ thông tin, số thiết kế phần mềm, hướng dịch vụ mang lại hiệu lớn Trong thời đại ngày nay, cơng tác thơng tin nói chung thơng tin thư viện nói riêng có tầm quan trọng đặc biệt ảnh hưởng sâu sắc tới lĩnh vực đời sống xã hội, có lĩnh vực giáo dục đào tạo Việc khai thác hiệu thông tin trở thành nhân tố hàng đầu chiến lược phát triển kinh tế – xã hội quốc gia Thư viện cầu nối thông tin người sử dụng Thư viện yếu tố quan trọng, thước đo đánh giá vai trò, chức năng, nhiệm vụ, hiệu đào tạo đại học tách rời trường đại học với thư viện Vì nhóm chúng em chọn đề tài ứng dụng ngôn ngữ java để xây dựng hệ thống “Quản lý thư viện” nhằm tìm hiểu sâu ngơn ngữ java lập trình củng cố kiến thức, xây dựng ứng dụng tương lai Qua nhóm chúng em xin cảm ơn thầy Cù Việt Dũng tậm tâm giúp đỡ, bảo chúng em trình học thực đề tài nghiên cứu nhóm chúng em Do thời gian thực nghiên cứu đề tài hạn chế nên nhóm chúng em khơng thể tránh khỏi thiếu sót định Chúng em mong nhận ý kiến đóng góp thầy bạn để chúng em có thêm kinh nghiệm tiếp tục hồn thành đề tài nghiên cứu CHƯƠNG I: TỔNG QUAN VỀ NGƠN NGỮ JAVA Lược sử ngơn ngữ Java Java khởi đầu James Gosling bạn đồng nghiệp Sun Microsystems năm 1991 Ban đầu ngôn ngữ gọi Oak (có nghĩa sồi; bên ngồi quan ơng Gosling có trồng nhiều loại này), họ dự định ngôn ngữ thay cho C++, tính giống Objective C Không nên lẫn lộn Java với JavaScript, hai ngôn ngữ giống tên loại cú pháp C Công ty Sun Microsystems giữ quyền phát triển Java thường xuyên Tháng 04/2011, công ty Sun Microsystems tiếp tục cho JDK 1.6.24 Java tạo với tiêu chí "Viết (code) lần, thực thi khắp nơi" ("Write Once, Run Anywhere" (WORA)) Chương trình phần mềm viết Java chạy tảng (platform) khác thông qua môi trường thực thi với điều kiện có mơi trường thực thi thích hợp hỗ trợ tảng Mơi trường thực thi Sun Microsystems hỗ trợ Sun Solaris, Linux, Mac OS, FreeBSD & Windows Ngồi ra, số cơng ty, tổ chức cá nhân khác phát triển môi trường thực thi Java cho hệ điều hành khác BEA, IBM, HP Trong đáng nói đến IBM Java Platform hỗ trợ Windows, Linux, AIX & z/OS Những chi tiết ngôn ngữ, máy ảo API Java giữ Cộng đồng Java (do Sun quản lý) Java tạo vào năm 1991 số kỹ sư Sun, bao gồm ông James Gosling, phần Dự án Xanh (Green Project) Java phát hành vào năm 1994, trở nên tiếng Netscape tuyên bố hội thảo SunWorld năm 1995 trình duyệt Navigator họ hỗ trợ Java Về sau Java hỗ trợ hầu hết trình duyệt Internet Explorer (Microsoft), Firefox (Mozilla), Safari (Apple) Đặc điểm ngơn ngữ Java Java có nhiều đặc điểm số đặc điểm bật java: - Đơn giản: Cú pháp java dựa C++ (vì dễ dàng với người tiếp xúc với ngôn ngữ C++) Bỏ nhiều đặc điểm gây bối rối sử dụng trỏ (pointer) nạp chồng toán tử (overloading), Java có hệ thống dọn rác tự động (garbage Collection) ta khơng cần xóa đối tượng tham chiếu C C++ - Hướng đối tượng: Java ngơn ngữ điển hình lập trình hướng đối tượng, có khái niệm lập trình hướng đối tượng như: đối tương (object), lớp (class), tính kế thừa (), tính đa hình (), tính trừu tượng (), tính đóng gói () - Độc lập tảng: Một Platform môi trường phần cứng phần mềm chương trình chạy Có hai loại Platform: loại dựa phần mềm (software-based) loại dựa phần cứng (hardware-based) Java cung cấp software-based platform Java Platform khác với nhiều tảng khác chỗ chạy tảng hardware-based khác Nó có hai thành phần là: Runtime Environment API (Application Programing Interface) Java chạy nhiều tảng Windows, Linux, Sun Solaris, Mac/OS, … Java code biên dịch Bộ biên dịch Compiler chuyển đổi thành Bytecode Bytecode code độc lập tảng chạy nhiều tảng khác Đây ưu điểm lớn java - Bảo mật: Java an tồn vì: Java khơng có trỏ chương trình chạy bên hộp thiết bị ảo Java có classloader: thêm bảo vệ việc phân biệt riêng rẽ package cho lớp hệ thống local file mà từ chúng import với file từ nguồn mạng; bytecode vertifier: kiểm tra đoạn code để tìm phần code khơng hợp lệ mà truy cập trái phép tới đối tượng; security manager: định xem nguồn resource mà lớp truy cập - Hiệu suất cao: Với việc sử dụng Just-In-Time compilers, Java giúp nâng cao hiệu năng, giúp việc debug dễ dàng nhanh chóng phát lỗi - Đa luồng: Một Thread giống chương trình riêng rẽ, thực thi cách đồng thời Chúng ta viết chương trình Java mà xử lý nhiều tác vụ lúc việc định nghĩa nhiều Thread Lợi Multi-thread chia sẻ nhớ Các Thread quan trọng cho Multi-media, Web App, … - Hỗ trợ thiết kế giao diện: Java hỗ trợ lập trình viên thiết kế giao diện thơng qua hai thư viện AWT SWING sau SWT - Hỗ trợ kết nối đến sở liệu: Java có hỗ trợ kết nối sở liệu SQL server, Oracle, mySQL, CHƯƠNG II: SỬ DỤNG NGÔN NGỮ JAVA XÂY DỰNG THỐNG QUẢN LÝ THƯ VIỆN Sự cần thiết toán Trong thời đại ngày nay, cơng tác thơng tin nói chung thơng tin thư viện nói riêng có tầm quan trọng đặc biệt ảnh hưởng sâu sắc tới lĩnh vực đời sống xã hội, có lĩnh vực giáo dục đào tạo Việc khai thác hiệu thông tin trở thành nhân tố hàng đầu chiến lược phát triển kinh tế – xã hội quốc gia Thư viện cầu nối thông tin người sử dụng Thư viện yếu tố quan trọng, thước đo đánh giá vai trò, chức năng, nhiệm vụ, hiệu đào tạo đại học tách rời trường đại học với thư viện Thiết kế sở liệu Hệ thống sử dụng SQL Server để xây dựng sở liệu Các bảng hệ thống sau: - Bảng NGUOIDUNG có trường: Username Pass Username primary key - Bảng BANDOC có trường: MaBD, TenBD, NamSinh, GioiTinh, DoiTuong, DiaChi SDT MaBD primary key - Bảng SACH có trường: MaSach, TenSach, TacGia, NXB, KeSach DonGia MaSach primary key - Bảng MUONSACH có trường: MaBD, MaSach, NgayMuon, NgayTra, GiaTien MaBD MaSach primary key Diagrams sở liệu Thiết kế giao diện a Thiết kế giao diện đăng nhập hệ thống class loginSystem implements ActionListener { // Properties of UI private JFrame mainF; private JLabel lbTitle; private JLabel lbUser; private JLabel lbPass; private JTextField txtUser; private JPasswordField txtPass; private JButton btLogin; private JButton btExit; public void createUILogin() { // Set properties for Frame mainF = new JFrame("Hệ thống quản lý thư viện"); mainF.setSize(400, 300); mainF.setLayout(null); mainF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //////////////////Set properties for component // Set properties for label lbTitle = new JLabel(); lbTitle.setBounds(90,0, 200, 40); lbTitle.setFont(new java.awt.Font("Time news roman", 1, 0)); lbTitle.setText("Đăng nhập"); lbTitle.setHorizontalAlignment((JLabel.CENTER)); lbTitle.setVerticalAlignment(JLabel.CENTER); lbUser = new JLabel(); lbUser.setBounds(15, 80, 85, 20); lbUser.setText("Tài Khoản:"); lbUser.setFont(new java.awt.Font("Time news roman", 0, 16)); lbUser.setHorizontalAlignment((JLabel.LEFT)); lbUser.setVerticalAlignment(JLabel.CENTER); lbPass = new JLabel(); lbPass.setBounds(15, 130, 85, 20); lbPass.setFont(new java.awt.Font("Time news roman", 0, 16)); lbPass.setText("Mật Khẩu:"); lbPass.setHorizontalAlignment((JLabel.LEFT)); lbPass.setVerticalAlignment(JLabel.CENTER); //Set properties for text field txtUser = new JTextField(20); txtUser.setBounds(110, 80, 250, 20); txtPass = new JPasswordField(20); txtPass.setBounds(110, 130, 250, 20); //Set button btLogin = new JButton("Đăng nhập"); btLogin.setBounds(50, 190, 100, 35); btLogin.addActionListener((ActionListener) this); btExit = new JButton("Thoát"); btExit.setBounds(230, 190, 100, 35); btExit.addActionListener((ActionListener) this); //Add component to fram mainF.add(lbTitle); mainF.add(lbUser); mainF.add(lbPass); mainF.add(txtUser); mainF.add(txtPass); mainF.add(btLogin); mainF.add(btExit); mainF.setVisible(true); } @Override public void actionPerformed(ActionEvent evt) { if(evt.getSource() == btLogin) { try { String URL = "jdbc:sqlserver://localhost:1433; databaseName = Te stJava; user = sa; password = 123456"; Connection cnn = DriverManager.getConnection(URL); String command = "Select * From NguoiDung Where Username = ' "+txtUser.getText()+"' And Pass = '"+txtPass.getText()+"'"; Statement stmt = cnn.createStatement(); ResultSet result; result = stmt.executeQuery(command); if(result.next()== true) { System.out.println("Success!!"); new mainMenuUI(); mainF.hide(); } else { System.out.println("Fail"); JOptionPane.showMessageDialog(btLogin, "Tên tài khoản, mật k hẩu sai!!"); } cnn.close(); } catch(SQLException ex) { System.out.print("Fail"); JOptionPane.showMessageDialog(btLogin, "Kết nối tới sở liệu thất bại!!"); } } if(evt.getSource() == btExit) { System.exit(0); } } } public class ConfigQLTV { public static void main(String[] args) { // Goi sang giao dien chinh cua chuong trinh loginSystem log = new loginSystem(); log.createUILogin(); } } b Thiết kế tabpanel class mainMenuUI { private final JFrame mainUI = new JFrame("Hệ thống quản lý thư viện"); private final JTabbedPane tbPanel = new JTabbedPane(); private final tabReader tbReader = new tabReader(); private final tabBook tbBook = new tabBook(); private final tabBorrow tbBorrow = new tabBorrow(); private final tabSearch tbSearch = new tabSearch(); public mainMenuUI() { mainUI.setSize(1100, 530); mainUI.setVisible(true); mainUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // add jtabpane vao frame mainUI.add(addPanel()); } private JTabbedPane addPanel() { // add cac panel vao tabpanel tbPanel.addTab("Bạn Đọc", tbReader.createtabReader()); tbPanel.addTab("Sách", tbBook.createBook()); tbPanel.addTab("Mượn Sách", tbBorrow.createBorrow()); tbPanel.addTab("Tìm kiếm", tbSearch.createSearch()); return tbPanel; } } c Thiết kế panel Bạn Đọc class tabReader implements ActionListener { private JPanel pnReader = new JPanel(); private JTextField txtID = new JTextField(); private JTextField txtName = new JTextField(); private JTextField txtBirthYear = new JTextField(); private JTextField txtGender = new JTextField(); private JTextField txtObject = new JTextField(); private JTextField txtAddress = new JTextField(); private JTextField txtNumPhone = new JTextField(); private JButton btAdd = new JButton("Thêm"); private JButton btFix = new JButton("Sửa"); private JButton btDel = new JButton("Xóa"); private JButton btLoad = new JButton("Load"); private JButton btReset = new JButton("Reset"); private JButton btSearch = new JButton("Tìm Kiếm"); private JTable dataTable = new JTable(); private DefaultTableModel tableModel = new DefaultTableModel(); private Vector dataRecord = new Vector(); private Vector dataTitle = new Vector(); public JPanel createtabReader() { pnReader.setLayout(null); JLabel lb = new JLabel(); lb.setBounds(5, 5, 95, 20); lb.setFont(new java.awt.Font("Time news roman", 0, 16)); lb.setText("Mã bạn đọc:"); pnReader.add(lb); txtID.setBounds(100, 5, 250, 20); pnReader.add(txtID); JLabel lb2 = new JLabel(); lb2.setBounds(5, 35, 95, 20); lb2.setFont(new java.awt.Font("Time news roman", 0, 16)); lb2.setText("Tên bạn đọc:"); pnReader.add(lb2); txtName.setBounds(100, 35, 250, 20); pnReader.add(txtName); JLabel lb3 = new JLabel(); lb3.setBounds(5, 65, 95, 20); 10 lb3.setFont(new java.awt.Font("Time news roman", 0, 16)); lb3.setText("Năm sinh:"); // Test // lb3.setBorder(border); pnReader.add(lb3); txtBirthYear.setBounds(100, 65, 250, 20); pnReader.add(txtBirthYear); JLabel lb4 = new JLabel(); lb4.setBounds(5, 95, 95, 20); lb4.setFont(new java.awt.Font("Time news roman", 0, 16)); lb4.setText("Giới tính:"); // Test // lb4.setBorder(border); pnReader.add(lb4); txtGender.setBounds(100, 95, 250, 20); pnReader.add(txtGender); JLabel lb5 = new JLabel(); lb5.setBounds(5, 125, 95, 20); lb5.setFont(new java.awt.Font("Time news roman", 0, 16)); lb5.setText("Đối tượng:"); pnReader.add(lb5); txtObject.setBounds(100, 125, 250, 20); pnReader.add(txtObject); JLabel lb6 = new JLabel(); lb6.setBounds(5, 155, 95, 20); lb6.setFont(new java.awt.Font("Time news roman", 0, 16)); lb6.setText("Địa chỉ:"); // Test // lb6.setBorder(border); pnReader.add(lb6); txtAddress.setBounds(100, 155, 250, 20); pnReader.add(txtAddress); JLabel lb7 = new JLabel(); lb7.setBounds(5, 185, 95, 20); lb7.setFont(new java.awt.Font("Time news roman", 0, 16)); lb7.setText("Điện thoại:"); // Test // lb7.setBorder(border); pnReader.add(lb7); txtNumPhone.setBounds(100, 185, 250, 20); pnReader.add(txtNumPhone); btAdd.setBounds(5, 225, 70, 25); btAdd.addActionListener(this); btFix.setBounds(95, 225, 70, 25); btFix.addActionListener(this); 13 } private void processUpdate() { String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { String command = "Update BanDoc Set TenBD = '"+txtName.getText( )+"', NamSinh = '"+txtBirthYear.getText()+"', GioiTinh = '"+txtGender.getText ()+"', DoiTuong = '"+txtObject.getText()+"', DiaChi = '"+txtAddress.getText() +"', SDT = '"+txtNumPhone.getText()+"' Where MaBD = '"+txtID.getText() +"'"; Statement stmt = cnn.createStatement(); stmt.executeUpdate(command); JOptionPane.showMessageDialog(pnReader, "Đã sửa thành công!!"); cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnReader, "Tuy vấn thất bại!!"); } } private void processDelete() { String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { String command = "Delete From BanDoc Where MaBD = '"+txtID.getT ext()+"'"; Statement stmt = cnn.createStatement(); stmt.executeUpdate(command); JOptionPane.showMessageDialog(pnReader, "Đã xóa thành cơng!!"); cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnReader, "Truy vấn thất bại!!"); } } private void processReset() { txtID.setText(""); txtName.setText(""); txtBirthYear.setText(""); txtGender.setText(""); txtObject.setText(""); txtAddress.setText(""); txtNumPhone.setText(""); } @Override public void actionPerformed(ActionEvent evt) { if(evt.getSource() == btAdd) processInsert(); 14 if(evt.getSource() == processUpdate(); if(evt.getSource() == processDelete(); if(evt.getSource() == processLoad(); if(evt.getSource() == processReset(); btFix) btDel) btLoad) btReset) } } d Thiết kế panel Sách class tabBook implements ActionListener { private JPanel pnBook = new JPanel(); private JTextField txtID = new JTextField(); private JTextField txtBookName = new JTextField(); private JTextField txtAuthor = new JTextField(); private JTextField txtPubCom = new JTextField(); private JTextField txtBookSelf = new JTextField(); private JTextField txtCost = new JTextField(); private JButton btAdd = new JButton("Thêm"); private JButton btFix = new JButton("Sửa"); private JButton btDel = new JButton("Xóa"); private JButton btLoad = new JButton("Load"); private JButton btReset = new JButton("Reset"); private JButton btSearch = new JButton("Tìm Kiếm"); private JTable dataTable = new JTable(); private DefaultTableModel tableModel = new DefaultTableModel(); private Vector dataRecord = new Vector(); private Vector dataTitle = new Vector(); public JPanel createBook() { pnBook.setLayout(null); JLabel lb = new JLabel(); lb.setBounds(5, 5, 95, 20); lb.setFont(new java.awt.Font("Time news roman", 0, 16)); lb.setText("Mã sách:"); pnBook.add(lb); txtID.setBounds(100, 5, 250, 20); pnBook.add(txtID); JLabel lb2 = new JLabel(); lb2.setBounds(5, 35, 95, 20); lb2.setFont(new java.awt.Font("Time news roman", 0, 16)); lb2.setText("Tên sách:"); pnBook.add(lb2); txtBookName.setBounds(100, 35, 250, 20); pnBook.add(txtBookName); JLabel lb3 = new JLabel(); lb3.setBounds(5, 65, 95, 20); lb3.setFont(new java.awt.Font("Time news roman", 0, 16)); 15 lb3.setText("Tác Giả:"); pnBook.add(lb3); txtAuthor.setBounds(100, 65, 250, 20); pnBook.add(txtAuthor); JLabel lb4 = new JLabel(); lb4.setBounds(5, 95, 95, 20); lb4.setFont(new java.awt.Font("Time news roman", 0, 16)); lb4.setText("NXB:"); pnBook.add(lb4); txtPubCom.setBounds(100, 95, 250, 20); pnBook.add(txtPubCom); JLabel lb5 = new JLabel(); lb5.setBounds(5, 125, 95, 20); lb5.setFont(new java.awt.Font("Time news roman", 0, 16)); lb5.setText("Kệ Sách:"); pnBook.add(lb5); txtBookSelf.setBounds(100, 125, 250, 20); pnBook.add(txtBookSelf); JLabel lb6 = new JLabel(); lb6.setBounds(5, 155, 95, 20); lb6.setFont(new java.awt.Font("Time news roman", 0, 16)); lb6.setText("Đơn Giá:"); pnBook.add(lb6); txtCost.setBounds(100, 155, 250, 20); pnBook.add(txtCost); btAdd.setBounds(5, 195, 70, 25); btAdd.addActionListener(this); btFix.setBounds(95, 195, 70, 25); btFix.addActionListener(this); btDel.setBounds(185, 195, 70, 25); btDel.addActionListener(this); btReset.setBounds(275, 195, 70, 25); btReset.addActionListener(this); btLoad.setBounds(665, 425, 90, 25); btLoad.addActionListener(this); pnBook.add(btAdd); pnBook.add(btFix); pnBook.add(btDel); pnBook.add(btReset); pnBook.add(btLoad); //pnReader.add(btClean); /// pnBook.add(createDataTab()); return pnBook; } 16 private JTable createDataTab() { dataTitle.add("MaSach"); dataTitle.add("TenSach"); dataTitle.add("TacGia"); dataTitle.add("NBX"); dataTitle.add("KeSach"); dataTitle.add("DonGia"); dataTable.setModel(new DefaultTableModel(dataRecord, dataTitle)); Border border = BorderFactory.createLineBorder(Color.BLACK); dataTable.setBorder(border); dataTable.setBounds(360, 5, 710, 400); dataTable.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mouseClicked(MouseEvent evt) { int row = dataTable.rowAtPoint(evt.getPoint()); txtID.setText(dataTable.getModel().getValueAt(row, 0)+""); txtBookName.setText(dataTable.getModel().getValueAt(row, 1)+""); txtAuthor.setText(dataTable.getModel().getValueAt(row, 2)+""); txtPubCom.setText(dataTable.getModel().getValueAt(row, 3)+""); txtBookSelf.setText(dataTable.getModel().getValueAt(row, 4)+""); txtCost.setText(dataTable.getModel().getValueAt(row, 5)+""); } }); return dataTable; } private void processLoad() { dataRecord.clear(); String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { System.out.println("Success"); String command = "Select * From Sach"; Statement stmt = cnn.createStatement(); ResultSet result = stmt.executeQuery(command); while(result.next()) { Vector data = new Vector(); data.add(result.getString(1)); data.add(result.getString(2)); data.add(result.getString(3)); data.add(result.getString(4)); data.add(result.getString(5)); data.add(result.getString(6)); dataRecord.add(data); dataTable.setModel(new DefaultTableModel(dataRecord, dataTitle) ); } cnn.close(); } catch(SQLException ex) 17 { JOptionPane.showMessageDialog(pnBook, "Truy vấn thất bại!!"); } } private void processInsert() { String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { String command = "Insert Into Sach Values ('"+txtID.getText()+"', '"+t xtBookName.getText()+"', '"+txtAuthor.getText()+"', '"+txtPubCom.getText() +"', '"+txtBookSelf.getText()+"', '"+txtCost.getText()+"')"; Statement stmt = cnn.createStatement(); stmt.executeUpdate(command); JOptionPane.showMessageDialog(pnBook, "Đã thêm thành công!!"); cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnBook, "Truy vấn thất bại!!"); } } private void processUpdate() { String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { String command = "Update Sach Set TenSach = '"+txtBookName.getT ext()+"', TacGia = '"+txtAuthor.getText()+"', NXB = '"+txtPubCom.getText() +"', KeSach = '"+txtBookSelf.getText()+"', DonGia = '"+txtCost.getText()+"' Where MaSach = '"+txtID.getText()+"'"; Statement stmt = cnn.createStatement(); stmt.executeUpdate(command); JOptionPane.showMessageDialog(pnBook, "Đã sửa thành công!!"); cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnBook, "Truy vấn thất bại!!"); } } private void processDelete() { String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { String command = "Delete From Sach Where MaSach = '"+txtID.getTe xt()+"'"; Statement stmt = cnn.createStatement(); stmt.executeUpdate(command); JOptionPane.showMessageDialog(pnBook, "Đã xóa thành cơng!!"); 18 cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnBook, "Truy vấn thất bại!!"); } } private void processReset() { txtID.setText(""); txtBookName.setText(""); txtAuthor.setText(""); txtPubCom.setText(""); txtBookSelf.setText(""); txtCost.setText(""); } @Override public void actionPerformed(ActionEvent evt) { if(evt.getSource() == btLoad) processLoad(); if(evt.getSource() == btAdd) processInsert(); if(evt.getSource() == btFix) processUpdate(); if(evt.getSource() == btDel) processDelete(); if(evt.getSource() == btReset) processReset(); } } e Thiết kế panel Mượn Sách class tabBorrow implements ActionListener { private JPanel pnBorrow = new JPanel(); private JTextField txtIDReader = new JTextField(); private JTextField txtIDBook = new JTextField(); private JTextField txtBorrowDate = new JTextField(); private JTextField txtReturnDate = new JTextField(); private JTextField txtBRMoney = new JTextField(); private JButton btAdd = new JButton("Thêm"); private JButton btFix = new JButton("Sửa"); private JButton btDel = new JButton("Xóa"); private JButton btLoad = new JButton("Load"); private JButton btReset = new JButton("Reset"); private JButton btSearch = new JButton("Tìm Kiếm"); private JTable dataTable = new JTable(); private DefaultTableModel tableModel = new DefaultTableModel(); private Vector dataRecord = new Vector(); private Vector dataTitle = new Vector(); public JPanel createBorrow() { pnBorrow.setLayout(null); 19 JLabel lb = new JLabel(); lb.setBounds(5, 5, 95, 20); lb.setFont(new java.awt.Font("Time news roman", 0, 16)); lb.setText("Mã bạn đọc:"); pnBorrow.add(lb); txtIDReader.setBounds(100, 5, 250, 20); pnBorrow.add(txtIDReader); JLabel lb2 = new JLabel(); lb2.setBounds(5, 35, 95, 20); lb2.setFont(new java.awt.Font("Time news roman", 0, 16)); lb2.setText("Mã sách:"); pnBorrow.add(lb2); txtIDBook.setBounds(100, 35, 250, 20); pnBorrow.add(txtIDBook); JLabel lb3 = new JLabel(); lb3.setBounds(5, 65, 95, 20); lb3.setFont(new java.awt.Font("Time news roman", 0, 16)); lb3.setText("Ngày mượn:"); pnBorrow.add(lb3); txtBorrowDate.setBounds(100, 65, 250, 20); pnBorrow.add(txtBorrowDate); JLabel lb4 = new JLabel(); lb4.setBounds(5, 95, 95, 20); lb4.setFont(new java.awt.Font("Time news roman", 0, 16)); lb4.setText("Ngày trả:"); pnBorrow.add(lb4); txtReturnDate.setBounds(100, 95, 250, 20); pnBorrow.add(txtReturnDate); JLabel lb5 = new JLabel(); lb5.setBounds(5, 125, 95, 20); lb5.setFont(new java.awt.Font("Time news roman", 0, 16)); lb5.setText("Giá thuê:"); pnBorrow.add(lb5); txtBRMoney.setBounds(100, 125, 250, 20); pnBorrow.add(txtBRMoney); btAdd.setBounds(5, 195, 70, 25); btAdd.addActionListener(this); btFix.setBounds(95, 195, 70, 25); btFix.addActionListener(this); btDel.setBounds(185, 195, 70, 25); btDel.addActionListener(this); btReset.setBounds(275, 195, 70, 25); btReset.addActionListener(this); btLoad.setBounds(665, 425, 90, 25); btLoad.addActionListener(this); pnBorrow.add(btAdd); pnBorrow.add(btFix); pnBorrow.add(btDel); pnBorrow.add(btReset); pnBorrow.add(btLoad); pnBorrow.add(createDataTab()); return pnBorrow; } private JTable createDataTab() { dataTitle.add("MaBD"); dataTitle.add("MaSach"); dataTitle.add("NgayMuon"); dataTitle.add("NgayTra"); 20 dataTitle.add("GiaThue"); dataTable.setModel(new DefaultTableModel(dataRecord, dataTitle)); Border border = BorderFactory.createLineBorder(Color.BLACK); dataTable.setBorder(border); dataTable.setBounds(360, 5, 710, 400); dataTable.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mouseClicked(MouseEvent evt) { int row = dataTable.rowAtPoint(evt.getPoint()); txtIDReader.setText(dataTable.getModel().getValueAt(row, 0)+""); txtIDBook.setText(dataTable.getModel().getValueAt(row, 1)+""); txtBorrowDate.setText(dataTable.getModel().getValueAt(row, 2)+"") ; txtReturnDate.setText(dataTable.getModel().getValueAt(row, 3)+""); txtBRMoney.setText(dataTable.getModel().getValueAt(row, 4)+""); } }); return dataTable; } private void processLoad() { dataRecord.clear(); String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { System.out.println("Success"); String command = "Select * From MuonSach"; Statement stmt = cnn.createStatement(); ResultSet result = stmt.executeQuery(command); while(result.next()) { Vector data = new Vector(); data.add(result.getString(1)); data.add(result.getString(2)); data.add(result.getString(3)); data.add(result.getString(4)); data.add(result.getString(5)); dataRecord.add(data); dataTable.setModel(new DefaultTableModel(dataRecord, dataTitle) ); } cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnBorrow, "Truy vấn thất bại!!"); } } private void processInsert() { 21 String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { String command = "Insert Into MuonSach Values ('"+txtIDReader.getT ext()+"', '"+txtIDBook.getText()+"', '"+txtBorrowDate.getText()+"', '"+txtRetu rnDate.getText()+"', '"+txtBRMoney.getText()+"')"; Statement stmt = cnn.createStatement(); stmt.executeUpdate(command); JOptionPane.showMessageDialog(pnBorrow, "Đã thêm thành công!!"); cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnBorrow, "Truy vấn thất bại!!"); } } private void processUpdate() { String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { String command = "Update MuonSach Set NgayMuon = '"+txtBorrow Date.getText()+"', NgayTra = '"+txtReturnDate.getText()+"', GiaTien = '"+txt BRMoney.getText()+"' Where MaBD = '"+txtIDReader.getText()+"' And MaSac h = '"+txtIDBook.getText()+"'"; Statement stmt = cnn.createStatement(); stmt.executeUpdate(command); JOptionPane.showMessageDialog(pnBorrow, "Đã sửa thành công!!"); cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnBorrow, "Truy vấn thất bại!!"); } } private void processDelete() { String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { String command = "Delete From MuonSach Where MaBD = '"+txtIDRe ader.getText()+"'"; Statement stmt = cnn.createStatement(); stmt.executeUpdate(command); JOptionPane.showMessageDialog(pnBorrow, "Đã xóa thành cơng!!"); cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnBorrow, "Truy vấn thất bại!!"); } } 22 private void processReset() { txtIDReader.setText(""); txtIDBook.setText(""); txtBorrowDate.setText(""); txtReturnDate.setText(""); txtBRMoney.setText(""); } @Override public void actionPerformed(ActionEvent evt) { if(evt.getSource() == btLoad) processLoad(); if(evt.getSource() == btAdd) processInsert(); if(evt.getSource() == btFix) processUpdate(); if(evt.getSource() == btDel) processDelete(); if(evt.getSource() == btReset) processReset(); } } f Thiết kế panel Tìm Kiếm class tabSearch implements ActionListener { private JPanel pnSearch = new JPanel(); private JTextField txtSearch = new JTextField(); private JButton btSearch = new JButton("Tìm kiếm"); private JTable dataTable = new JTable(); private DefaultTableModel tableModel = new DefaultTableModel(); private Vector dataRecord = new Vector(); private Vector dataTitle = new Vector(); public JPanel createSearch() { pnSearch.setLayout(null); JLabel lb = new JLabel(); lb.setBounds(70, 25, 120, 25); lb.setFont(new java.awt.Font("Time news roman", 0, 16)); lb.setText("Tên bạn đọc:"); pnSearch.add(lb); txtSearch.setBounds(191, 25, 450, 25); pnSearch.add(txtSearch); btSearch.setBounds(675, 25, 100, 25); btSearch.addActionListener(this); pnSearch.add(btSearch); pnSearch.add(createDataTab()); return pnSearch; } private JTable createDataTab() 23 { dataTitle.add("MaSach"); dataTitle.add("NgayMuon"); dataTitle.add("NgayTra"); dataTitle.add("GiaThue"); dataTable.setModel(new DefaultTableModel(dataRecord, dataTitle)); Border border = BorderFactory.createLineBorder(Color.BLACK); dataTable.setBorder(border); dataTable.setBounds(5, 100, 1068, 356); return dataTable; } private void processSearch() { dataRecord.clear(); String URL = "jdbc:sqlserver://localhost:1433; databaseName = TestJava ; user = sa; password = 123456"; try(Connection cnn = DriverManager.getConnection(URL);) { System.out.println("Success"); String ret = null, cmd1; cmd1 = "Select MaBD From BanDoc Where TenBD = '"+txtSearch.getT ext()+"'"; Statement stmt1 = cnn.createStatement(); ResultSet rest = stmt1.executeQuery(cmd1); while(rest.next()) { ret = rest.getString(1); } String command = "Select MuonSach.MaSach, MuonSach.NgayMuon, MuonSach.NgayTra, MuonSach.GiaTien From MuonSach, BanDoc Where BanD oc.MaBD = '"+ret+"' And BanDoc.MaBD = MuonSach.MaBD "; Statement stmt = cnn.createStatement(); ResultSet result = stmt.executeQuery(command); while(result.next()) { Vector data = new Vector(); data.add(result.getString(1)); data.add(result.getString(2)); data.add(result.getString(3)); data.add(result.getString(4)); dataRecord.add(data); dataTable.setModel(new DefaultTableModel(dataRecord, dataTitle) ); } cnn.close(); } catch(SQLException ex) { JOptionPane.showMessageDialog(pnSearch, "Truy vấn thất bại!!"); } } private void processReset() { txtSearch.setText(""); } 24 @Override public void actionPerformed(ActionEvent evt) { if(evt.getSource() == btSearch) processSearch(); } } Giao diện chương trình sau xây dựng a Giao diện đăng nhập b Giao diện tab bạn đọc c Giao diện tab sách 25 d Giao diện tab mượn sách e Giao diện tab tìm kiếm • Kết đạt được: KẾT LUẬN 26 - Đã biết xây dựng hệ thống ngơn ngữ Java có kết nói sở liệu Đã thiết kế xây dựng đầy đử chức hệ thống - Đã có sở liệu cho hệ thống - Đã có giao diện cho người dùng • Hướng phát triển - Tiếp tục nghiên cứu phát triển phần mềm hoàn thiện sửa chữa sai xót - Tiếp tục nghiên cứu để làm cho phầm mềm thêm có nhiều tính - Tiếp tục phát triển giao diện để tăng trải nghiệm người dùng, không làm người dùng nhàm chán với sản phẩm 27 DANH MỤC TÀI LIỆU THAM KHẢO Giáo trình ngơn ngữ lập trình Java – Đại học cơng nghệ thông tin https://stackoverflow.com https://vietjack.com https://wikipedia.org https://daynhauhoc.com

Ngày đăng: 13/04/2020, 10:34

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan