Library Management System Java version _java

Source: Internet
Author: User
Tags gettext stmt stub

The purpose of this paper is to master the database programming technology through the library management system, to connect the database correctly, to query, insert, delete and modify the information in the database.
content: Create a Bibliography information table in the database, including book title, author, publishing house, Publication date, ISBN, Price field. Design a GUI interface for bibliographic management. There are four tabs on the interface, namely query, insert, delete, modify. Click on the Query tab, the interface appears with the title, author, publishing house, ISBN four text boxes, a button and a read-only text area. text box content can be empty, enter the appropriate query information (for example, according to the title of the query can only enter the title), click on the interface of the "Query" button, you can in the text area below the interface to display the detailed information of the bibliography. Click on the Insert tab, the interface appears with the title, author, publisher, Publication date, ISBN, Price text box, a button. After you enter the information in the text box, click the Insert button, which inserts the bibliography information into the database table. Click on the Delete tab, the appearance of the interface has a title text box and a button, enter the title and click the "Delete" button, the bibliography information from the database table deleted. Click on the Change tab, the interface appears with the title, author, publishing house, Publication date, ISBN, Price text box, a button. The title you entered must already exist, or a message box will 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.
Source code:

Bookinfo.java

 * Project name: Book Management System * Version: 1.0 * Creator: Zhangjunqiang * Create Time: 2016/5/26 * */package Librarysystem; 
 
Import java.awt.*; 
 
Import javax.swing.*; 
Import java.awt.event.*; 
 
Import java.sql.*; @SuppressWarnings ("Serial") public class BookInfo extends JFrame implements actionlistener{//main face controls Private Jlabe 
  L 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 stub BookInfo 
    O=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{//Create controls on the main interface inputlabel=new JLabel ("Please enter the title:"); 
    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 ("modification"); 
    Deletebut=new JButton ("delete"); 
    Refreshbut=new JButton ("refresh"); 
    Searchbut.addactionlistener (this); 
    Addbut.addactionlistener (this); 
    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); 
    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 (); @Override public void actionperformed (ActionEvent e) {//TODO auto-generated Method stub if (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 block E1.printstacktrace (); 
      }}else{Joptionpane.showmessagedialog (this, "input cannot be null", "hint", joptionpane.plain_message); } else if (E.getsource () ==addbut) {@SuppressWarnings ("unused") Addbookdialog addwin=new Addbookdi Alog (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, prompt, Joptionpane.plain_messa 
      GE); 
        } else{//system.out.print (bookname); 
        int n = joptionpane.showconfirmdialog (null, "Confirm deletion?", "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 success", "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, prompt, Joptionpane.plain_messa 
      GE); else{@SuppressWarnings ("unused") Modifybook modifywin=new Modifybook (This, "Modify information," True,booktabl 
        Emodel,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 block E1.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 elements 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 ("Publication Time"); 
    Getcolname (). Add ("price"); * * * Import * * * */try {class.forname ("com.mysql.jdbc.Driver"); 
    catch (ClassNotFoundException e) {//TODO auto-generated catch block E.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") b 
    Oolean 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 (); @Override public int getColumnCount () {//Get the number of columns//TODO auto-generated method stub return This.colName.siz 
  E (); @Override public int GetRowCount () {//Get number of rows/TODO Auto-generAted method Stub return This.rowData.size (); @Override public Object getvalueat (int row, int col) {//Get data for a column in a row//TODO auto-generated method stub R 
  Eturn (This.rowData.get (Row)). Get (COL); @Override public String getcolumnname (int column) {//TODO auto-generated a stub return This.col 
  Name.get (column); 
  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-generated catch block E.printstacktrace (); 
  }//InitData ("SELECT * from Book_info"); 
 } public void Deletebook (String sql) {   try {stmt.executeupdate (SQL); 
    catch (SQLException E1) {//TODO auto-generated catch block E1.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 bookn 
  Umlabel; 
  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, is modal window super (Owner,title,model); 
    Booknumlabel=new JLabel ("ISBN:"); 
    Booknamelabel=new JLabel ("title:"); 
    Bookwriterlabel=new JLabel ("Author:"); Bookpublishlabel=new JLabel ("Publishing house:");
    Bookpricelabel=new JLabel ("Price:"); 
     
    Booktimelabel=new JLabel ("Publication 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); 
    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); @Override public void actionperformed (ActionEvent e) {//TODO auto-generated Method stub if (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 have empty", "hint", joptionpane.plain_message); 
        } else{//system.out.println ("input succeeded");  
            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 block E1.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 booknuml 
  Abel 
  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 ("book Name: "); 
    Bookwriterlabel=new JLabel ("Author:"); 
    Bookpublishlabel=new JLabel ("Publishing house:"); 
    Bookpricelabel=new JLabel ("Price:"); 
     
    Booktimelabel=new JLabel ("Publication 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); 
    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 (); @Override public void actionperformed (ActionevenT e) {//System.out.println (Bookpricetext.gettext ()); TODO auto-generated Method Stub if (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 have empty", "hint", joptionpane.plain_message); 
        else{int n = joptionpane.showconfirmdialog (null, "Confirm modification?", "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 block E1.printstacktrace (); 
          } joptionpane.showmessagedialog (This, "Modify success", "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:


Add Book Interface:



To modify the interface:



Delete action:



Database interface:


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.