この記事の例は、参照のためにJava GUI学生帳管理の特定のコードを共有しています。特定のコンテンツは次のとおりです
-MySQLデータベーステーブルビルディング:
1.ブックテーブル
2.BOTブックローン記録フォーム
3.STD学生テーブル
4.DLログインユーザーテーブル
。
- コンテンツをリストします
1.Databd.java //プログラムポータルとログイン検証
Import java.awt。*; import java.awt.event。*;インポートjava.sql。*; import javax.swing.border.emptyborder; public class databd extends jframe explmess actionlistener {static framedemo a = null; string getUser = "";文字列getpd = "";文字列sql = "select * from dl where id = '";文字列sql1 = "password = '"をdlから[パスワード]を選択します。 jbutton btnnewbutton = new jbutton( "login"); jbutton btnnewbutton_1 = new jbutton( "Register"); jtextfield show = new jtextfield( "/tボタンを選択してください"、10);プライベートJPanel ContentSpane;プライベートjtextfieldユーザーフィールド。プライベートJPasswordfield Pwdfield;プライベートステートメントステートメント= null; // queryアカウントプライベートステートメントステートメント2 = null; //アカウントを登録プライベート静的DATABDフレーム。 // resultset rst2 = null; public static void main(string [] args){eventqueue.invokelater(new runnable(){public void run(){try {frame = new databd(); frame.setVisible(true);} catch(exceance e){e.printstacktrace();}}}); } public databd(){string driver = "com.mysql.jdbc.driver"; string url = "jdbc:mysql:// localhost:8088/library?useunicode = true&charatereCoding = utf-8&usessl = false";文字列ユーザー= "root";文字列パスワード= "11111"; try {class.forname(driver);接続conn = drivermanager.getConnection(url、user、password);接続conn2 = drivermanager.getConnection(url、user、password);ステートメント= conn.createStatement(); Statement2 = conn2.createStatement(resultset.type_scroll_sensitive、resultSet.concur_updatable); rst2 = statement2.executequery( "select * from dl"); show.setedable(false); setDefaultCloseoperation(jframe.exit_on_close); SetSize(250、200); setLocationRelativeto(null); contentSpane = new JPanel(); contentSpane.setborder(新しいemptyborder(5、5、5、5)); contentSpane.setLayout(null); add(contentspane); jlabel lblusername = new Jlabel( "account"); lblusername.setbounds(12、13、54、15); contentSpane.add(lblusername); jlabel lblpassword = new Jlabel( "パスワード"); lblpassword.setbounds(12、38、54、15); contentSpane.add(lblpassword); userfield = new jtextfield(); userfield.setbounds(76、10、144、21); contentSpane.add(userfield); userfield.setColumns(10); pwdfield = new JPassWordField(); pwdfield.setechochar( '*'); //パスワードエコー文字pwdfield.setbounds(76、35、144、21); ContentSpane.Add(PWDField); pwdfield.setColumns(10); add(show、borderlayout.south); btnnewbutton.addactionlistener(this); // btnnewbutton.setbounds(10、92、93、23); ContentSpane.Add(btnnewbutton); btnnewbutton_1.AddActionListener(this); // btnnewbutton_1.setbounds(127、92、93、23)を登録します。 contentSpane.Add(btnnewbutton_1); show.addactionlistener(this); } catch(classNotFoundException e){system.out.print( "drive not not ind"); } catch(Exception e){System.out.print( "Error oassed"); e.printstacktrace();}} public void actionperformed(actionEvent e){getUser = userfield.getText()。トリム()+"'"; string pd = string.valueof(pwdfield.getPassword()); {resultet rs = statement.executequery(sql+getuser); if(e.getSource()== btnnewbutton){// login if(rs.next()){if(rs.getString(2).equals(pd)){frame.setVisible(false); joptionpane.ShowMessageDialog(null、 "ログインに成功します"); uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); a = framedemo.a; //静的オブジェクトを生成} else show.settext( "/tパスワードエラー"); } else show.settext( "/tアカウントは存在しません"); } else if(e.getSource()== btnnewbutton_1){// register if(userfield.getText()。equals( "")){show.settext( "/t登録アカウントは空になりません");} 空の"); else {Register();}}}}}} catch(Exception c){c.printstacktrace();}} private void Register(){string name = userfield.getText()、passwd = string.valueof(pwdfield.getpassword(); try {rst2.movetoinsertrow(); rst2.updatestring(1、name); rst2.updatestring(2、passwd); rst2.insertrow(); rst2.movetocurrentrow(); show.settext( "/t登録に正常に"); } catch(例外e){show.settext( "/t登録失敗");}}}}2.framedemo //メインフレームワーク、イベントをAddInformationクラスに引き渡す
パッケージデータベース; javax.swing。*;インポートjavax.swing.border.titledborder; import java.awt。*; import java.awt.event。 static framedemo a = new framedemo(); jpanel panele = new jPanel()、panel1 = new jpanel()、panel2 = new jpanel()、panel3 = new jpanel()、panel4 = new jpanel()、panel5 = new jpanel(); string [] adds = {"操作を選択してください"、 "コレクションレコードの追加"、 "本の借入記録の追加"、 "add student record"}; string [] deletes = {"操作を選択してください"、 "コレクションレコードを削除してください"、 "削除本の借入記録"、 "delete sudent record"}; string [] change = {"操作を選択してください"、 "inventoryレコードを変更してください"、 "返す日付"}; string [] querys = {"操作を選択してください"、 "query book number"、 "query student onlowing record"、 "query student information"}; string [] shows = {"操作を選択してください"、 "すべてのコレクションレコードを表示してください"、 "借り入れレコードを表示します"、 "show sudent records"}; string [] record = new String [4]; jtextarea ta = new jtextarea( " - > system/n"、11,60を使用してください); jscrollpane Pane = new JScrollpane(TA);接続conn = null;ステートメントstmt = null;ステートメントstmt1 = null;ステートメントstmt2 = null;結果rst = null;結果rst1 = null;結果rst2 = null;静的ブールキー。プライベートJFrame Big = new JFrame( "操作を選択してください"); Private JComboBox Add = new JComboBox(ADDS); Private JComboBox delete = new JComboBox(deletes); Private JComboBox Change = new JComboBox(変更);プライベートJComboBox Query = new JComboBox(Querys); Private JComboBox Show = new JComboBox(shows); public framedemo(){super( "図書館管理システム"); try {class.forname( "com.mysql.jdbc.driver"); string url = "jdbc:mysql:// localhost:8088/library?useunicode = true&charatereCoding = utf-8&usessl = false";文字列ユーザー= "root";文字列パスワード= "111111"; conn = drivermanager.getConnection(url、user、password); Panel4.setLayout(新しいGridlayout(3,1,30,10)); panel4.setborder(new Titledborder( "Select Management:")); panel1.add(new Jlabel( "add record:")); panel1.add(add); panel1.add(new Jlabel( "Delete Record:")); panel1.add(delete); panel2.add(new Jlabel( "Record:"));パネル2.Add(変更); panel2.add(new Jlabel( "Query Record:")); Panel2.Add(Query); panel3.add(new Jlabel( "show); panel4.add(panel1); panel4.add(panel2); panel4.add(panel3); panedd(pane); paner.add(paner5、borderlayout.south); panel.add(panel4、borderlayout.center); add(and); stmt = conn.createstatement(resultset.type_scroll_sensitive、resultet.concur_updatable); stmt2 = condertatement.type_sensive、results.addactionlistener(this); SetSize(470,440); //データベースは、add.getSelectedIndex()== 1){ta.append( " - > addinformation a1 = new addinformation(" book title "、" inventory "、1); a1 = new addinformation(book number "、2); if.getSelectedIndex()== 1){> delete collection record/n ");番号 "、5);} else if(delete.getSelectedIndex()== 3){ta.append(" - > delete sudent record/n "); addInformation a1 = new addInformation("学生学生番号 "、6);} delete.setselectedem("操作を選択してください "); Record/n "); addInformation a1 = new addInformation(" book number "、" inventory number "、7);} else if(change.getSelectedIndex()== 2){ta.Append(" - > return/n "); addinformation a1 = new addinformation(" book number "、"操作を選択します "); if(query.getSelectedIndex()== 1){ta.append(" - > query by book title/n "); addinformation a1 = new addInformation(" book name "、9);} else if(query.getSelectedIndex()== 2){ta.Append("> n ");番号 "、10);} else if(query.getSelectedIndex()== 3){ta.Append(" - > Query Student Lonowing Records/n "); addInformation a1 = new addInformation("学生学生数 "、11);} els if(query.getSelectedIndex()= 4) AddInformation(学生番号」、Query.setselectioned(show.getSelectedIndex(){ta.append( " - > try {rst = stmt.executequery); ta.append("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Ta.Append( "+"/t "+num+"/n ");実行すると、カーソルは最後の行の最後の位置を指します。RST.FIRST(); ta.append("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ta.append("-->Show book borrowing record/n"); rst1 = stmt1.executequery( "select * from bs"); ta.append("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ utildate =(java.util.date)calender.getTime(); Ta.Append( "+bookid+"+"+x+"/t "+next); 30); java.util.gettime()カーソルは、最後の行の最後の位置、rst1.first()を指します。 ta.append("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ name = rst2.getString(2); Ta.Append( "/t"+id+"/t"+name+"/t"+"/n"); ta.append("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ show.setSelectedItem("Please select the action"); ta.setCareTposition()。 ta.append("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- stock=rst.getString(5); Ta.Append(+""+"+"+stock+n "); "+brand+" "+price+" "+stock+"/n ");} //実行後、カーソルは最後のラインrst.absolute(a)の最後の位置を指します。 ta.append("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------3.AddinFormation.java //イベントを処理します
パッケージデータベース; javax.swing。*;インポートjava.util.calendar;インポートjava.util.gregoriancalendar; Import java.awt.event。*; Import java.sql。 quxiao = new JButton( "Cancel"); jtextfield jtf1 = new jtextfield(10)、jtf2 = new jtextfield(10)、jtf3 = new jtextfield(10);接続conn = null; static {try {class.forname( "com.mysql.jdbc.driver"); } catch(Exception e){e.printstacktrace();}} int x = 0; public connection getConnection()throws sqlexception {if(conn == null){conn = drivermanager.getConnection( "jdbc:mysql:// localhost:8088/library?useunicode = true&characterencoding = utf-8&usessl = false"、 "root"); } return conn; } addInformation(string s1、string s2、string s3、int m){jpanel p = new jpanel(); P.SetLayout(new FlowLayout()); P.Add(New Jlabel(S1)); p.add(jtf1); P.Add(New Jlabel(S2)); P.Add(JTF2); P.Add(New Jlabel(S3)); P.Add(JTF3); P.Add(確かに); P.Add(Quxiao);追加(p); if(m == 8){joptionpane.showMessageAgeAg(null、 "bookの日付はxxxx-xx-xx形式!"、 "warning"、joptionpane.warning_message); } setSize(500、100); setVisible(true); setDefaultCloseOperation(jframe.dispose_on_close); setLocationRelativeto(null); sure.addactionlistener(this); quxiao.addactionlistener(this); x = m; } addinformation(string s1、string s2、int m){jpanel p = new jpanel(); P.SetLayout(new FlowLayout()); P.Add(New Jlabel(S1)); p.add(jtf1); P.Add(New Jlabel(S2)); P.Add(JTF2); P.Add(確かに); P.Add(Quxiao);追加(quxiao);追加(p); SetSize(500、100); setVisible(true); setDefaultCloseOperation(jframe.dispose_on_close); setLocationRelativeto(null); sure.addactionlistener(this); quxiao.addactionlistener(this); x = m; } addinformation(string s1、int m){jpanel p = new jpanel(); P.SetLayout(new FlowLayout()); P.Add(New Jlabel(S1)); p.add(jtf1); if(m == 4){joptionpane.showmessageialog(null、 "借入記録がある場合、本情報は削除できません!"、 "警告"、joptionpane.warning_message); } if(m == 6){joptionpane.showmessageialog(null、 "借り入れレコードがある場合、学生情報は削除できない!"、 "警告"、joptionpane.warning_message); } p.add(Sure); P.Add(Quxiao);追加(p); SetSize(500、80); setVisible(true); setDefaultCloseOperation(jframe.dispose_on_close); setLocationRelativeto(null); sure.addactionlistener(this); quxiao.addactionlistener(this); x = m; } public void ActionPerformed(actionEvent ae){if(ae.getSource()== sure){if(x == 1){//コレクションレコード文字列sql = "book値に挿入(?、、?)"; try(connection conn = getConnection(); preatedStatement stmt = conn.preparestatement(sql)){int id = integer.parseint(jtf1.getText());文字列名= jtf2.getText(); int num = integer.parseint(jtf3.getText()); stmt.setint(1、id); stmt.setString(2、name); stmt.setint(3、num); stmt.executeupdate(); framedemo.a.ta.append( " - >成功/n"); } catch(例外x){framedemo.a.ta.append( " - >挿入エラー/n"); x.printstacktrace();} addinformation.this.dispose(); } if(x == 2){//借入レコード文字列sql = "bookid、stdid、date)values(?、?、?)"に挿入を追加します ";文字列sql1 = "idを選択し、num from book where id =?";文字列sql2 = "id =?"からstdからidを選択します。 "; try {connection conn1 = getConnection(); preatedStatement stmt1 = conn1.preparestatement(sql1、resultset.type_scroll_sensitive、resultset.concur_updatable); stmt1.setint(1、integer.parseint(jtf1.getText())); resultet rst1 = stmt1.executequery(); //この本があるかどうか、在庫が0であるかどうかを確認します(rst1.next()&&(rst1.getint(2)> 0)){int n = rst1.getint(2); n- = 1; int m = rst1.getint(1); rst1.updateint(1、m); rst1.updateint(2、n); rst1.updaterow();接続conn2 = getConnection();準備stmt2 = conn2.preparestatement(sql2); stmt2.setint(1、integer.parseint(jtf2.getText())); resultet rst2 = stmt2.executequery(); //誰かのif(rst2.next()){connection conn = getConnection(); preatedStatement stmt = conn.preparestatement(sql); int bookid = integer.parseint(jtf1.getText()); int stdid = integer.parseint(jtf2.getText()); stmt.setint(1、bookid); stmt.setint(2、stdid); stmt.setdate(3、new Date(system.currenttimemillis())); stmt.executeupdate(); framedemo.a.ta.append( " - >成功/n"); } else {framedemo.a.ta.append( " - >生徒のレコードなし、no no no no no record/n");}} else {framedemo.a.ta.append( " - >この本はall/n") x.printstacktrace();} addinformation.this.dispose(); } if(x == 3){//学生レコード文字列sql = "std値に挿入(?、?)"; try(connection conn = getConnection(); preatedStatement stmt = conn.preparestatement(sql)){int id = integer.parseint(jtf1.getText());文字列名= jtf2.getText(); stmt.setint(1、id); stmt.setString(2、name); stmt.executeupdate(); framedemo.a.ta.append( " - >成功/n"); } catch(例外x){framedemo.a.ta.append( " - >挿入エラー/n"); x.printstacktrace();} addinformation.this.dispose(); } if(x == 4){// bookレコード文字列sql = "book where id =?"; string sql1 = "select * from bs bookid =?"; try {connection conn1 = getConnection();準備stmt1 = conn1.preparestatement(sql1); stmt1.setint(1、integer.parseint(jtf1.getText())); resultet rst1 = stmt1.executequery(); if(rst1.next()){//借入記録には本がありますjoptionpane.showmessageialog(null、「借入記録の本の借入記録があります!」、「警告」、joptionpane.warning_message); } else {//借入記録には本がありません、Connection Conn = getConnection(); preatedStatement stmt = conn.preparestatement(sql); int id = integer.parseint(jtf1.getText()); stmt.setint(1、id); int t = stmt.executeupdate(); if(t == 0){framedemo.a.ta.append( " - > delete error/n"); joptionpane.showmessagedialog(null、 "本情報は存在しません!」、「警告」、joptionpane.warning_message); } else framedemo.a.ta.append( " - > delete record resustfully/n"); }} catch(例外x){framedemo.a.ta.append( " - >削除failed/n"); x.printstacktrace();} addinformation.this.dispose(); } if(x == 5){//本を削除しますレコード文字列sql = "bookid =?and stdid =?";文字列sql1 = "idを選択し、num from book where id =?"; try {connection conn = getConnection(); preatedStatement stmt = conn.preparestatement(sql); int bookid = integer.parseint(jtf1.getText()); int stdid = integer.parseint(jtf2.getText()); stmt.setint(1、bookid); stmt.setint(2、stdid); int t = stmt.executeupdate(); if(t!= 0){connection conn1 = getConnection(); preatedStatement stmt1 = conn1.preparestatement(sql1、resultset.type_scroll_sensitive、resultset.concur_updatable); stmt1.setint(1、integer.parseint(jtf1.getText())); resultet rst1 = stmt1.executequery(); rst1.next(); int m = rst1.getint(1); int n = rst1.getint(2); n+= 1; rst1.updateint(1、m); rst1.updateint(2、n); rst1.updaterow(); framedemo.a.ta.append( " - >借入レコードを正常に削除/n"); } else {framedemo.a.ta.append( " - >削除エラー/n"); joptionpane.showmessageialog(null、「この借入記録は存在しません!」、「警告」、joptionpane.warning_message); }} catch(例外x){framedemo.a.ta.append( " - > delete error/n"); x.printstacktrace();} addinformation.this.dispose(); } if(x == 6){//学生レコード文字列sql = "delete from std where id =?";文字列sql1 = "select * from bs where stdid =?"; try {connection conn1 = getConnection();準備stmt1 = conn1.preparestatement(sql1); stmt1.setint(1、integer.parseint(jtf1.getText())); resultet rst1 = stmt1.executequery(); if(rst1.next()){//その人は借入記録に載っています、joptionpane.showmessageialog(null、 "人は借入記録にいます!"、 "warning"、joptionpane.warning_message) } else {connection conn = getConnection(); preatedStatement stmt = conn.preparestatement(sql); int stdid = integer.parseint(jtf1.getText()); stmt.setint(1、stdid); int t = stmt.executeupdate(); if(t == 0){framedemo.a.ta.append( " - > delete error/n"); joptionpane.showmessageialog(null、 "学生レコードは存在しません!"、 "warning"、joptionpane.warning_message); } else framedemo.a.ta.append( " - > delete record resustfully/n"); }} catch(例外x){framedemo.a.ta.append( " - > delete error/n"); x.printstacktrace();} addinformation.this.dispose(); } if(x == 7){// book inventory record string sql = "book set num =?where id =?"; try(connection conn = getConnection(); preatedStatement stmt = conn.preparestatement(sql)){int num = integer.parseint(jtf2.getText()); int bookid = integer.parseint(jtf1.getText()); stmt.setint(1、num); stmt.setint(2、bookid); int t = stmt.executeupdate(); if(t == 0){framedemo.a.ta.append( " - >修正エラー/n"); joptionpane.showmessageialog(null、 "本の記録は存在しません!"、 "warning"、joptionpane.warning_message); } else framedemo.a.ta.append( " - >修正レコードは成功/n"); } catch(例外x){framedemo.a.ta.append( " - >修正エラー/n"); x.printstacktrace();} addinformation.this.dispose(); } if(x == 8){//返される日付を変更するstring sql = "bs set date =?where bookid =?and stdid =?"; try(connection conn = getConnection(); preatedStatement stmt = conn.preparestatement(sql)){date date = date.valueof(jtf3.getText());カレンダーカレンダー= new GregorianCalendar(); calendar.settime(date); calendar.add(calendar.date、-30); java.util.date utildate =(java.util.date)calendar.getTime(); utildate =(java.util.date)calendar.getTime();日付newdate = new date(utildate.getTime()); int stdid = integer.parseint(jtf2.getText()); int bookid = integer.parseint(jtf1.getText()); stmt.setdate(1、newdate); stmt.setint(2、bookid); stmt.setint(3、stdid); int t = stmt.executeupdate(); if(t == 0){framedemo.a.ta.append( " - >修正エラー/n"); joptionpane.showmessageialog(null、 "借用記録は存在しません!"、 "warning"、joptionpane.warning_message); } else framedemo.a.ta.append( " - >修正レコードは成功/n"); } catch(例外x){framedemo.a.ta.append( " - >修正エラー/n"); x.printstacktrace();} addinformation.this.dispose(); } if(x == 9){// query string sql = "select * from book where where name =?";結果rst = null; try(connection conn = getConnection(); preatedStatement stmt = conn.preparestatement(sql)){string name = jtf1.getText(); stmt.setString(1、name); rst = stmt.executequery(); if(rst.next()){ frameDemo.a.ta.append("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- framedemo.a.ta.append( "/t"+rst.getint(1)+"/t"+rst.getString(2)+"/t"+rst.getint(3)+"/n"); frameDemo.a.ta.append("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- x){framedemo.a.ta.append( " - > query error/n"); x.printstacktrace.this.dispose()} {// query string sql = "select * stmt = conn.preparestatement(sql)){stmt.setint(1、jtf1.gettext())) frameDemo.a.ta.append("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- joptionpane.showmessageialog(null、「存在しません!」、「警告」、joptionpane.warning_message) (x == 11){// query string sql = "Select bookid、stdid =?"; rst = stmt.executequery(); frameDemo.a.ta.append("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ calender.settime(x); framedemo.ta.append( "/t"+rst.getint+"+rst.getint(2)+"+newdate+"/n") (java.util.date)calendar.getTime(); framedemo.a.ta.append( "/t"+rst.getint(1)+"/t"+rst.getint(2)+"/t"+"/t"+newdate+"/n"); frameDemo.a.ta.append("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- sql = "sectd from id =?"; frameDemo.a.ta.append("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- framedemo.a.ta.append( "/t"+rst.getint(1)+"/t"+rst.getString(2)+"/n"); frameDemo.a.ta.append("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- x){framedemo.a.ta.append( " - > query error/n"); x.printstacktrace();} addinformation.dispose()}} }}}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。