本情報管理は、読者情報管理に似ています。本の追加と帳簿のクエリと変更の2つの部分が含まれています。本の追加には、本の8つの情報の正しい入力が必要であり、ISBN番号は一意です。実装プロセス中に、ISBNは主キーとして使用する必要があります。実装プロセスを見てみましょう。
データベース内の本:
本を追加するプロセス:
追加されたデータベースコンテンツ:
書籍のクエリと変更:ISBN、著者、出版社、本名など、さまざまな条件に応じてクエリプロセスをクエリすることができ、すべての本はデフォルトでリストに表示されます。クエリ条件を入力した後、クエリの結果がテーブルに表示されます。
このモジュールはタグパネルを使用して、必要な情報を簡単に照会できます。変更プロセスがISBN番号を入力し、Enterを押して以下のすべての本情報を表示すると、すべてのコンテンツが本情報を変更した後に更新されます。
予約クエリ:
本情報の変更:
本のソースコードを次のように追加する:addbook.java
パッケージpdsu.bbm.view; java.awt.borderlayoutをインポートします。 java.awt.flowlayoutをインポートします。 java.awt.fontをインポートします。 java.awt.imageをインポートします。 java.awt.event.actioneventをインポートします。 java.awt.event.actionlistenerをインポートします。 java.text.parseexceptionをインポートします。 java.text.simpledateformatをインポートします。 Import Java.util.date; java.util.iteratorをインポートします。 java.util.listをインポートします。 javax.swing.buttongroupをインポートします。 javax.swing.imageiconをインポートします。 javax.swing.jbuttonをインポートします。 javax.swing.jcomboboxをインポートします。 javax.swing.jframeをインポートします。 javax.swing.jlabelをインポートします。 javax.swing.joptionpaneをインポートします。 javax.swing.jpanelをインポートします。 javax.swing.jtextfieldをインポートします。 pdsu.bbm.dao.bookdaoをインポートします。 pdsu.bbm.dao.booktypedaoをインポートします。 import pdsu.bbm.model.book; pdsu.bbm.model.booktypeをインポートします。 Public Class Addbookは、JFRAMEを拡張し、ActionListener {private JPanel ContentSpane;プライベートJlabel label_1;プライベートJLabel LBLL;プライベートJlabel label_2;プライベートJlabel label_3;プライベートJlabel label_4;プライベートJlabel label_5; jpanel centpanel; jbutton button1、button2、button3;プライベートJComboBox ComboBox_1; Image Icon = new ImageIcon( "Image/Icon.png")。getImage();プライベートJTextfield isbnfiled;プライベートJTextfield Publishfield;プライベートJTextField PublishDateField; Private JTextfield Publishtime;プライベートJTextfield Unitpricefield;プライベートjtextfield booknamefield; Private JTextField AuthorFiled; public static void main(string [] args){new addbook(); } public addbook(){settitle( "Book Information"); SetSize(555、333); seticonimage(アイコン); setLocationRelativeto(null); Settitle( "Book Information"); SetSize(555、334); setLocationRelativeto(null); setVisible(true); centppanel = new JPanel(); centerppanel.setlayout(null); Jlabel label1 = new Jlabel( "isbn:"); label1.setfont(new Font( "宋体"、font.plain、20)); Label1.setbounds(71、26、95、42); centerppanel.add(label1); ISBNFILED = new JTextField(); isbnfiled.setbounds(120、30、110、30); centerppanel.add(isbnfiled); isbnfiled.setColumns(10); Jlabel label = new Jlabel( "category:"); label.setfont(new Font( "Zongyi"、font.plain、20)); label.setbounds(306、30、116、35); centerppanel.add(label); label_1 = new Jlabel( "Book Title:"); label_1.setfont(new Font( "宋体"、font.plain、20)); label_1.setbounds(50、75、154、50); centerppanel.add(label_1); lbll = new Jlabel( "著者:"); lbll.setfont(new Font( "宋体"、font.plain、20)); lbll.setbounds(306、75、137、50); centerppanel.add(lbll); label_2 = new Jlabel( "Publisher:"); label_2.setfont(new Font( "宋体"、font.plain、20)); label_2.setbounds(50、130、154、50); centrppanel.add(label_2); label_3 = new Jlabel( "宋体:"); label_3.setfont(new Font( "宋体"、font.plain、20)); label_3.setbounds(285、135、137、50); centerppanel.add(label_3); publishfield = new jtextfield(); publishfield.setColumns(10); publishfield.setbounds(120、143、110、30); centrppanel.add(publishfield); publishDateField = new JTextField(); publishdatefield.setColumns(10); publishdatefield.setbounds(380、143、120、30); centppanel.add(publishdatefield); label_4 = new Jlabel( "印刷数:"); label_4.setfont(new Font( "宋体"、font.plain、20)); label_4.setbounds(28、190、154、50); centerppanel.add(label_4); publishtime = new jtextfield(); publishtime.setColumns(10); publishtime.setbounds(120、203、110、30); centrppanel.add(publishtime); label_5 = new Jlabel( "宋体:"); label_5.setfont(new Font( "宋体"、font.plain、20)); label_5.setbounds(305、194、84、42); centerppanel.add(label_5); UnitPriceField = new JTextField(); Unitpricefield.setColumns(10); Unitpricefield.setbounds(380、203、120、30); centerppanel.add(unitpricefield); getContentPane()。add(conterppanel、borderlayout.center); booknamefield = new jtextfield(); booknamefield.setColumns(10); booknamefield.setbounds(120、86、110、30); centerppanel.add(booknamefield); authorfiled = new jtextfield(); authorfiled.setColumns(10); authorfiled.setbounds(380、86、120、30); centppanel.add(authorfiled); List <BookType> rs = booktypedao.selectbooktype(); iterator <booktype> iterator = rs.iterator(); string [] alltypename = new String [rs.size()]; int i = 0; while(iterator.hasnext()){string typename = iterator.next()。getTypename(); alltypename [i] = typename; i ++; } combobox_1 = new JComboBox(AlltyPename); combobox_1.setbounds(380、30、120、30); combobox_1.setfont(new Font( "安体"、font.plain、16)); centerppanel.add(combobox_1); BUTTONGROUPグループ= new ButtongRoup(); jPanel Panel2 = new JPanel(); panel2.setLayout(new FlowLayout()); button1 = new JButton( "add"); button1.setfont(new Font( "宋体"、font.plain、20)); Button2 = new JButton( "宋体"、font.plain、20)); button3 = new JButton( "Reset"); button3.setFont(new Font( "宋体"、font.plain、20));パネル2.Add(button1);パネル2.Add(button3);パネル2.Add(button2); button1.addactionlistener(this); Button2.AddActionListener(this); button3.addActionListener(this); getContentPane()。add(paner2、borderlayout.south); setVisible(true); } @suppresswarnings( "unsused")public void actionperformed(actionevent e){if(e.getSource()== button1){string isbn = isbnfiled.getText()。toString(); string bookname = booknamefield.getText();文字列著者= authorfiled.getText(); string selectType = combobox_1.getSelectedItem()。toString(); string publish = publishfield.getText(); string publishdate = publishdatefield.getText(); string time = publishtime.getText()。trim();文字列価格= unitpricefield.getText()。trim(); if(isbn.equals( "")|| bookname.equals( "")|| author.equals( "")|| selecttype.equals( "")|| publish.equals( "")|| publishdate.equals( "")|| time.equals( "")|| firsog( "戻る; } else {booktype bt = new booktype(); book book = new book(); book.setisbn(isbn); book.setbookName(BookName); book.setauthor(著者); bt.settypename(selectType); int m = 0;リスト<BookType> list = booktypedao .selectidbytypename(selectType); iterator <booktype> item = list.iterator(); while(ite.hasnext()){m = item.next()。getId(); } book.settypeid(m + ""); book.setpublish(publish); try {book.setpublishtime(integer.parseint(time)); book.setunitprice(integer.parseint(price)); } catch(numberformatexception e1){e1.printstacktrace(); } simpledateFormat sdf = new simpledateFormat( "yyyy-mm-dd");日付regdate = null; {regdate = sdf.parse(publishdate); } catch(parseexception e1){joptionpane.showmessagedialog(これ、「正しい日付を入力してください」); publishdatefield.settext( ""); } book.setpublishdate(regdate); if(regdate == null)return; if(bookdao.selectbookbyisbn(isbn)!= null){joptionpane.showmessageialog(this、 "このisbn番号は既に存在します");戻る; } else if(bookdao.insertbook(book)!= 0){joptionpane.showmessageialog(this、 "add addecly"); } else joptionpane.showmessageialog(this、 "add failed"); }} if(e.getSource()== button3){isbnfiled.settext( ""); booknamefield.settext( ""); stultyfield.settext( ""); publishfield.settext( ""); publishdatefield.settext( ""); publishtime.settext( ""); unitpricefield.settext( ""); unitpricefield.settext( ""); } if(e.getSource()== button2){dispose(); new MainWindow(); }}}ソースコードを予約および変更する:bookselectandModify.java
パッケージpdsu.bbm.view; java.awt.borderlayoutをインポートします。 java.awt.dimensionをインポートします。 java.awt.fontをインポートします。 java.awt.gridlayoutをインポートします。 java.awt.imageをインポートします。 java.awt.event.actioneventをインポートします。 java.awt.event.actionlistenerをインポートします。 java.text.simpledateformatをインポートします。 java.util.arraylistをインポートします。 Import Java.util.date; java.util.iteratorをインポートします。 java.util.listをインポートします。 javax.swing.imageiconをインポートします。 javax.swing.jbuttonをインポートします。 javax.swing.jcomboboxをインポートします。 javax.swing.jframeをインポートします。 javax.swing.jlabelをインポートします。 javax.swing.joptionpaneをインポートします。 javax.swing.jpanelをインポートします。 javax.swing.jscrollpaneをインポートします。 javax.swing.jtabbedpaneをインポートします。 javax.swing.jtableをインポートします。 javax.swing.jtextfieldをインポートします。 javax.swing.swingconstantsをインポートします。 javax.swing.table.tablemodelをインポートします。 pdsu.bbm.dao.bookdaoをインポートします。 pdsu.bbm.dao.booktypedaoをインポートします。 import pdsu.bbm.model.book; pdsu.bbm.model.booktypeをインポートします。 public class bookselectandModify拡張jframeを実装するactionlistener {//シリアル番号プライベートスタティック最終long serialversionuid = 1lを設定します。プライベートJtabbedpane jtabbedpane; //タグパネル// SelectJPクエリパネルSelect_ConditionJPドロップダウンリストパネルSELECT_RESULTJP結果パネルアップデートJP修正パネル//更新JP修正パネル、BookJP Middle Jp2ボタンパネルプライベートJPANEL SELECTJP、SELECT_CONDITIONJP、SELECT_RESULTJP、BUTTOANJP1、BOUTMJP、BOTTUNTJP2; // private jtextfield selectjtf、isbnjtf、booknamejtf、authorjtf、printtimejtf、publishjtf、publishdatejtf、unitpricejtf; private Jlabel isbnjl、booknamejl、authorjl、categoryjl、printtimejl、publishjl、publishdatejl、unitpricejl; // 集中! private jtable jtable; // tableプライベートjcombobox choicejcb、booktypejcbを定義します。プライベートjscrollpane jscrollpane; Private jbutton selectjb、exitjb、updatejb、resetjb; // queryボタン、exitボタン、ボタンの変更、閉じるボタンプライベートテーブルデルgetSelect; private string [] title = {"isbn"、 "book sequence"、 "book name"、 "author"、 "Publisher"、 "Publication of Prints"、 "Prints"、 "unit price"}; Image Icon = new ImageIcon( "Image/Icon.png")。getImage(); public bookselectandModify(){super(); seticonimage(アイコン); Settitle( "Book Query and Modification");セットバウンド(100、100、555、400); setLocationRelativeto(null); // jtabbedpane tabjtabbedpane = new jtabbedpane(); add(jtabbedpane); selectjp = new JPanel(); selectjp.setlayout(new borderlayout()); jtabbedpane.add( "Book Information Query"、SelectJP); //クエリ条件パネルSELECT_CONDITIONJP = new JPanel(); choicejcb = new jcombobox(); string array [] = {"isbn"、 "book name"、 "book sequence"、 "author"、 "publisher"、 "all"}; //(int i = 0; i <array.length; i ++){choicejcb.additem(array [i]); } select_conditionjp.add(choicejcb); selectjtf = new JTextField(20); select_conditionjp.add(selectjtf); selectjp.add(select_conditionjp、borderlayout.north); // query resultパネルselect_resultjp = new JPanel(); bookdao dao = new bookdao();リスト<book> list = dao.selectbook(); jtable = new jtable(getSelect(list)、title); //!変更されていないjtable.setAutoresizeMode(jtable.auto_resize_off); //!列幅jtable.getColumnModel()。getColumn(4).setPreferredWidth(175)を設定します。 jscrollpane = new jscrollpane(jtable); //スクロールパネルにテーブルを追加//スクロールパネルの境界jscrollpane.setpreferredsize(new Dimension(450、170)); select_resultjp.add(jscrollpane); selectjp.add(select_resultjp、borderlayout.center); //クエリボタンパネルbuttonjp1 = new JPanel(); selectjb = new jbutton( "query"); selectjb.setfont(new Font( "Zongyi"、font.plain、20)); selectjb.addactionlistener(this); buttonjp1.add(selectjb); exitjb = new jbutton( "exit"); exitjb.setfont(new Font( "Zongyi"、font.plain、20)); exitjb.addactionlistener(this); buttonjp1.add(exitjb); selectjp.add(buttonjp1、borderlayout.south); //情報の変更ページupdatejp = new JPanel(); updatejp.setlayout(new borderlayout()); jtabbedpane.addtab( "Book Information Modification"、updateJP); bookjp = new JPanel(); final gridlayout gridlayout = new gridlayout(8、2); //境界間の距離を設定しますgridlayout.setvgap(8); gridlayout.sethgap(8); bookjp.setlayout(gridlayout); isbnjl = new Jlabel( "isbn:"); isbnjl.setfont(new Font( "宋体"、font.plain、20)); isbnjl.sethorizontalAlignment(SwingConstants.Center); bookjp.add(isbnjl); isbnjtf = new JTextfield(20); isbnjtf.AddActionListener(this); bookjp.add(isbnjtf); categoryjl = new Jlabel( "category:"); categoryjl.setfont(new Font( "宋体"、font.plain、20)); categoryjl.sethorizontalAlignment(SwingConstants.Center); bookjp.add(categoryjl); //ドロップダウンリスト<booktype> list1 = booktypedao.selectbooktype(); iterator <booktype> it = list1.iterator(); string ty [] = new String [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 = new Jlabel( "Book Title:"); booknamejl.setfont(new Font( "宋体"、font.plain、20)); booknamejl.sethorizontalalignment(swingconstants.center); bookjp.add(booknamejl); booknamejtf = new jtextfield(); booknamejtf.setColumns(20); bookjp.add(booknamejtf);著者jl = new Jlabel( "著者:"); authorjl.setfont(new Font( "宋体"、font.plain、20)); authorjl.sethorizontalAlignment(SwingConstants.Center); bookjp.add(authorjl); authorjtf = new jtextfield(); authorjtf.setColumns(20); bookjp.add(authorjtf); publishjl = new Jlabel( "Publisher:"); publishjl.setfont(new Font( "宋体"、font.plain、20)); publishjl.sethorizontalAlignment(SwingConstants.Center); bookjp.add(publishjl); publishjtf = new jtextfield(); bookjp.add(publishjtf); publishdatejl = new Jlabel( "公開日:"); publishdatejl.setFont(new Font( "宋体"、font.plain、20)); publishdatejl.sethorizontalAlignment(SwingConstants.Center); bookjp.add(publishdatejl); publishdatejtf = new jtextfield(); publishdatejtf.sethorizontalAlignment(swingconstants.left); bookjp.add(publishdatejtf); printtimejl = new Jlabel( "印刷数:"); printtimejl.setfont(new Font( "宋体"、font.plain、20)); printtimejl.sethorizontalalignment(swingconstants.center); bookjp.add(printtimejl); printtimejtf = new jtextfield(); bookjp.add(printtimejtf); unitpricejl = new Jlabel( "単価:"); unitpricejl.setfont(new Font( "宋体"、font.plain、20)); unitpricejl.sethorizontalAlignment(SwingConstants.Center); bookjp.add(unitpricejl); unitpricejtf = new jtextfield(); bookjp.add(unitpricejtf); //ボタンパネル//ボタンパネルデザインbuttonjp2 = new JPanel(); updatejb = new jbutton( "Modify"); updatejb.setfont(new Font( "songyi"、font.plain、20)); updatejb.addactionlistener(this); resetjb = new jbutton( "close"); resetjb.setfont(new Font( "Zongyi"、font.plain、20)); resetjb.addactionlistener(this); buttonjp2.add(updatejb); buttonjp2.add(resetjb); updatejp.add(bookjp、borderlayout.center); updatejp.add(buttonjp2、borderlayout.south); setVisible(true); setDefaultCloseoperation(jframe.exit_on_close); } //クエリテーブルをパネルプライベートオブジェクトに追加[] [] [] getSelect(list <book> list){// todo auto-enerated method stub object [] [] objects = new object [list.size()] [title.length]; for(int i = 0; i <list.size(); i ++){book book = list.get(i);オブジェクト[i] [0] = book.getisbn(); objects [1] = book.getTypeid(); // book sequence objects [i] [2] = book.getbookname(); // book name objects [i] [3] = book.getauthor(); // book著者オブジェクト[i] [4] = book.getpublish(); Objects [i] [6] = book.getPublishTime(); //印刷オブジェクトの数[i] [7] = book.getunitprice(); // unit price} return objects; } public void ActionPerformed(actionEvent e){// todo auto-formatedメソッドスタブif(e.getsource()== isbnjtf){string isbn = isbnjtf.getText();文字列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){// int r = Choicejcb.getSelectedIndex(); if(r == 0){string name = selectjtf.getText()。trim(); //リニアテーブルタイプのリスト<book>リスト= new arrayList <book>(); book book = bookdao.selectbookbyisbn(name); if(book == null){joptionpane.showmessagedialog(これ、 "このエンコードは存在しません!"); } else {list.add(book); Object [] [] [] data = getSelect(list); jtable = new jtable(data、title); jtable.getColumnModel()。getColumn(4).setPreferredWidth(175); jtable.setautoreizeMode(jtable.auto_resize_off); jscrollpane.setviewportview(jtable); }} if(r == 1){// book name name string name = selectjtf.getText()。toString();リスト<book>リスト= bookdao.selectbookbyname(name); //線形テーブルが空であるかどうかを判断します(list == null || list.size()== 0){joptionpane.showmessageialog(これ、「本は存在しません!」); } else {object [] [] data = getSelect(list); jtable = new jtable(data、title); Tianjia(); }} if(r == 2){// book sequence string name = selectjtf.getText()。toString();リスト<book> list = bookdao.selectbookbytype(name); //線形テーブルが空であるかどうかを判断します(list == null || list.size()== 0){joptionpane.showmessageialog(これ、「本は存在しません!」); } else {object [] [] data = getSelect(list); jtable = new jtable(data、title); Tianjia(); }} if(r == 3){//著者name = selectjtf.getText()。toString(); //リストの検索<book>リスト= bookdao.selectbookbyauthor(name); //線形テーブルが空であるかどうかを確認します(list == null || list.size()== 0){joptionpane.showmessageialog(これ、「この本は存在しません!」); } else {object [] [] data = getSelect(list); jtable = new jtable(data、title); Tianjia(); }} if(r == 4){//出版社に従って文字列を検索= selectjtf.getText()。toString();リスト<book>リスト= bookdao.selectbookbypublish(name); if(list == null || list.size()== 0){joptionpane.showmessagedialog(これ、「この本は存在しません!」); } else {object [] [] data = getSelect(list); jtable = new jtable(data、title); 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();文字列著者=著者jtf.getText(); string publish = publishjtf.getText(); string publishdate = publishdatejtf.getText(); SimpleDateFormat sdf = new SimpledateFormat( "yyyy-mm-dd");日付regdate = null; {regdate = sdf.parse(publishdate); } catch(例外E2){e2.printstacktrace(); joptionpane.showmessagedialog(これ、「正しい日付形式を入力してください!」); } if(regdate == null)return; int publishtime = integer.parseint(printtimejtf.getText()); int unitprice = integer.parseint(unitpricejtf.getText()); //オブジェクトのbook = new Book(); book.setisbn(isbn); book.settypeid(id); book.setbookName(BookName); book.setauthor(著者); book.setpublish(publish); book.setpublishdate(regdate); book.setpublishtime(publishtime); book.setunitprice(unitprice); book.settypename(typename); int result = bookdao.update(book);リスト<book> list2 = new arrayList <book>(); list2.add(book); // system.out.println(list2); if(result == 1){joptionpane.showmessagedialog(this、 "Modified usglected!"); bookdao bk = new bookdao();リスト<book> list = bk.selectbook(); jtable = new jtable(getSelect(list)、title); Tianjia(); } else {joptionpane.showmessagedialog(this、 "modified failed!"); }} if(e.getSource()== resetjb){dispose(); new MainWindow(); } if(e.getSource()== exitjb){dispose(); new MainWindow(); }} private void tianjia(){jtable.getColumnModel()。getColumn(4).setPreferredWidth(175); jtable.setautoreizeMode(jtable.auto_resize_off); jscrollpane.setviewportview(jtable); } public static void main(string [] args){new BookSelectandModify(); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。