Java version: library Management system

Source: Internet
Author: User
Tags gettext

Experiment Objective : Mastering the Database Programming technology. Can connect the database correctly, can query, insert, delete, modify the information in the database.

Experimental content : Create a bibliographic information table in the database, including the title, author, publishing house, Publication date, ISBN, Price field. Design a GUI interface for bibliographic management. There are four tabs on this interface, namely, query, insert, delete, modify. Click on the Query tab, the interface appears with the title, author, publisher, ISBN four text box, a button and a read-only text area. text box content can be empty, after entering the appropriate query information (for example, according to the title query can only enter the title), click the "Query" button on the interface, you can display in the text area below the interface to match the conditions of the bibliography details. Click on the Insert tab and the interface appears with the title, author, publisher, Publication date, ISBN, Price text box, and a button. After entering the information in the text box, click the Insert button and the bibliography information is inserted into the database table. Click the Delete tab, there is a title text box on the interface and a button, enter the title and click the "Delete" button, the bibliography information is removed from the database table. Click on the Modify tab, the interface appears with the title, author, publishing house, Publication date, ISBN, Price text box, a button. The title of the entry must already exist, or the message box will pop up to display an error message. After entering the information, click the "Modify" button, the corresponding bibliographic information in the database table is modified to the new value.

Experimental Source:

Bookinfo.java

<strong>/</strong>* * Project name: Book Management System * Version: 1.0 * Created by: Zhangjunqiang * Created: 2016/5/26 * */package Librarysystem;import J Ava.awt.*;import javax.swing.*;import java.awt.event.*;import java.sql.*; @SuppressWarnings ("Serial") public class BookInfo extends JFrame implements actionlistener{//the controls on the main side are private JLabel inputlabel;private JTextField Inputtext; Private JButton searchbut;private JTable booktable;private jscrollpane bookscroll;private JButton addbut;private JButton modifybut;private JButton deletebut;private JButton refreshbut;private Booktablemodel booktablemodel;public static void Main (string[] args) throws SQLException {//TODO auto-generated method Stubbookinfo bookinfo=new bookInfo (); Bo Okinfo.setdefaultcloseoperation (Jframe.exit_on_close), bookinfo.setbounds (+,-), Bookinfo.setvisible ( true);//bookinfo.importsql ();//Export Data bookinfo.setminwindowlayout ();//Set Data}public BookInfo () throws sqlexception{// Create a control on the main interface inputlabel=new JLabel ("Please enter the title:"); inputtext=new JTextField (SEARC)Hbut=new JButton ("Enquiry"); booktablemodel=new Booktablemodel (); booktable=new JTable (Booktablemodel); bookscroll=new JScrollPane (booktable); Addbut=new JButton ("add") modifybut=new JButton ("modify");d eletebut=new JButton ("delete"); Refreshbut=new JButton ("refresh"); Searchbut.addactionlistener (this); Addbut.addactionlistener (this); Refreshbut.addactionlistener (This), Modifybut.addactionlistener (this),;d Eletebut.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); Container con2=new Container () 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 ();} @Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method Stubif (E.getsource () ==searchbut) {if (!this.iNputtext.gettext (). Equals ("")) {String bookname=this.inputtext.gettext ();   String sql= "SELECT * from book_info WHERE book_name = '" +bookname+ "'"; try {booktablemodel=new booktablemodel (SQL); Booktable.setmodel (Booktablemodel);} catch (SQLException E1) {//TODO Auto   -generated catch Blocke1.printstacktrace ();} }else{joptionpane.showmessagedialog (This, "input cannot be empty", "hint", Joptionpane.plain_message);}} else if (E.getsource () ==addbut) {@SuppressWarnings ("unused") Addbookdialog addwin=new Addbookdialog (This, "Add book", True ); this.refreshtable ();} else if (E.getsource () ==refreshbut) {this.refreshtable ();} else if (E.getsource () ==deletebut) {int rownum=booktable.getselectedrow (); if (rownum<0| | Rownum>booktable.getrowcount ()) {Joptionpane.showmessagedialog (this, "unchecked", "hint", joptionpane.plain_message);} Else{//system.out.print (bookname); int n = joptionpane.showconfirmdialog (null, "Confirm delete?", "Confirm Delete box", Joptionpane.yes_no_ OPTION), if (n = = joptionpane.yes_option) {string booknum= (String) booktable.getvalueat (rowNum, 0); String sql= "DELETE from Book_info WHERE book_num= '" +booknum+ "'"; Booktablemodel.deletebook (sql); this.refreshtable (); Joptionpane.showmessagedialog (This, "Delete succeeded", "hint", joptionpane.plain_message);} else if (n = = joptionpane.no_option) {return;}}} else if (E.getsource () ==modifybut) {Booktable.setmodel (booktablemodel); int Rownum=booktable.getselectedrow (); if ( rownum<0| | Rownum>booktable.getrowcount ()) {Joptionpane.showmessagedialog (this, "unchecked", "hint", joptionpane.plain_message);}    else{@SuppressWarnings ("unused") Modifybook modifywin=new Modifybook (This, "Modified information", true,booktablemodel,rownum); This.refreshtable ();}}} public void Refreshtable () {Booktablemodel Searchbook;try {searchbook = new Booktablemodel ("SELECT * from Book_info"); Booktable.setmodel (Searchbook); booktablemodel=searchbook;} catch (SQLException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}}}

Booktablemodel.java

Package Librarysystem;import java.sql.*;import java.util.*;/* * Book table model * */import javax.swing.table.*; @SuppressWarnings ("Serial") public class Booktablemodel extends abstracttablemodel{//Table element private vector<vector<string>>    Rowdata;private vector<string> colname;    Database private PreparedStatement stmt; Private ResultSet result;public Booktablemodel (String sql) throws 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 ("ISBN"); Getcolname (). Add ("title"); Getcolname (). Add ("Author"); Getcolname (). Add ("Publishing house"); Getcolname (). Add ("Publishing Time"); Getcolname (). Add ("price");/* * Database Import * */try {class.forname (" Com.mysql.jdbc.Driver ");} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} String url= "Jdbc:mysql://localhost:3306/device"; String user= "root"; String password= "zjq1314520";    Connection con=drivermanager.getconnection (Url,user,password);    stmt = con.preparestatement (sql);    Result=stmt.executequery (); Importsql ();} void Importsql () throws sqlexception{//TODO auto-generated 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); signnull=false;} Result.close ();} @Overridepublic int getColumnCount () {//Gets the number of columns//TODO auto-generated method Stubreturn this.colName.size ();} @Overridepublic int GetRowCount () {//Gets the number of rows//TODO auto-generated method Stubreturn this.rowData.size ();} @Overridepublic Object getvalueat (int row, int col) {//Get the data for a column of a row//TODO auto-generated method Stubreturn (this.rowData.ge T (Row)). Get (col);} @Overridepublic String getcolumnname (int column) {//TODO auto-generated method Stubreturn this.colName.get (column);} Public VECTOR&LT;VECTOR&LT;STRING&GT;&GT 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-generated catch Blocke . Printstacktrace ();} InitData ("SELECT * from Book_info");} public void Deletebook (String sql) {try {stmt.executeupdate (SQL)} catch (SQLException E1) {//TODO auto-generated catch bl Ocke1.printstacktrace ();}}}
Addbookdialog.java

Package Librarysystem;import Java.awt.*;import Java.awt.event.*;import java.sql.sqlexception;import javax.swing.*;@ Suppresswarnings ("Serial") public class Addbookdialog extends JDialog implements Actionlistener{private JLabel Booknumlabel;private JLabel booknamelabel;private JLabel bookwriterlabel;private JLabel bookpublishlabel;private JLabel bookpricelabel;private JLabel booktimelabel;private jtextfield booknumtext;private JTextField bookNameText; Private JTextField bookwritertext;private JTextField bookpublishtext;private JTextField bookPriceText;private JTextField booktimetext;private JButton submitbut;private JButton cancelbut;public addbookdialog (Frame owner,String Title,boolean model) {//parent window, window name, whether the mode window is super (Owner,title,model); Booknumlabel=new JLabel ("ISBN:"); booknamelabel=new    JLabel ("title:"); Bookwriterlabel=new JLabel ("Author:"); Bookpublishlabel=new JLabel ("publisher:"); bookpricelabel=new JLabel ("Price Booktimelabel=new JLabel ("Publication Time:"), Booknumtext=new JTextField (Booknametext);=new JTextField (ten); Bookwritertext=new JTextField (Bookpublishtext=new jtextfield); bookpricetext=new JTextField (Booktimetext=new JTextField (9); Submitbut=new JButton ("confirmation"); Cancelbut=new JButton ("cancellation"); Submitbut.addactionlistener (This), Cancelbut.addactionlistener (this); This.setbounds (350,150,400,260); This.setresizable (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); Container 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); Container 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); Container con4=new Container (); Con4.setlayout (new FlowLayout ()); Con4.add (submitbut); Con4.add (cancelbut); Container 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);} @Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method Stubif (E.getsource () ==submitbut) {if (Booknumtext.gettext (). Equals ("") | | Booknametext.gettext (). Equals ("") | | Bookwritertext.gettext (). Equals ("") | | Bookpublishtext.gettext (). Equals ("") | |Bookpricetext.gettext (). Equals ("") | |    Booktimetext.gettext (). Equals ("")) {//system.out.println ("input failed"); Joptionpane.showmessagedialog (This, "input cannot be empty", "hint", joptionpane.plain_message);} ELSE{//SYSTEM.OUT.PRINTLN ("input success"); String sql= "INSERT into" + "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 () + "', '" +booktimetext.gettext () + "')"; try {Booktablemodel Book=new Booktablemodel (); Book.addbook (SQL);} catch (SQLException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}    Joptionpane.showmessagedialog (This, "Add success", "hint", joptionpane.plain_message); This.setvisible (FALSE);}} if (E.getsource () ==cancelbut) {this.setvisible (false);}}}
Modifybook.java

Package Librarysystem;import Java.awt.*;import Java.awt.event.*;import java.sql.sqlexception;import javax.swing.*;@ Suppresswarnings ("Serial") public class Modifybook extends JDialog implements Actionlistener{private JLabel Booknumlabel;private JLabel booknamelabel;private JLabel bookwriterlabel;private JLabel bookpublishlabel;private JLabel bookpricelabel;private JLabel booktimelabel;private jtextfield booknumtext;private JTextField bookNameText; Private JTextField bookwritertext;private JTextField bookpublishtext;private JTextField bookPriceText;private JTextField booktimetext;private JButton submitbut;private JButton cancelbut;private Booktablemodel bookModel;private int Rownum;public Modifybook (Frame owner,string title,boolean type,booktablemodel model,int Row) {Super (Owner,title, type); Bookmodel=model;rownum=row;booknumlabel=new JLabel ("ISBN:"); booknamelabel=new JLabel ("title:"); bookwriterlabel=   New JLabel ("Author:"); Bookpublishlabel=new JLabel ("publisher:"); bookpricelabel=new JLabel ("Price Booktimelabel=new JLabel ("Publication Time:"); booknumtext=new JTextField (10); Booknametext=new JTextField Bookwritertext=new JTextField (10), Bookpublishtext=new JTextField (bookpricetext=new JTextField); Booktimetext=new JTextField (9) submitbut=new JButton ("Confirm modification"); cancelbut=new JButton ("cancellation"); Submitbut.addactionlistener (This), Cancelbut.addactionlistener (this); This.setbounds (350,150,400,260); This.setresizable (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); Container 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); Container 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); Container con4=new Container (); Con4.setlayout (new FlowLayout ()); Con4.add (submitbut); Con4.add (cancelbut); Container 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.setEditable (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 ();} @Overridepublic void actionperformed (ActionEvent e) {//system.out.println (Bookpricetext.gettext ());//TODO Auto-generated Method Stubif (E.getsource () ==submitbut) {if (Booknumtext.gettext (). Equals ("") | | Booknametext.gettext (). Equals ("") | | Bookwritertext.gettext (). Equals ("") | | Bookpublishtext.gettext (). Equals ("") | | Bookpricetext.gettext (). Equals ("") | |    Booktimetext.gettext (). Equals ("")) {//system.out.println ("input failed"); Joptionpane.showmessagedialog (This, "Modify cannot be empty", "hint", joptionpane.plain_message);} Else{int n = joptionpane.showconfirmdialog (null, "confirm modify?", "confirm Modify Box", joptionpane.yes_no_option); if (n = = Joptionpane.yes _option) {String sql= "UPDATE book_info SET book_name = '" +bOoknametext.gettext () + "', book_writer= '" +bookwritertext.gettext () + "', publish_house= '" +bookpublishtext.gettext () + "', Book_price= '" +bookpricetext.gettext () + "', Publish_time= '" +booktimetext.gettext () + "' WHERE Book_num = '" + Booknumtext.gettext () + "'"; try {Booktablemodel book=new booktablemodel (); Book.addbook (SQL);} catch (SQLException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}    Joptionpane.showmessagedialog (This, "modified successfully", "hint", joptionpane.plain_message); This.setvisible (false);} else if (n = = joptionpane.no_option) {return;}}} if (E.getsource () ==cancelbut) {this.setvisible (false);}}}
Program Run Result:
Main interface:


Query interface:


To add a book interface:



Modify the interface:



Delete operation:



Database interface:



Java version: library Management system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.