Java Graphic Interface------book lending system

Source: Internet
Author: User
Tags rollback stmt stub

Look directly at the code:

Package TTT;
Import Java.awt.BorderLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;

Import java.sql.Statement;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;
Import Javax.swing.JScrollPane;
Import javax.swing.JTable;
Import Javax.swing.border.TitledBorder;
Import Javax.swing.table.DefaultTableModel;
        public class Labmsys extends jframe{JTable booktable=null;
        Static DefaultTableModel Bookmodel=null;
        JTable Borrowtable=null;
        Static DefaultTableModel Bmodel=null;
        JPanel j1=new JPanel ();
        JPanel j2=new JPanel ();
        JPanel j3=new JPanel ();
        JButton button1=new JButton ("Borrow books");



    JButton button2=new JButton ("return book");
        Public Labmsys () {this.setlayout (New BorderLayout ());
        This.add (j1,borderlayout.west); This.add (j2,bOrderlayout.east);
        This.add (J3,borderlayout.center);
        J1.setborder (New Titledborder ("Book Library Table");
        J2.setborder (New Titledborder ("Book Inventory table");
        This.booktable=bookin ();
        This.borrowtable=borrowin ();
        JScrollPane jsp1=new JScrollPane (borrowtable);
        JScrollPane jsp2=new JScrollPane (booktable);
        J1.add (JSP1);
        J2.add (JSP2);
        J3.add (button1);
        J3.add (button2); Add a library listener Button1.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {//TODO auto-generated method stub int rownum=booktable.g
                Etselectedrow ();
                System.out.println (rownum);
                    if (rownum>=0) {String isbn=booktable.getvalueat (rownum, 0). toString ();
                    System.out.println (ISBN); int Count=integer.parseint ((String) booktable.getvalueat (rownUm, 2));
                    String s=joptionpane.showinputdialog ("Please enter a school number");
                    if (count<=0) joptionpane.showmessagedialog (NULL, "insufficient book stock");
                    else if (null==s) joptionpane.showmessagedialog (null, "Please enter a school number"); else{if (Borrowbook (isbn,s)) {System.out.prin
                            TLN ("yes");
                        Joptionpane.showmessagedialog (NULL, "successful!");} else Joptionpane.showmessagedialog (null, "wrong!");}
        else Joptionpane.showmessagedialog (null, "Choose book!");}); Add Book Listener Button2.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {//TODO auto-generated method stub int Rownum=borrowtable . getSelectedrow ();
                System.out.println (rownum);
                    if (rownum>=0) {String isbn=borrowtable.getvalueat (rownum, 2). ToString ();
                    String tablexh = Borrowtable.getvalueat (rownum, 1). ToString ();
                    System.out.println (ISBN);
                    String xh=joptionpane.showinputdialog ("Please enter a school number");
                    if (!xh.equals (TABLEXH)) Joptionpane.showmessagedialog (null, "incorrect study number"); else{if (Returnbook (ISBN)) {joptionpane.showm
                        Essagedialog (NULL, "successful!");} else Joptionpane.showmessagedialog (null, "wrong!");}



    else Joptionpane.showmessagedialog (null, "Choose book!");}); public boolean Borrowbook (String ISBN, string s) {int xh = inteGer.parseint (s);
                int isbn2 =integer.parseint (ISBN);
        System.out.println ("School No.:" +XH);
        Boolean b=false;
        String driver= "Com.mysql.jdbc.Driver";
        String url= "Jdbc:mysql://localhost:3306/test";
        String user= "root";

        String password= "123456";
        Connection Conn=null;

        Statement Stmt=null; 
               try {class.forname (driver);  
               conn = drivermanager.getconnection (URL, user, password);  

               stmt = Conn.createstatement ();
               System.out.println ("ISBN:" +isbn);
               Conn.setautocommit (FALSE);
               String SQL1 = "Update book set count=count-1 where isbn= '" +isbn2+ "";
               String sql2 = "INSERT into borrow (XH,ISBN) VALUES (' +xh+" ', ' "+isbn2+ ')";

               Stmt.executeupdate (SQL1);
               Stmt.executeupdate (SQL2);
               System.out.println ("ISBN2:" +ISBN2); stmt = (PreparedStatement) conn.preparestaTement ("Update book set count=count-1 where isbn=?");
               Conn.commit ();
               Stmt.close ();
               Conn.close ();
        B=true;
               catch (Exception e) {try{conn.rollback ();
              }catch (Exception E1) {e1.printstacktrace ();}
            return b;

           Private Boolean Returnbook (String ISBN) {int isbn2 =integer.parseint (ISBN);
           Boolean b=false;
           String driver= "Com.mysql.jdbc.Driver";
           String url= "Jdbc:mysql://localhost:3306/test";
           String user= "root";


           String password= "123456";
           Connection Conn=null;


           Statement Stmt=null; 
             try {class.forname (driver);  
             conn = drivermanager.getconnection (URL, user, password);  
             stmt = Conn.createstatement ();
             Conn.setautocommit (FALSE); Stmt.executeupdate ("Delete F")Rom borrow where isbn= ' "+isbn2+" ");
             Stmt.executeupdate ("Update book set count=count+1 where isbn= '" +isbn2+ "");
             System.out.println ("Return book ISBN2:" +ISBN2);
             Conn.commit ();
             Stmt.close ();
             Conn.close ();
             B=true;
                   catch (Exception e) {try{conn.rollback ();  
           }catch (Exception E1) {e1.printstacktrace ();}}
    return b;
        Private JTable Borrowin () {//TODO auto-generated method Stub String []head={"id", "XH", "ISBN"};
        String [][]data=null;
        Bookmodel=new DefaultTableModel (Data,head);
        JTable t=new JTable (Bookmodel);
        String driver= "Com.mysql.jdbc.Driver";
        String url= "Jdbc:mysql://localhost:3306/test";
        String user= "root";

        String password= "123456";
        Connection Conn=null;
        Statement Stmt=null;
        ResultSet Rs=null;  
              try { Class.forName (driver);  
               conn = drivermanager.getconnection (URL, user, password);  
               stmt = Conn.createstatement (); 
               Rs=stmt.executequery ("SELECT * from borrow");
               String[] Row=new string[3];
               while (Rs.next ()) {row[0]=rs.getstring (1);
               Row[1]=rs.getstring (2);
               Row[2]=rs.getstring (3);
               Bookmodel.addrow (row); Bookmodel.firetabledatachanged ();
               } rs.close ();
               Stmt.close ();

        Conn.close ();}  
              catch (Exception e) {//TODO auto-generated catch block System.out.println (e);
    } return t; JTable Bookin () {//TODO auto-generated method Stub String []head={' ISBN ', ' name ', ' count '}
        ;
        String [][]data=null;
        Bmodel=new DefaultTableModel (Data,head);
        JTable t=new JTable (Bmodel);String driver= "Com.mysql.jdbc.Driver";
        String url= "Jdbc:mysql://localhost:3306/test";
        String user= "root";

        String password= "123456";
        Connection Conn=null;
        Statement Stmt=null;
        ResultSet Rs=null; 

               try {class.forname (driver);  
               conn = drivermanager.getconnection (URL, user, password);  
               stmt = Conn.createstatement (); 
               Rs=stmt.executequery ("SELECT * from book");
               String[] Row=new string[3];
               while (Rs.next ()) {row[0]=rs.getstring (1);
               Row[1]=rs.getstring (2);
               Row[2]=rs.getstring (3);
               Bmodel.addrow (row); Bmodel.firetabledatachanged ();
               } rs.close ();
               Stmt.close ();

        Conn.close ();}  
              catch (Exception e) {//TODO auto-generated catch block System.out.println (e); } returN T; public static void Main (string[] args) {//TODO auto-generated method stub Labmsys n=new Labmsys ( 
        );
        N.settitle ("Book Lending Management System");
        N.setsize (1000,500);
        N.setlocationrelativeto (NULL);
        N.setdefaultcloseoperation (Jframe.exit_on_close);
    N.setvisible (TRUE);
 }

}

Database:

----Table structure for table ' book '--DROP Table IF EXISTS ' book '; CREATE TABLE ' book ' (' ISBN ' int (one) not null, ' name ' varchar (x) not NULL, ' count ' int (one) default NULL, PRIMARY

KEY (' ISBN ') Engine=innodb DEFAULT Charset=utf8;
----dumping data for table "book"--LOCK TABLES ' book ' WRITE;
/*!40000 ALTER TABLE ' book ' DISABLE KEYS * * *;
INSERT into ' book ' VALUES (662530, ' C # ', 9), (662545, ' Python ',), (663520, ' C + + ', 6), (663548, ' Java ', 8);
/*!40000 ALTER TABLE ' book ' ENABLE KEYS * * *;

UNLOCK TABLES;
----Table structure for table ' borrow '--DROP Table IF EXISTS ' borrow '; CREATE TABLE ' borrow ' (' id ' int () NOT null auto_increment, ' XH ' int (one) Not null, ' ISBN ' Int (one) not NULL, PRIM

ARY KEY (' id ')) engine=innodb DEFAULT Charset=utf8;
----dumping data for table ' borrow '--LOCK TABLES ' borrow ' WRITE;
/*!40000 ALTER TABLE ' borrow ' DISABLE KEYS * * *; INSERT into ' borrow ' VALUES (1006,123456,662545), (1007,456789,663520), (1009,789456,662530), (1010,741852,662530); 

Run results show:

Done.
Attach Source code Package

http://download.csdn.net/detail/broccoli2/9816754

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.