الغرض من هذه المقالة هو إتقان تقنية برمجة قاعدة البيانات من خلال نظام إدارة المكتبة ، وتكون قادرة على الاتصال بشكل صحيح بقاعدة البيانات ، وتكون قادرة على الاستعلام عن المعلومات وإدراجها وحذفها وتعديلها في قاعدة البيانات.
المحتوى: إنشاء جدول معلومات ببليوغرافي في قاعدة البيانات ، بما في ذلك العنوان والمؤلف وبيت النشر وتاريخ النشر ورقم الكتاب وحقول الأسعار. تصميم واجهة واجهة المستخدم الرسومية لإدارة المراجع. هناك أربع علامات تبويب على هذه الواجهة ، وهي الاستعلام وإدراج وحذف وتعديل. انقر فوق علامة تبويب الاستعلام ، وتتضمن الواجهة التي تظهر أربعة مربعات نصية: عنوان الكتاب ، المؤلف ، الناشر ، رقم الكتاب ، زر ومنطقة نصية للقراءة فقط. يمكن أن يكون محتوى مربع النص فارغًا. بعد إدخال معلومات الاستعلام المقابلة (على سبيل المثال ، يمكنك إدخال العنوان فقط وفقًا لعنوان الكتاب) ، انقر فوق الزر "الاستعلام" على الواجهة لعرض المعلومات التفصيلية للببليوغرافيا التي تلبي المعايير في منطقة النص أسفل الواجهة. انقر فوق علامة التبويب إدراج ، والواجهة التي تظهر تحتوي على عنوان الكتاب ، ومؤلف ، وبيت النشر ، وتاريخ النشر ، ورقم الكتاب ، ومربع نص السعر ، وزر. بعد إدخال المعلومات في مربع النص ، انقر فوق الزر "إدراج" ويتم إدراج معلومات الببليوغرافية في جدول قاعدة البيانات. انقر فوق علامة التبويب حذف. يوجد مربع نص للعنوان وزر على الواجهة. بعد إدخال العنوان ، انقر فوق الزر "حذف". يتم حذف المعلومات الببليوغرافية من جدول قاعدة البيانات. انقر فوق علامة التبويب Modify ، والواجهة التي تظهر تتضمن عنوان الكتاب ، ومؤلف ، وبيت النشر ، وتاريخ النشر ، ورقم الكتاب ، ومربع نص السعر ، وزر. يجب أن يكون عنوان الكتاب الذي تم إدخاله موجودًا ، وإلا فإن مربع الرسائل سوف يظهر ويعرض رسالة خطأ. بعد إدخال المعلومات ، انقر فوق الزر "تعديل" ، ويتم تعديل المعلومات الببليوغرافية المقابلة في جدول قاعدة البيانات إلى القيمة الجديدة.
رمز المصدر:
bookinfo.java
* اسم المشروع: نظام إدارة الكتب * الإصدار: 1.0 * المبدع: Zhang Junqiang * وقت الإنشاء: 2016/5/26 * */Package LibrarySystem ؛ استيراد java.awt.*؛ استيراد javax.swing.*؛ استيراد java.awt.event.*؛ استيراد java.sql.*؛ suppressWarnings ("Serial") Public Class BookInfo يمتد JFrame الأدوات ActionListener {// Control on the Progonist Face Jlabel inputlabel ؛ jtextfield private ؛ خاص Jbutton Searchbut ؛ خاص Jtable Booktable ؛ Private Jscrollpane bookscroll ؛ خاص jbutton addbut ؛ الخاص jbutton modifybut ؛ خاص Jbutton DeleteBut ؛ خاص Jbutton RefreshBut ؛ BookTableModel الخاص booktableModel ؛ Public Static Void Main (String [] args) يلقي sqlexception {// todo method method method tuto colboinfo bookinfo = new BookInfo () ؛ BookInfo.setDefaultCloseOperation (jframe.exit_on_close) ؛ BookInfo.setBounds (350 ، 150 ، 600 ، 400) ؛ BookInfo.setVisible (صحيح) ؛ // bookinfo.importsql () ؛ // تصدير البيانات bookinfo.setMinWindowLayout () ؛ // set data} public bookinfo () يلقي sqlexception {// إنشاء عنصر تحكم في الواجهة الرئيسية inputlabel = new jlabel ("يرجى إدخال عنوان الكتاب:") ؛ inputText = New JTextField (10) ؛ SearchBut = New JButton ("Query") ؛ booktableModel = new booktableModel () ؛ booktable = new jtable (booktableModel) ؛ bookscroll = new jscrollpane (booktable) ؛ addBut = New JButton ("Add") ؛ ModifyBut = New JButton ("Modify") ؛ DELETEBUT = New JButton ("delete") ؛ RefreshBut = New JButton ("Refresh") ؛ SearchBut.AddActionListener (this) ؛ addBut.addActionListener (هذا) ؛ refreshbut.addActionListener (this) ؛ modifyBut.addActionListener (this) ؛ DELETEBUT.AddActionListener (this) ؛ } void setMinWindowLayout () {// Main Interface Layout Container Con1 = New Container () ؛ con1.setLayout (New FlowLayout ()) ؛ con1.Add (inputLabel) ؛ con1.Add (inputText) ؛ con1.Add (SearchBut) ؛ con1.Add (RefreshBut) ؛ حاوية con2 = حاوية جديدة () ؛ con2.setLayout (New FlowLayout ()) ؛ con2.add (addbut) ؛ con2.add (modifyBut) ؛ con2.add (deleteBut) ؛ 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 method method method method if ( String sql = "SELECT * from Book_info حيث book_name = '"+bookName+"' '" ؛ Try {booktableModel = new BookTableModel (SQL) ؛ booktable.setModel (booktableModel) ؛ } catch (sqlexception e1) {// todo acto catch block e1.printstacktrace () ؛ }} آخر {joptionpane.showmessagedialog (هذا ، "لا يمكن أن يكون الإدخال فارغًا" ، "موجه" ، joptionpane.plain_message) ؛ }} else if ( this.refreshtable () ؛ } آخر if ( } آخر إذا ( if (rownum <0 || rownum> booktable.getRowCount ()) {joptionpane.showmessagedialog (هذا ، "غير محدد" ، "tip" ، joptionpane.plain_message) ؛ } آخر {//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 = "delete من book_info حيث book_num = '"+bookNum+"' '" ؛ booktableModel.DeleteBook (SQL) ؛ this.refreshtable () ؛ joptionpane.showmessagedialog (هذا ، "deletesuccess" ، "tip" ، joptionpane.plain_message) ؛ } آخر إذا (n == joptionpane.no_option) {return ؛ }}} آخر if ( int rownum = booktable.getSelectedRow () ؛ if (rownum <0 || rownum> booktable.getRowCount ()) {joptionpane.showmessagedialog (this ، "uncheck" ، "form" ، joptionpane.plain_message) ؛ } آخر {suppressWarnings ("unused") modifybook modifywin = new ModifyBook (هذا ، "تعديل المعلومات" ، true ، booktablemodel ، rownum) ؛ this.refreshtable () ؛ }}} public void refreshtable () {booktableModel searchbook ؛ حاول {searchbook = new booktableModel ("Select * from Book_info") ؛ booktable.setModel (SearchBook) ؛ booktableModel = searchbook ؛ } catch (sqlexception e1) {// todo acto catch block e1.printstacktrace () ؛ }}} booktablemodel.java
حزمة مكتبة. استيراد java.sql.*؛ استيراد java.util.*؛ /** نموذج جدول الكتاب**/ استيراد javax.swing.table.* ؛ suppressWarnings ("Serial") من الفئة العامة BookTablEmodel يمتد AbstractTableModel {// عناصر الجدول الخاص المتجه الخاص <string >> Rowdata ؛ متجه خاص <string> colname ؛ // قاعدة البيانات الخاصة الإعداد STMT ؛ نتيجة نتائج النتائج الخاصة ؛ Public BooktableModel (String SQL) يلقي sqlexception {this.initdata (sql) ؛ } Public BooktableModel () يلقي sqlexception {this.initdata ("Select * from Book_info") ؛ } public void initData (String SQL) يلقي sqlexception {setRowData (ناقل جديد <string >> ()) ؛ setColName (ناقل جديد <string> ()) ؛ getColname (). add ("Book No.") ؛ getColname (). add ("عنوان الكتاب") ؛ getColname (). add ("عنوان الكتاب") ؛ getColname (). add ("المؤلف") ؛ getColname (). add ("Publisher") ؛ getColname (). ADD ("وقت النشر") ؛ getColName (). إضافة ("السعر") ؛ / * * database import * */ try {class.forname ("com.mysql.jdbc.driver") ؛ } catch (classnotfoundException e) {// todo catch catch e.printstacktrace () ؛ } url url = "jdbc: mysql: // localhost: 3306/device" ؛ string user = "root" ؛ سلسلة كلمة المرور = "ZJQ1314520" ؛ Connection con = drivermanager.getConnection (url ، المستخدم ، كلمة المرور) ؛ stmt = con.preparestatement (SQL) ؛ النتيجة = stmt.executequery () ؛ eSpareSQL () ؛ } void ishorsql () يلقي sqlexception {// todo method method method tuto cubsuppressWarnings ("unused") boolean signnull = true ؛ بينما (result.next ()) {vector <string> item = new Vector <String> () ؛ لـ (int i = 1 ؛ i <7 ؛ i ++) {item.add (result.getString (i)) ؛ } getRowData (). add (item) ؛ signnull = false ؛ } result.close () ؛ } Override public int getColumnCount () {// احصل على عدد الأعمدة // todo method method tuto return this.colname.size () ؛ } Override public int getRowCount () {// احصل على عدد الصفوف // todo method method collated this.rowdata.size () ؛ } Override public int getRowCount () {// احصل على عدد الصفوف // todo method method collated this.rowdata.size () ؛ } Override Public Object GetValueat (int row ، int col) {// احصل على بيانات صف وعمود // todo method method tuto return (this.rowdata.get (row)). get (col) ؛ } Override public string getColumnName (عمود int) {// todo method method method tuto return this.colname.get (column) ؛ } المتجه العام <vector <string >> getRowData () {return rowdata ؛ } public void setRowData (متجه <vector <string >> rowdata) {this.rowdata = rowdata ؛ } المتجه العام <string> getColName () {return colname ؛ } public void setColName (متجه <string> colname) {this.colname = colname ؛ } public void addbook (String sql) {try {stmt.executeupdate (sql) ؛ } catch (sqlexception e) {// todo catch catch e.printstacktrace () ؛ } // initData ("حدد * من book_info") ؛ } public void deletebook (String sql) {try {stmt.executeupdate (sql) ؛ } catch (sqlexception e1) {// todo acto catch block e1.printstacktrace () ؛ }}} padbookdialog.java
حزمة مكتبة. استيراد java.awt.*؛ استيراد java.awt.event.*؛ استيراد java.sql.sqlexception ؛ استيراد javax.swing.*؛ suppressWarnings ("Serial") الطبقة العامة poxpbookDialog يمتد jdialog الأدوات ActionListener {private Jlabel BookNumlabel ؛ خاص Jlabel Booknamelabel ؛ كاتب الكتب Jlabel الخاص ؛ خاص Jlabel BookPublishLabel ؛ Private Jlabel bookpricelabel ؛ خاص Jlabel Booktimelabel ؛ private Jtextfield BookNumtext ؛ Private Jtextfield BookeNmetext ؛ Private Jtextfield WorkwriterText ؛ خاص JTextField BookPublishText ؛ Private Jtextfield bookpricetext ؛ Private Jtextfield Booktimetext ؛ Private Jbutton Submitbut ؛ خاص jbutton cancelbut ؛ Public BookBookDialog (مالك الإطار ، عنوان السلسلة ، نموذج منطقي) {// النافذة الأصل ، اسم النافذة ، هل هي نافذة وضع Super (المالك ، العنوان ، الطراز) ؛ BookNumlabel = New Jlabel ("رقم الكتاب:") ؛ BookNameLabel = New Jlabel ("Book Title:") ؛ كاتب الكتب = New Jlabel ("المؤلف:") ؛ BookPublishLabel = New Jlabel ("Publisher:") ؛ bookpricelabel = new Jlabel ("Price:") ؛ BookTimElabel = New Jlabel ("Time Publishing:") ؛ BookNumText = New JTextField (10) ؛ BookEnmetext = New JTextfield (10) ؛ كاتب الكتب = New JTextfield (10) ؛ BookPublishText = New JTextField (10) ؛ bookpricetext = New JTextfield (10) ؛ booktimetext = New JTextfield (9) ؛ SubmitBut = New JButton ("Cancel") ؛ CancelBut = New JButton ("إلغاء") ؛ submitBut.addActionListener (this) ؛ cancelbut.addActionListener (this) ؛ this.setBounds (350،150،400،260) ؛ this.setResible (false) ؛ this.setLayout (New BorderLayout ()) ؛ initlayout () ؛ } public void initlayout () {Container [] con1 = حاوية جديدة [6] ؛ لـ (int i = 0 ؛ i <6 ؛ i ++) con1 [i] = حاوية جديدة () ؛ 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 (كاتب الكتب) ؛ con1 [2] .add (كاتب الكتب) ؛ 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 = حاوية جديدة () ؛ con2.setLayout (New BorderLayout ()) ؛ con2.add (con1 [0] ، borderlayout.north) ؛ con2.add (con1 [1] ، borderlayout.center) ؛ con2.add (con1 [2] ، borderlayout.south) ؛ حاوية con3 = حاوية جديدة () ؛ con3.setLayout (New BorderLayout ()) ؛ con3.add (con1 [3] ، borderlayout.north) ؛ con3.add (con1 [4] ، borderlayout.center) ؛ con3.add (con1 [5] ، borderlayout.south) ؛ حاوية con4 = حاوية جديدة () ؛ Con4.SetLayout (New FlowLayout ()) ؛ con4.add (submitbut) ؛ Con4.add (CancelBut) ؛ حاوية con5 = حاوية جديدة () ؛ 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 (صحيح) ؛ } Override public void actionperformed (ActionEvent e) {// todo method method method method if ( bookpricetext.getText (). يساوي ("") ||. joptionpane.showmessagedialog (هذا ، "لا يمكن أن يكون الإدخال فارغًا" ، "موجه" ، joptionpane.plain_message) ؛ } آخر {//system.out.println("input ناجحًا ") ؛ String sql = "insert in" + "book_info (book_num ، book_name ، book_writer ، publish_house ، book_price ، publish_time)" + "القيم (" "+booknumtext.getText ()+" "،"+booknametext.getText ()+"،"+"+probritertext.getText ()+" ، ""+bookpublishtext.gettext ()+"" ، ""+bookpriceText.getText ()+"+" BookTableModel = booktablemode () } if ( modifybook.java
حزمة مكتبة. استيراد java.awt.*؛ استيراد java.awt.event.*؛ استيراد java.sql.sqlexception ؛ استيراد javax.swing.*؛ suppressWarnings ("Serial") الطبقة العامة modifybook يمتد jdialog الأدوات ActionListener {private Jlabel BookNumlabel ؛ خاص Jlabel Booknamelabel ؛ كاتب الكتب Jlabel الخاص ؛ خاص Jlabel BookPublishLabel ؛ Private Jlabel bookpricelabel ؛ خاص Jlabel Booktimelabel ؛ private Jtextfield BookNumtext ؛ Private Jtextfield BookeNmetext ؛ Private Jtextfield WorkwriterText ؛ خاص JTextField BookPublishText ؛ Private Jtextfield bookpricetext ؛ Private Jtextfield Booktimetext ؛ Private Jbutton Submitbut ؛ خاص jbutton cancelbut ؛ BookTableModel الخاص Bookmodel ؛ int int rownum ؛ Public ModifyBook (مالك الإطار ، عنوان السلسلة ، نوع Boolean ، نموذج BookTableModel ، int Row) {Super (Owner ، title ، type) ؛ BookModel = نموذج ؛ ROWNUM = ROW ؛ BookNumlabel = New Jlabel ("رقم الكتاب:") ؛ BookNameLabel = New Jlabel ("Book Title:") ؛ كاتب الكتب = New Jlabel ("المؤلف:") ؛ BookPublishLabel = New Jlabel ("Publisher:") ؛ bookpricelabel = new Jlabel ("Price:") ؛ BookTimElabel = New Jlabel ("Time Publishing:") ؛ BookNumText = New JTextField (10) ؛ BookEnmetext = New JTextfield (10) ؛ كاتب الكتب = New JTextfield (10) ؛ BookPublishText = New JTextField (10) ؛ bookpricetext = New JTextfield (10) ؛ booktimetext = New JTextfield (9) ؛ SubmitBut = New JButton ("تأكيد التعديل") ؛ CancelBut = New JButton ("إلغاء") ؛ submitBut.addActionListener (this) ؛ cancelbut.addActionListener (this) ؛ this.setBounds (350،150،400،260) ؛ this.setResible (false) ؛ this.setLayout (New BorderLayout ()) ؛ this.setValue () ؛ this.initlayout () ؛ } public void initlayout () {Container [] con1 = حاوية جديدة [6] ؛ لـ (int i = 0 ؛ i <6 ؛ i ++) con1 [i] = حاوية جديدة () ؛ 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 (كاتب الكتب) ؛ con1 [2] .add (كاتب الكتب) ؛ 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 = حاوية جديدة () ؛ con2.setLayout (New BorderLayout ()) ؛ con2.add (con1 [0] ، borderlayout.north) ؛ con2.add (con1 [1] ، borderlayout.center) ؛ con2.add (con1 [2] ، borderlayout.south) ؛ حاوية con3 = حاوية جديدة () ؛ con3.setLayout (New BorderLayout ()) ؛ con3.add (con1 [3] ، borderlayout.north) ؛ con3.add (con1 [4] ، borderlayout.center) ؛ con3.add (con1 [5] ، borderlayout.south) ؛ حاوية con4 = حاوية جديدة () ؛ Con4.SetLayout (New FlowLayout ()) ؛ con4.add (submitbut) ؛ Con4.add (CancelBut) ؛ حاوية con5 = حاوية جديدة () ؛ 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 (صحيح) ؛ } public void setValue () {this.booknumtext.settext ((string) bookmodel.getValueat (rownum ، 0)) ؛ this.booknumtext.setedable (false) ؛ this.booknametext.settext ((String) bookmodel.getValueat (Rownum ، 1)) ؛ this.bookwritertext.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 method method method aducated if ( bookpublishtext.getText (). "تأكيد تعديل مربع" ، joptionpane.yes_no_option) ؛ "+bookwritertext.gettext ()+" '، publish_house =' "+bookpublishtext.getText ()+" '، book_price =' "+bookpricetext.getText ()+" ، "، publish_time =" " booktableModel = New BooktableModel () ؛ } joptionpane.showmessagedialog (هذا ، "التعديل نجح" ، "موجه" ، joptionpane.plain_message) ؛ this.setVisible (false) ؛ } آخر إذا (n == joptionpane.no_option) {return ؛ }}} if ( }}}} نتائج تشغيل البرنامج:
الواجهة الرئيسية:
واجهة الاستعلام:
أضف واجهة كتاب:
تعديل الواجهة:
حذف العملية:
واجهة قاعدة البيانات:
لمزيد من المعلومات حول نظام الإدارة ، يرجى النقر فوق "موضوع نظام إدارة الخاص" للتعلم
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.