Manajemen informasi buku mirip dengan manajemen informasi pembaca. Ini termasuk dua bagian: Penambahan buku dan kueri buku dan modifikasi. Penambahan informasi buku memerlukan input delapan informasi yang benar dari buku ini, dan nomor ISBN unik. Selama proses implementasi, ISBN harus digunakan sebagai kunci utama. Mari kita lihat proses implementasi:
Buku dalam database:
Proses menambahkan buku:
Konten basis data ditambahkan:
Kueri dan modifikasi buku: Proses kueri dapat ditanya sesuai dengan kondisi yang berbeda, seperti ISBN, penulis, penerbit, nama buku, dll., Dan semua buku ditampilkan dalam daftar secara default. Setelah memasuki kondisi kueri, hasil kueri ditampilkan dalam tabel.
Modul ini menggunakan panel tag, yang dapat dengan mudah meminta informasi yang diperlukan. Ketika proses modifikasi memasukkan nomor ISBN dan tekan ENTER untuk menampilkan semua informasi buku di bawah ini, maka semua konten akan diperbarui setelah memodifikasi informasi buku.
Kueri Buku:
Ubah Informasi Buku:
Tambahkan kode sumber buku sebagai berikut: addbook.java
Paket PDSU.BBM.View; impor java.awt.borderlayout; impor java.awt.flowlayout; impor java.awt.font; impor java.awt.image; impor java.awt.event.actionevent; impor java.awt.event.actionlistener; impor java.text.parseexception; impor java.text.simpledateFormat; impor java.util.date; impor java.util.iterator; impor java.util.list; impor javax.swing.buttongroup; impor javax.swing.imageicon; impor javax.swing.jbutton; impor javax.swing.jcombobox; impor javax.swing.jframe; impor javax.swing.jlabel; impor javax.swing.joptionpane; impor javax.swing.jpanel; impor javax.swing.jtextfield; impor pdsu.bbm.dao.bookdao; impor pdsu.bbm.dao.booktypedao; impor pdsu.bbm.model.book; impor pdsu.bbm.model.booktype; Public Class AddBook memperluas JFRame mengimplementasikan ActionListener {private JPanel ContentPane; Private Jlabel Label_1; Private Jlabel LBLL; Private Jlabel Label_2; Private Jlabel Label_3; Private Jlabel Label_4; Private Jlabel Label_5; Jpanel centerppanel; JButton Button1, Button2, Button3; Private JComboBox ComboBox_1; Image Icon = New ImageIcon ("Image/Icon.png"). GetImage (); Private JTextfield Isbnfiled; Private JTextfield Publishfield; Private JTextField PublishDateField; Private JTextfield Publishtime; Private JTextField UnitPricefield; Private JTextfield Booknamefield; Private JTextfield Otorfiled; public static void main (string [] args) {new AddBook (); } public addBook () {settitle ("Tambahkan informasi buku"); setsize (555, 333); seticonimage (ikon); setLocationRelativeto (null); settitle ("Tambahkan informasi buku"); setsize (555, 334); setLocationRelativeto (null); setVisible (true); centerppanel = jpanel baru (); centeppanel.setlayout (null); Jlabel label1 = new jlabel ("isbn:"); label1.setfont (font baru ("宋体", font.plain, 20)); label1.setbounds (71, 26, 95, 42); centeppanel.add (label1); IsBnFiled = new jTextField (); Isbnfiled.setbounds (120, 30, 110, 30); centeppanel.add (isbnfiled); IsBnfiled.setColumns (10); Label jlabel = jlabel baru ("kategori:"); label.setfont (font baru ("zongyi", font.plain, 20)); label.setbounds (306, 30, 116, 35); centeppanel.add (label); label_1 = jLabel baru ("Judul buku:"); label_1.setfont (font baru ("宋体", font.plain, 20)); label_1.setbounds (50, 75, 154, 50); centeppanel.add (label_1); lbll = new jlabel ("penulis:"); lbll.setfont (font baru ("宋体", font.plain, 20)); lbll.setbounds (306, 75, 137, 50); centeppanel.add (lbll); label_2 = JLabel baru ("Penerbit:"); label_2.setfont (font baru ("宋体", font.plain, 20)); label_2.setbounds (50, 130, 154, 50); centeppanel.add (label_2); label_3 = jLabel baru ("宋体:"); label_3.setfont (font baru ("宋体", font.plain, 20)); label_3.setbounds (285, 135, 137, 50); centeppanel.add (label_3); publishfield = new jTextField (); publishfield.setColumns (10); publishfield.setbounds (120, 143, 110, 30); centeppanel.add (publishfield); publishDateField = new jTextField (); publishDateField.setColumns (10); publishDateField.setbounds (380, 143, 120, 30); centeppanel.add (publishDateField); label_4 = jLabel baru ("Jumlah pencetakan:"); label_4.setFont (font baru ("宋体", font.plain, 20)); label_4.setbounds (28, 190, 154, 50); centeppanel.add (label_4); publishTime = new jTextField (); publishtime.setColumns (10); publishtime.setbounds (120, 203, 110, 30); centeppanel.add (waktu publish); label_5 = jLabel baru ("宋体:"); label_5.setfont (font baru ("宋体", font.plain, 20)); label_5.setbounds (305, 194, 84, 42); centeppanel.add (label_5); unitpricefield = new jTextField (); unitpricefield.setColumns (10); unitpricefield.setbounds (380, 203, 120, 30); centeppanel.add (unitpricefield); getContentPane (). Add (centePpanel, borderlayout.center); booknamefield = new jTextField (); booknamefield.setColumns (10); booknamefield.setbounds (120, 86, 110, 30); centeppanel.add (booknamefield); woritorfiled = new jTextField (); wornorfiled.setColumns (10); woritfiled.setbounds (380, 86, 120, 30); centeppanel.add (authorfiled); Daftar <BookType> rs = booktypedao.selectbookType (); Iterator <BookType> iterator = rs.iterator (); String [] alltypeName = string baru [rs.size ()]; int i = 0; while (iterator.hasnext ()) {string typename = iterator.next (). getTypeName (); AlltypeName [i] = typename; i ++; } combobox_1 = jcomboBox baru (alltypeName); combobox_1.setbounds (380, 30, 120, 30); combobox_1.setfont (font baru ("安体", font.plain, 16)); centeppanel.add (combobox_1); Grup buttongroup = new Buttongroup (); Jpanel panel2 = jpanel baru (); panel2.setLayout (flowlayout baru ()); tombol1 = jbutton baru ("add"); button1.setfont (font baru ("宋体", font.plain, 20)); button2 = jbutton baru ("宋体", font.plain, 20)); tombol3 = jbutton baru ("reset"); tombol3.setFont (font baru ("宋体", font.plain, 20)); panel2.add (tombol1); panel2.add (tombol3); panel2.add (tombol2); Button1.addactionListener (ini); tombol2.addactionListener (ini); tombol3.addactionListener (ini); getContentPane (). Tambah (Panel2, BorderLayout.South); setVisible (true); } @SuppressWarnings ("tidak digunakan") public void actionPerformed (actionEvent e) {if (e.getSource () == tombol1) {string isBn = isBnFiled.getText (). ToString (); String bookname = booknamefield.getText (); String author = authorfiled.getText (); String selectType = comboBox_1.getSelectedItem (). ToString (); String publish = publishfield.getText (); String publishDate = publishDateField.getText (); String time = publishtime.getText (). Trim (); String harga = unitpricefield.getText (). Trim (); if (isbn.equals ("") || bookname.equals ("") || author.equals ("") || selectType.equals ("") || publish.equals ("") || publishdate.equals ("") || time.equals (") || harga kembali; } else {bookType bt = bookType baru (); Buku buku = buku baru (); book.setisbn (ISBN); book.setBookName (bookname); book.setauthor (penulis); BT.SetTyPeName (SelectType); int m = 0; Daftar <BookType> Daftar = booktypedao .SelectIdByTypeName (SelectType); Iterator <BookType> item = list.iterator (); while (ite.hasnext ()) {m = item.next (). getId (); } book.setTypeId (m + ""); book.setpublish (publikasi); coba {book.setPublishtime (integer.parseint (waktu)); book.setunitprice (integer.parseint (harga)); } catch (NumberFormateException E1) {E1.PrintStackTrace (); } SimpleDateFormat SDF = new SimpleDateFormat ("yyyy-mm-dd"); Tanggal regdate = null; coba {regdate = sdf.parse (publishDate); } catch (parseException e1) {joptionpane.showmessagealog (ini, "Harap masukkan tanggal yang benar"); publishDateField.setText (""); } book.setPublishDate (regdate); if (regdate == null) kembali; if (bookdao.selectbookbyisbn (isbn)! = null) {joptionpane.showmessagealog (ini, "nomor ISBN ini sudah ada"); kembali; } else if (bookdao.insertbook (buku)! = 0) {joptionpane.showmessagealog (this, "ditambahkan dengan sukses"); } lain jOptionPane.showmessage -(ini, "tambahkan gagal"); }} if (e.getSource () == tombol3) {isBnFiled.setText (""); booknamefield.setText (""); Authield.setText (""); publishfield.setText (""); publishDateField.setText (""); publishtime.setText (""); unitpricefield.setText (""); unitpricefield.setText (""); } if (e.getSource () == tombol2) {Dispose (); MainWindow baru (); }}} Kueri buku dan memodifikasi kode sumber: bookselectandmodify.java
Paket PDSU.BBM.View; impor java.awt.borderlayout; impor java.awt.dimension; impor java.awt.font; impor java.awt.gridlayout; impor java.awt.image; impor java.awt.event.actionevent; impor java.awt.event.actionlistener; impor java.text.simpledateFormat; impor java.util.arraylist; impor java.util.date; impor java.util.iterator; impor java.util.list; impor javax.swing.imageicon; impor javax.swing.jbutton; impor javax.swing.jcombobox; impor javax.swing.jframe; impor javax.swing.jlabel; impor javax.swing.joptionpane; impor javax.swing.jpanel; impor javax.swing.jscrollpane; impor javax.swing.jtabbedpane; impor javax.swing.jtable; impor javax.swing.jtextfield; impor javax.swing.swingconstants; impor javax.swing.table.tablemodel; impor pdsu.bbm.dao.bookdao; impor pdsu.bbm.dao.booktypedao; impor pdsu.bbm.model.book; impor pdsu.bbm.model.booktype; Kelas Publik BookselectAndModify Extends JFRame mengimplementasikan ActionListener {// Tetapkan nomor serial private private static final long serialversionuid = 1l; Jtabbedpane pribadi jtabbedpane; // Tag panel// selectJP query panel select_conditionJP drop-down list panel select_resultJP result button panel updateJP modification panel// updateJP modification panel, bookJP middle panel buttonJP2 button panel private JPanel selectJP, select_conditionJP, select_resultJP, buttonJP1, bookJP, updateJP, buttonJP2; // Private JTextfield SelectJtf, Isbnjtf, BooknameJtf, Otornjtf, printTimeJtf, publishjtf, publishdateJtf, unitpriceJtf; Private Jlabel Isbnjl, BooknameJl, Otornjl, CategoryJl, PrintTimeJl, Publishjl, PublishDateJl, UnitPriceJl; // Fokus! Private Jtable Jtable; // Tentukan Tabel Private JComboBox ChoiceJCB, BookTypeJCB; jscrollpane pribadi jscrollpane; Private JButton SelectJB, ExitJB, UpdateJB, ResetJB; // Tombol Kueri, Tombol Keluar, Modifikasi Tombol, Tutup Tombol TableModel Private GetSelect; Private String [] title = {"isbn", "urutan buku", "nama buku", "penulis", "penerbit", "tanggal publikasi", "jumlah cetakan", "harga unit"}; Image Icon = New ImageIcon ("Image/Icon.png"). GetImage (); BookselectAndModify () {super (); seticonimage (ikon); settitle ("kueri dan modifikasi buku"); setbounds (100, 100, 555, 400); setLocationRelativeto (null); // jtabbedpane tabjtabbedpane = new jtabbedpane (); Tambahkan (JtabbedPane); selectJp = jpanel baru (); pilihjp.setLayout (borderlayout baru ()); jtabbedpane.add ("kueri informasi buku", selectJp); // Panel Kondisi Kueri Select_conditionJp = new jpanel (); pilihanjcb = new jcomboBox (); String array [] = {"isbn", "nama buku", "urutan buku", "penulis", "penerbit", "semua"}; // diatur ke array satu dimensi untuk (int i = 0; i <array.length; i ++) {choicejcb.additem (array [i]); } select_conditionjp.add (pilihanjcb); SELECTJTF = JTextField baru (20); select_conditionjp.add (selectJtf); selectjp.add (select_conditionjp, borderlayout.north); // Panel hasil kueri select_resultjp = jpanel baru (); Bookdao dao = bookdao baru (); Daftar <Book> list = dao.selectbook (); jtable = jtable baru (getSelect (daftar), judul); //! Atur ukuran tabel jtable.setAutoresizeMode yang tidak berubah (jtable.auto_resize_off); //! Atur lebar kolom jtable.getColumnModel (). GetColumn (4) .setPreferredWidth (175); jscrollpane = jscrollpane baru (jtable); // Tambahkan tabel ke panel gulir // tunjukkan batas panel gulir jscrollpane.setpreferredSize (dimensi baru (450, 170)); select_resultjp.add (jscrollpane); selectjp.add (select_resultjp, borderlayout.center); // tombol query panel tomboljp1 = jpanel baru (); pilihjb = jbutton baru ("kueri"); pilihjb.setfont (font baru ("zongyi", font.plain, 20)); pilihjb.addactionListener (ini); tomboljp1.add (selectJb); EXITJB = JBUTTON baru ("EXIT"); exitjb.setfont (font baru ("zongyi", font.plain, 20)); Exitjb.addactionListener (ini); ButtonJP1.Add (EXITJB); pilihjp.add (tomboljp1, borderlayout.south); // halaman modifikasi informasi updatejp = jpanel baru (); updateJp.setLayout (borderlayout baru ()); jtabbedpane.addtab ("modifikasi informasi buku", updateJp); bookjp = jpanel baru (); gridlayout final gridlayout = gridlayout baru (8, 2); // atur jarak antara Borders gridlayout.setVGap (8); gridlayout.setHgap (8); bookjp.setlayout (gridlayout); Isbnjl = new jlabel ("isbn:"); Isbnjl.setfont (font baru ("宋体", font.plain, 20)); Isbnjl.sethorizontalalignment (swingconstants.center); bookjp.add (isbnjl); Isbnjtf = JTextField baru (20); Isbnjtf.addactionListener (ini); bookjp.add (isbnjtf); CategoryJl = JLabel baru ("Kategori:"); CategoryJl.setFont (font baru ("宋体", font.plain, 20)); CategoryJl.sethorizontalAlignment (swingconstants.center); bookjp.add (CategoryJl); // daftar daftar drop-down <BookType> list1 = booktypedao.selectbookType (); Iterator <BookType> it = list1.iterator (); String ty [] = string baru [list1.size ()]; int i = 0; booktypeJcb = new jcomboBox (); while (it.hasnext ()) {ty [i] = it.next (). getTypeName (); booktypeJcb.additem (ty [i]); i ++; } bookjp.add (booktypeJcb); booknameJl = Jlabel baru ("Judul buku:"); booknameJl.setfont (font baru ("宋体", font.plain, 20)); booknamejl.sethorizontalAlignment (swingconstants.center); bookjp.add (booknamejl); booknameJtf = new jTextField (); booknameJtf.setColumns (20); bookjp.add (booknameJtf); Authorjl = JLabel baru ("Author:"); Authorjl.setfont (font baru ("宋体", font.plain, 20)); Authorjl.sethorizontalAlignment (swingconstants.center); bookjp.add (authorjl); AuthorJtf = new jTextField (); Authorjtf.setColumns (20); bookjp.add (authorjtf); publishjl = jLabel baru ("Penerbit:"); publishjl.setfont (font baru ("宋体", font.plain, 20)); publishjl.sethorizontalAlignment (swingconstants.center); bookjp.add (publishjl); publishjtf = new jTextField (); bookjp.add (publishjtf); publishDateJl = new jlabel ("Tanggal yang diterbitkan:"); publishdateJl.setfont (font baru ("宋体", font.plain, 20)); publishDateJl.sethorizontalAlignment (swingconstants.center); bookjp.add (publishDateJl); publishDateJtf = new jTextField (); publishdateJtf.sethorizontalAlignment (swingconstants.left); bookjp.add (publishdateJtf); printTimeJl = JLabel baru ("Jumlah Pencetakan:"); printTimeJl.setfont (font baru ("宋体", font.plain, 20)); printTimeJl.sethorizontalAlignment (swingconstants.center); bookjp.add (printTimeJl); printTimeJtf = new jTextField (); bookjp.add (printtimejtf); unitpriceJl = JLabel baru ("Harga unit:"); unitpriceJl.setfont (font baru ("宋体", font.plain, 20)); unitpriceJl.sethorizontalAlignment (swingconstants.center); bookjp.add (unitpriceJl); unitpriceJtf = new jTextField (); bookjp.add (unitpriceJtf); // panel tombol // tombol desain panel tombolJP2 = new jpanel (); updateJb = jbutton baru ("ubah"); updateJb.setfont (font baru ("Songyi", font.plain, 20)); updateJb.addactionListener (ini); resetJb = jbutton baru ("tutup"); resetjb.setfont (font baru ("zongyi", font.plain, 20)); resetjb.addactionlistener (ini); tomboljp2.add (updateJb); tomboljp2.add (resetJb); updatejp.add (bookjp, borderlayout.center); updatejp.add (tomboljp2, borderlayout.south); setVisible (true); setDefaultCloseOperation (jframe.exit_on_close); } // Tambahkan tabel kueri ke Panel Private Object [] [] getSelect (daftar <Book> Daftar) {// TODO Metode yang dihasilkan secara otomatis objek stub [] [] objek = objek baru [list.size ()] [title.length]; untuk (int i = 0; i <list.size (); i ++) {buku buku = list.get (i); objek [i] [0] = book.getisbn (); Objek [1] = book.getTypeId (); // Buku Objek urutan [i] [2] = book.getBookName (); // Buku Nama Objek [i] [3] = book.getAuthor (); // objek penulis buku [i] [4] = book.getpublish (); // objek penerbit [i] [5] [5] [5] = Book.getPublish (); // objek publisher [i] [5] [5] [5] [book.getpublish (); // objek publisher [i] [5] [5] [5] [5] = bookpublish (); // publisher objek [i] [5] [5] [5] objek [i] [6] = book.getPublishtime (); // Jumlah objek pencetakan [i] [7] = book.getunitprice (); // Harga unit} pengembalian objek; } public void ActionPerformed (ActionEvent e) {// TODO METODE AUTO-AUTO-ENTERATED Stub if (e.getSource () == isbnjtf) {string isBn = isBnjtf.getText (); String typename = null; String id = bookdao.selectbookbyisbn (ISBN) .getTypeId (); typename = booktypedao.selectbyId (id); System.out.println (Typename); booktypeJcb.setselectedItem (typename); booknameJtf.settext (bookdao.selectbookbyisbn (ISBN) .getBookName ()); Authorjtf.settext (bookdao.selectbookbyisbn (ISBN) .getauthor ()); publishjtf.settext (bookdao.selectbookbyisbn (ISBN) .getPublish ()); publishdateJtf.settext (bookdao.selectbookbyisbn (ISBN) .getPublishDate ()); publishdateJtf.settext (bookdao.selectbookbyisbn (ISBN) .getPublishDate ()+""); printTimeJtf.setText (bookdao.selectbookbyisbn (ISBN) .getPublishtime ()+""); unitpriceJtf.settext (bookdao.selectbookbyisbn (ISBN) .getunitprice ()+""); } if (e.getSource () == selectJb) {// temukan int r = choiceJcb.getSelectedIndEndex (); if (r == 0) {string name = selectJtf.getText (). trim (); // Daftar Jenis Tabel Linear <Book> Daftar = ArrayList baru <Book> (); Buku buku = bookdao.selectbookbyisbn (nama); if (book == null) {joptionpane.showmessagealog (ini, "pengkodean ini tidak ada!"); } else {list.add (buku); Objek [] [] data = getSelect (daftar); jtable = jtable baru (data, judul); jtable.getColumnModel (). getColumn (4) .setPreferredWidth (175); jtable.setAutoresizeMode (jtable.auto_resize_off); jscrollpane.setViewPortView (jtable); }} if (r == 1) {// Informasi kueri berdasarkan nama buku string name = selectJtf.getText (). ToString (); Daftar <Book> Daftar = bookdao.selectbookbyname (nama); // Tentukan apakah tabel linier kosong jika (list == null || list.size () == 0) {joptionpane.showmessageDialog (ini, "Buku ini tidak ada!"); } else {objek [] [] data = getSelect (daftar); jtable = jtable baru (data, judul); tianjia (); }} if (r == 2) {// Informasi kueri berdasarkan urutan buku string name = selectJtf.getText (). ToString (); Daftar <Book> Daftar = Bookdao.SelectBookByType (Name); // Tentukan apakah tabel linier kosong jika (list == null || list.size () == 0) {joptionpane.showmessageDialog (ini, "Buku ini tidak ada!"); } else {objek [] [] data = getSelect (daftar); jtable = jtable baru (data, judul); tianjia (); }} if (r == 3) {// temukan string sesuai dengan nama penulis = selectJtf.getText (). ToString (); // Cari Daftar <Book> Daftar = Bookdao.SelectBookByAuthor (Nama); // Periksa apakah tabel linier kosong if (list == null || list.size () == 0) {joptionpane.showmessageDialog (ini, "Buku ini tidak ada!"); } else {objek [] [] data = getSelect (daftar); jtable = jtable baru (data, judul); tianjia (); }} if (r == 4) {// Cari string sesuai dengan nama penerbit = selectJtf.getText (). ToString (); Daftar <Book> Daftar = bookdao.selectbookbypublish (nama); if (list == null || list.size () == 0) {joptionpane.showmessagealog (ini, "Buku ini tidak ada!"); } else {objek [] [] data = getSelect (daftar); jtable = jtable baru (data, judul); tianjia (); }} if (r == 5) {List <Book> list = bookdao.selectbook (); }} if (e.getSource () == updateJb) {string isBn = isbnjtf.getText (). trim (); String typename = ((string) bookTypeJcb.getSelectedItem ()). Trim (); String id = booktypedao.selectbytypeName (typename); String bookname = booknameJtf.getText (); String worhor = authorjtf.getText (); String publish = publishjtf.getText (); String publishDate = publishDateJtf.getText (); SimpleDateFormat SDF = new SimpleDateFormat ("yyyy-mm-dd"); Tanggal regdate = null; coba {regdate = sdf.parse (publishDate); } catch (Exception e2) {e2.printstacktrace (); JOPTIONPANE.SHOWMESSADEIALOG (ini, "Harap masukkan format tanggal yang benar!"); } if (regdate == null) kembali; int publishtime = integer.parseint (printTimeJtf.getText ()); int unitprice = integer.parseint (unitpriceJtf.getText ()); // Encapsulate Buku Buku Objek = Buku Baru (); book.setisbn (ISBN); book.settypeid (id); book.setBookName (bookname); book.setauthor (penulis); book.setpublish (publikasi); book.setPublishDate (regdate); Book.SetPublishtime (waktu publish); book.setunitprice (unitprice); book.settypename (typename); int result = bookdao.update (buku); Daftar <Book> List2 = ArrayList baru <Book> (); daftar2.Add (buku); // System.out.println (List2); if (result == 1) {jOptionPane.showmessagedialog (ini, "berhasil dimodifikasi!"); Bookdao bk = bookdao baru (); Daftar <Book> daftar = bk.selectbook (); jtable = jtable baru (getSelect (daftar), judul); tianjia (); } else {jOptionPane.showmessageDialog (ini, "Modified Fag!"); }} if (e.getSource () == resetjb) {dispose (); MainWindow baru (); } if (e.getSource () == EXITJB) {Dispose (); MainWindow baru (); }} private void tianjia () {jtable.getColumnModel (). getColumn (4) .setPrefredWidth (175); jtable.setAutoresizeMode (jtable.auto_resize_off); jscrollpane.setViewPortView (jtable); } public static void main (string [] args) {new bookselectandmodify (); }}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.