この記事の目的は、ライブラリ管理システムを介してデータベースプログラミングテクノロジーをマスターし、データベースに正しく接続し、データベースの情報をクエリ、挿入、削除、変更できることです。
コンテンツ:タイトル、著者、出版社、出版日、帳簿番号、価格フィールドなど、データベースに書誌情報テーブルを作成します。参考文献管理のためのGUIインターフェイスを設計します。このインターフェイスには4つのタブがあります。つまり、クエリ、挿入、削除、および変更されています。 [クエリ]タブをクリックすると、表示されるインターフェイスには、本のタイトル、著者、出版社、帳簿番号、ボタン、読み取り専用のテキストエリアの4つのテキストボックスが含まれています。テキストボックスのコンテンツは空にすることができます。対応するクエリ情報を入力した後(たとえば、本のタイトルに従ってタイトルを入力できます)、インターフェイスの「クエリ」ボタンをクリックして、インターフェイスの下のテキスト領域の基準を満たす参考文献の詳細情報を表示します。 [挿入]タブをクリックすると、表示されるインターフェイスには、本のタイトル、著者、出版社、出版日、本番号、価格テキストボックス、ボタンが含まれています。テキストボックスに情報を入力したら、[挿入]ボタンをクリックすると、書誌情報がデータベーステーブルに挿入されます。 [削除]タブをクリックします。タイトル用のテキストボックスと、表示されるインターフェイスにボタンがあります。タイトルを入力したら、[削除]ボタンをクリックします。書誌情報は、データベーステーブルから削除されます。 [変更]タブをクリックすると、表示されるインターフェイスには、本のタイトル、著者、出版社、出版日、本番号、価格テキストボックス、ボタンが含まれます。入力された本のタイトルが存在する必要があります。そうしないと、メッセージボックスがポップアップ表示され、エラーメッセージが表示されます。情報を入力したら、[変更]ボタンをクリックすると、データベーステーブルの対応する参考文献情報が新しい値に変更されます。
ソースコード:
bookinfo.java
*プロジェクト名:Book Management System *バージョン:1.0 *作成者:Zhang Junqiang *作成時間:2016/5/26 * */パッケージLibrarySystem; java.awt。*; javax.swing。*; java.awt.event。*; Java.sql。*をインポートします。 @suppresswarnings( "serial")public class bookinfo extends jframe explention actionlistener {//主人公のプライベートJlabel inputlabelの対照。プライベートJTextField inputText;プライベートjbutton search -but;プライベートJTable BookTable;プライベートjscrollpane bookscroll;プライベートjbutton add -add;プライベートjbutton modify -but;プライベートjbutton delete -but;プライベートJButton Refreshbut; Private BookTableModel BookTableModel; public static void main(string [] args)throws sqlexception {// todo auto-fenated method stub bookinfo bookinfo = new bookinfo(); bookinfo.setdefaultcloseoperation(jframe.exit_on_close); bookinfo.setbounds(350、150、600、400); bookinfo.setVisible(true); // bookinfo.importsql(); // export data bookinfo.setminwindowlayout(); // set data} public bookinfo()throws sqlexception {//メインインターフェイスinputlabel = new jlabel( "を入力してください:"); inputText = new JTextField(10); search but = new jbutton( "query"); booktablemodel = new booktablemodel(); booktable = new jtable(booktablemodel); bookscroll = new jscrollpane(booktable); addbut = new jbutton( "add"); ModifyCut = new JButton( "Modify"); delete -but = new JButton( "Delete"); refressbut = new jbutton( "refresh"); search.AddActionListener(this); addbut.addactionlistener(this); refressed.addactionlistener(this); Modify-chidify.addactionlistener(this); delete.addactionlistener(this); } void setMinWindowLayout(){//メインインターフェイスレイアウトコンテナcon1 = new Container(); con1.setLayout(new FlowLayout()); con1.add(inputlabel); con1.add(inputText); con1.add(search -but); con1.add(refreshbut);コンテナcon2 = new container(); con2.setLayout(new FlowLayout()); con2.add(addt); con2.add(modify -cut); con2.add(delete -but); this.setlayout(new borderlayout()); this.add(con1、borderlayout.north); this.add(bookscroll、borderlayout.center); this.add(con2、borderlayout.south); this.validate(); } @Override public void ActionPerformed(actionEvent e){// todo auto-formatedメソッドスタブif(e.getSource()== search-but){if(!this.inputtext.getText( "")){string bookname = this.inputtext.getText();文字列sql = "select * frombook_info where book_name = '"+bookname+"'"; try {booktablemodel = new booktablemodel(sql); booktable.setmodel(booktablemodel); } catch(sqlexception e1){// todo auto-feenated catch block e1.printstacktrace(); }} else {joptionpane.showmessageialog(this、 "input nable empty"、 "prompt"、joptionpane.plain_message); }} else if(e.getSource()== add -but){@suppresswarnings( "unused")addbookdialog addwin = new addbookdialog(this、 "add book"、true); this.refreshtable(); } else if(e.getSource()== refreshbut){this.refreshtable(); } else if(e.getSource()== delete -but){int rownum = booktable.getSelectedRow(); if(rownum <0 || rownum> booktable.getRowCount()){joptionpane.showmessageialog(this、 "unselected"、 "tip"、joptionpane.plain_message); } else {//system.out.print(bookname); int n = joptionpane.showconfirmdialog(null、 "削除の確認?"、 "削除ボックスの確認"、joptionpane.yes_no_option); if(n == joptionpane.yes_option){string booknum =(string)booktable.getValueat(rownum、0); string sql = "book_infoからdelete where book_num = '"+booknum+"'"; booktablemodel.deletebook(SQL); this.refreshtable(); joptionpane.showmessageialog(this、 "deletesuccess"、 "tip"、joptionpane.plain_message); } else if(n == joptionpane.no_option){return; }}} else if(e.getSource()== Modify -cify){booktable.setModel(booktablemodel); int rownum = booktable.getSelectedRow(); if(rownum <0 || rownum> booktable.getRowCount()){joptionpane.showmessageialog(this、 "uncheck"、 "prompt"、joptionpane.plain_message); } else {@suppresswarnings( "unused")modifybook modifywin = new ModifyBook(this、 "Modify Information"、true、booktablemodel、rownum); this.refreshtable(); }}} public void refreshtable(){booktablemodel searchbook; try {SeartBook = new BookTableModel( "select * frombook_info"); booktable.setModel(検索ブック); BookTableModel = SearchBook; } catch(sqlexception e1){// todo auto-feenated catch block e1.printstacktrace(); }}} booktablemodel.java
パッケージライブラリシステム。 Java.sql。*をインポートします。 Java.util。*をインポートします。 /**ブックテーブルモデル**/ Import javax.swing.table。*; @suppresswarnings( "serial")public class booktablemodel extends abstracttablemodel {//テーブルプライベートベクトル<vector <string >> rowdata; private vector <string> colname; //データベースPrivate PredStatement Stmt;プライベート結果セットの結果。 public booktablemodel(String SQL)はsqlexception {this.initdata(sql); } public booktablemodel()throws sqlexception {this.initdata( "select * from book_info"); } public void initdata(string sql)throws sqlexception {setrowdata(new Vector <Vector <string >>()); setColname(new Vector <String>()); getColname()。add( "Book No."); getColname()。追加( "Book Title"); getColname()。追加( "Book Title"); getColname()。add( "Author"); getColname()。add( "publisher"); getColname()。add( "Publishing Time"); getColname()。add( "price"); / * *データベースインポート * */ try {class.forname( "com.mysql.jdbc.driver"); } catch(classNotFoundException e){// todo auto-enerated catch block e.printstacktrace(); } string url = "jdbc:mysql:// localhost:3306/device";文字列ユーザー= "root";文字列パスワード= "zjq1314520";接続con = drivermanager.getConnection(url、user、password); stmt = con.preparestatement(sql); result = stmt.executequery(); importsql(); } void importSql()throws sqlexception {// dodo auto-enerated method stub @suppresswarnings( "unused")boolean signnull = true; while(result.next()){vector <string> item = new Vector <String>(); for(int i = 1; i <7; i ++){item.add(result.getString(i)); } getRowData()。add(item); sightnull = false; } result.close(); } @Override public int getColumnCount(){//列の数を取得// todo auto-enerated method stub return this.colname.size(); } @Override public int getRowCount(){//行の数を取得// todo auto-feenated method stub return this.rowdata.size(); } @Override public int getRowCount(){//行の数を取得// todo auto-feenated method stub return this.rowdata.size(); } @Override public Object getValueat(int row、int col){//行と列のデータを取得// todo auto-fenatedメソッドスタブリターン(this.rowdata.get(row))。get(col); } @Override public String getColumnname(int column){// todo auto-fenated method stub return this.colname.get(列); } public vector <vector <string >> getRowData(){return rowdata; } public void setrowdata(vector <vector <string >> rowdata){this.rowdata = rowdata; } public vector <string> getColname(){return colname; } public void setColname(vector <string> colname){this.colname = colname; } public void addbook(string sql){try {stmt.executeupdate(sql); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); } // initdata( "select * frombook_info"); } public void deletebook(string sql){try {stmt.executeupdate(sql); } catch(sqlexception e1){// todo auto-feenated catch block e1.printstacktrace(); }}} addbookdialog.java
パッケージライブラリシステム。 java.awt。*; java.awt.event。*; java.sql.sqlexceptionをインポートします。 javax.swing。*; @suppresswarnings( "serial")パブリッククラスAddbookdialog拡張jdialog実装actionlistener {private jlabel booknumlabel;プライベートJlabel booknameLabel;プライベートJlabel BookWriterLabel;プライベートJlabel Bookpublishlabel;プライベートJlabel bookpricelabel;プライベートJlabel booktimelabel;プライベートjtextfield booknumtext;プライベートjtextfield booknametext; Private JTextfield BookWritertext; Private JTextfield BookPublishText;プライベートJTextfield bookpricetext;プライベートjtextfield booktimeText;プライベートjbutton submitut;プライベートjbutton cancelcut; public addbookdialog(フレーム所有者、文字列タイトル、ブールモデル){//親ウィンドウ、ウィンドウ名、モードウィンドウスーパー(所有者、タイトル、モデル)です。 booknumlabel = new Jlabel( "book number:"); booknameLabel = new Jlabel( "Book Title:"); bookwriterlabel = new Jlabel( "著者:"); bookpublishlabel = new Jlabel( "Publisher:"); bookpricelabel = new Jlabel( "price:"); booktimelabel = new Jlabel( "Publishing Time:"); booknumtext = new jtextfield(10); booknametext = new JTextField(10); bookwritertext = new jtextfield(10); bookpublishtext = new JTextfield(10); bookpricetext = new JTextfield(10); booktimeText = new JTextField(9); submit but = new JButton( "Cancel"); cancelChut = new JButton( "Cancel"); submit.AddActionListener(this); cancelced.addactionlistener(this); this.setbounds(350,150,400,260); this.SetSizable(false); this.setlayout(new borderlayout()); initlayout(); } public void initlayout(){container [] con1 = new container [6]; for(int i = 0; i <6; i ++)con1 [i] = new container(); con1 [0] .setlayout(new flowlayout()); con1 [0] .add(booknumlabel); con1 [0] .add(booknumtext); con1 [1] .setlayout(new flowlayout()); con1 [1] .add(booknamelabel); con1 [1] .add(booknametext); con1 [2] .setlayout(new flowlayout()); con1 [2] .add(bookwriterlabel); con1 [2] .add(bookwritertext); con1 [3] .setlayout(new flowlayout()); con1 [3] .add(bookpublishlabel); con1 [3] .add(bookpublishtext); con1 [4] .setlayout(new flowlayout()); con1 [4] .add(bookpricelabel); con1 [4] .add(bookpricetext); con1 [5] .setlayout(new flowlayout()); con1 [5] .add(booktimelabel); con1 [5] .add(booktimetext);コンテナcon2 = new container(); con2.setlayout(new borderlayout()); con2.add(con1 [0]、borderlayout.north); con2.add(con1 [1]、borderlayout.center); con2.add(con1 [2]、borderlayout.south);コンテナcon3 = new container(); con3.setlayout(new borderlayout()); con3.add(con1 [3]、borderlayout.north); con3.add(con1 [4]、borderlayout.center); con3.add(con1 [5]、borderlayout.south);コンテナcon4 = new container(); con4.setLayout(new FlowLayout()); con4.add(submitut); con4.add(cancelcut);コンテナcon5 = new container(); con5.setlayout(new borderlayout()); con5.add(con2、borderlayout.north); con5.add(con3、borderlayout.center); con5.add(con4、borderlayout.south); this.add(con5、borderlayout.center); this.validate(); this.setVisible(true); } @Override public void ActionPerformed(actionEvent e){// todo auto-formated methood stub if(e.getsource()== submit){if(booknumtext.getText()。equals( "")|| booknametext.getText() bookpricetext.getText()。equals( "")||。 joptionpane.showmessageialog(これ、「入力は空にすることはできません」、「プロンプト」、joptionpane.plain_message); } else {//system.out.println("input suctune ");文字列sql = "inserting" + "book_info(book_num、book_name、book_writer、publish_house、book_price、publish_time)" + "values( '"+booknumtext.getText()+"' '、'"+booknametext.getText()+"'、'"+bookwritertext.getText()+"'、'"+bookpublishtext.getText()+"'、'"+bookpriceText.getText()+" booktablemodel book.addbook(sql); this.setVisible(false); ModifyBook.java
パッケージライブラリシステム。 java.awt。*; java.awt.event。*; java.sql.sqlexceptionをインポートします。 javax.swing。*; @suppresswarnings( "serial")public class modifybook extends jdialog emplments actionlistener {private jlabel booknumlabel;プライベートJlabel booknameLabel;プライベートJlabel BookWriterLabel;プライベートJlabel Bookpublishlabel;プライベートJlabel bookpricelabel;プライベートJlabel booktimelabel;プライベートjtextfield booknumtext;プライベートjtextfield booknametext; Private JTextfield BookWritertext; Private JTextfield BookPublishText;プライベートJTextfield bookpricetext;プライベートjtextfield booktimeText;プライベートjbutton submitut;プライベートjbutton cancelcut;プライベートブックテーブルモデルブックモデル。 Private int rownum; public ModifyBook(フレーム所有者、文字列タイトル、ブール型タイプ、booktablemodelモデル、int row){super(所有者、タイトル、タイプ); bookmodel = model; rownum = row; booknumlabel = new Jlabel( "book number:"); booknameLabel = new Jlabel( "Book Title:"); bookwriterlabel = new Jlabel( "著者:"); bookpublishlabel = new Jlabel( "Publisher:"); bookpricelabel = new Jlabel( "price:"); booktimelabel = new Jlabel( "Publishing Time:"); booknumtext = new jtextfield(10); booknametext = new JTextField(10); bookwritertext = new jtextfield(10); bookpublishtext = new JTextfield(10); bookpricetext = new JTextfield(10); booktimeText = new JTextField(9); submit but = new JButton( "Confism Modification"); cancelChut = new JButton( "Cancel"); submit.AddActionListener(this); cancelced.addactionlistener(this); this.setbounds(350,150,400,260); this.SetSizable(false); this.setlayout(new borderlayout()); this.setValue(); this.initlayout(); } public void initlayout(){container [] con1 = new container [6]; for(int i = 0; i <6; i ++)con1 [i] = new container(); con1 [0] .setlayout(new flowlayout()); con1 [0] .add(booknumlabel); con1 [0] .add(booknumtext); con1 [1] .setlayout(new flowlayout()); con1 [1] .add(booknamelabel); con1 [1] .add(booknametext); con1 [2] .setlayout(new flowlayout()); con1 [2] .add(bookwriterlabel); con1 [2] .add(bookwritertext); con1 [3] .setlayout(new flowlayout()); con1 [3] .add(bookpublishlabel); con1 [3] .add(bookpublishtext); con1 [4] .setlayout(new flowlayout()); con1 [4] .add(bookpricelabel); con1 [4] .add(bookpricetext); con1 [5] .setlayout(new flowlayout()); con1 [5] .add(booktimelabel); con1 [5] .add(booktimetext);コンテナcon2 = new container(); con2.setlayout(new borderlayout()); con2.add(con1 [0]、borderlayout.north); con2.add(con1 [1]、borderlayout.center); con2.add(con1 [2]、borderlayout.south);コンテナcon3 = new container(); con3.setlayout(new borderlayout()); con3.add(con1 [3]、borderlayout.north); con3.add(con1 [4]、borderlayout.center); con3.add(con1 [5]、borderlayout.south);コンテナcon4 = new container(); con4.setLayout(new FlowLayout()); con4.add(submitut); con4.add(cancelcut);コンテナcon5 = new container(); con5.setlayout(new borderlayout()); con5.add(con2、borderlayout.north); con5.add(con3、borderlayout.center); con5.add(con4、borderlayout.south); this.add(con5、borderlayout.center); this.validate(); this.setVisible(true); } public void setValue(){this.booknumtext.settext((string)bookmodel.getValueat(rownum、0)); this.booknumtext.setedable(false); this.booknametext.settext((string)bookmodel.getValueat(rownum、1)); this.bookwritext.settext((string)bookmodel.getValueat(rownum、2)); this.bookpublishtext.settext((string)bookmodel.getValueat(rownum、3)); this.booktimetext.settext((string)bookmodel.getValueat(rownum、4)); this.bookpricetext.settext((string)bookmodel.getValueat(rownum、5)); this.validate(); } @Override public void ActionPerformed(ActionEvent e){// System.out.println(bookpricetext.getText()); // todo auto-generatedメソッドスタブif(e.getSource()== submit){if(booknumtext.getText()。equals( "")|| booknametext.getText( "")|| bookwritertext.getText() bookpublishtext.getText( "")||変更? "、"修正]ボックスの確認 "、joptionpane.yes_no_option); if(n == joptionpane.yes_option){string sql =" update book_info set book_name = '" '"+bookwritext.getText()+"'、publish_house = '"+bookpublishtext.getText()+"'、book_price = '"+bookpriceText.getText()+"'、publish_time = '"+booktimetext.getText()+"' book_num = '"+booknumtext+" booktablemodel book = new booktablemodel(); } joptionpane.showmessagedialog(This、 "Modification scuced"、 "Prompt"、joptionpane.plain_message); this.setVisible(false); } else if(n == joptionpane.no_option){return; }}} if(e.getSource()== cancelcut){this.setVisible(false); }}}}プログラムの実行結果:
メインインターフェイス:
クエリインターフェイス:
本のインターフェースを追加します:
インターフェイスを変更します。
操作の削除:
データベースインターフェイス:
管理システムの詳細については、「管理システムの特別トピック」をクリックして学習してください
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。