My first project----Java Library Management System __java

Source: Internet
Author: User
Tags stringbuffer

Project Reference from: http://www.java1234.com/a/yuanchuang/swing2/
Project video and code download address: Link: Http://pan.baidu.com/s/1pLpQw2J Password: CNCV
Construction of the project development environment: Http://pan.baidu.com/s/1ntzhAmH#list/path=%2F

One, function

(1), User login
(2), Book category management
(3), book Management
(4), exit

Second, tools

(1), Java Programming: Eclipes (1.8 soon version)
(2), Sql:mysql
(3), Jdbc:jar (Mysql-connector-java-5.1.40-bin.jar)

Three, the effect shows

(1), Login

(2), the main interface

(3), Book category add

(4), Book category management

(5), book add

(6), book Management

(7), about the author

Four, database design

(1), T_user table

(2), T_booktype table

(3), T_book table

(iv), Java hierarchy Analysis:

(1), Logic diagram

(2), package structure

(v) Database-level analysis:

1, ER analysis

2, data

User: User ID, username, password
Book Category: Book category number, book category name
Books: Book number, book name, book author, book Price, book description, Book category (Foreign key)

The categories of books and books are related to each other according to the category of books

3, the establishment of database tables
(1), T_use User Information table
(2), T_booktype Book category management table
(3), T_book Book Information management table

4, Association of Database Tables (association of foreign Keys)

(vi), main Java code Analysis:

(1), Dao class (take Bookdao for example)

Package Com.java1234.dao;
Import Java.sql.ResultSet;

Import java.sql.SQLException;
Import Com.java1234.model.Book;
Import Com.java1234.model.BookType;
Import Com.java1234.util.StringUtil;
Import com.mysql.jdbc.Connection;

Import com.mysql.jdbc.PreparedStatement; 
     /** * Book DAO class * @author h_pioneer * */public class Bookdao {/** * books add * @param con * @param book * @return * @throws Exception * * public int Add (Connection con,book book) throws exception{Str
        ing sql= "insert into t_book values (null,?,?,?,?,?,?)";
        PreparedStatement pstmt= (PreparedStatement) con.preparestatement (SQL);
        Pstmt.setstring (1, Book.getbookname ());
        Pstmt.setstring (2, Book.getauthor ());
        Pstmt.setstring (3, Book.getsex ());
        Pstmt.setfloat (4, Book.getprice ());
        Pstmt.setint (5, Book.getbooktypeid ());
        Pstmt.setstring (6, Book.getbookdesc ());
    return Pstmt.executeupdate (); /** * Book Information inquiry * @param Con * @param book * @return * @throws Exception/public ResultSet list (Connection con,book book ) throws exception{StringBuffer sb=new stringbuffer ("select * from T_book b,t_booktype BT where b.booktypeid=bt.id
        ");
        if (Stringutil.isnotempty (Book.getbookname ())) {Sb.append ("and b.bookname like '%" +book.getbookname () + "%"); } if (Stringutil.isnotempty (Book.getauthor ())) {Sb.append ("and b.author like '%" +book.getauthor
        ()+"%'"); } if (Book.getbooktypeid ()!=null && Book.getbooktypeid ()!=-1) {sb.append ("and b.booktypeid=" +b
        Ook.getbooktypeid ());
        } PreparedStatement pstmt= (PreparedStatement) con.preparestatement (sb.tostring ());
    return Pstmt.executequery (); /** * Book Information delete * @param con * @param ID * @return * @throws SQLException/Public int delete (Connection con,string id) throws exception{String sql= "deLete from T_book where id=? ";
        PreparedStatement pstmt= (PreparedStatement) con.preparestatement (SQL);
        Pstmt.setstring (1, id);
    return Pstmt.executeupdate (); /** * Book Information modification * @param con * @param books * @return * @throws Exception/Public I NT Update (Connection Con,book book) throws exception{String sql= "Update T_book set Bookname=?,author=?,sex=?,price =?,bookdesc=?,booktypeid=?
        where id=? ";


        PreparedStatement pstmt= (PreparedStatement) con.preparestatement (SQL);
        Pstmt.setstring (1, Book.getbookname ());
        Pstmt.setstring (2, Book.getauthor ());
        Pstmt.setstring (3, Book.getsex ());
        Pstmt.setfloat (4, Book.getprice ());
        Pstmt.setstring (5, Book.getbookdesc ());
        Pstmt.setint (6, Book.getbooktypeid ());
        Pstmt.setint (7, Book.getid ());
    return Pstmt.executeupdate (); /** * * @param con * @param booktypeid * @return * @throws excEption */Public boolean Existbookbybooktypeid (Connection con,string booktypeid) throws exception{String
        Sql= "SELECT * from T_book where booktypeid=?";
        PreparedStatement pstmt= (PreparedStatement) con.preparestatement (SQL);
        Pstmt.setstring (1, Booktypeid);
        ResultSet rs = Pstmt.executequery ();
        String string = new string ();
    return Rs.next ();
 }
}

* Key content::

JDBC to make simple database additions and deletions to check

Detailed reference: http://www.cnblogs.com/wuyuegb2312/p/3872607.html

(2), model class (take Bookmodel as an example)

Package Com.java1234.model;  /** * Library Entity class * @author H_pioneer * */public class Book {private int id;//number private String bookname;  Book name private String author;  author private String sex;  Sex private float price;  Price private Integer Booktypeid;  Book category private String Booktypename;  Book category name private String Bookdesc; Note Public book (int id2, string bookname, string author, string sex, float price, Integer booktypeid, String bookd
        ESC) {super ();
        This.id = Id2;
        This.bookname = BookName;
        This.author = author;
        This.sex = sex;
        This.price = Price;
        This.booktypeid = Booktypeid;
    This.bookdesc = Bookdesc;
        Public Book (string bookname, string author, Integer Booktypeid) {super ();
        This.bookname = BookName;
        This.author = author;
    This.booktypeid = Booktypeid; Public Book (string bookname, string author, string sex, float price, integeR Booktypeid, String Bookdesc) {super ();
        This.bookname = BookName;
        This.author = author;
        This.sex = sex;
        This.price = Price;
        This.booktypeid = Booktypeid;
    This.bookdesc = Bookdesc;
        Public book () {super ();
    TODO auto-generated Constructor stub} public int getId () {return id;
    The public void setId (int id) {this.id = ID;
    Public String Getbookname () {return bookname;
    } public void Setbookname (String bookname) {this.bookname = BookName;
    Public String Getauthor () {return author;
    } public void Setauthor (String author) {this.author = author;
    Public String Getsex () {return sex;
    } public void Setsex (String sex) {this.sex = sex;
    public float GetPrice () {return price;
    public void Setprice (float price) {this.price = Price; } PublIC Integer Getbooktypeid () {return booktypeid;
    The public void Setbooktypeid (Integer booktypeid) {This.booktypeid = Booktypeid;
    Public String Getbooktypename () {return booktypename;
    } public void Setbooktypename (String booktypename) {this.booktypename = Booktypename;
    Public String Getbookdesc () {return bookdesc;
    } public void Setbookdesc (String bookdesc) {this.bookdesc = Bookdesc;
 }


}

* * Key content::

(1) The method of constructing the interface class

(2), Get,set method

Shortcut keys:
Shift+alt+s–> Generate getters and setters–> Select the Get,set parameters you need

(3) The use of constructors
Shortcut keys:
(1), Shift+alt+s–>generate constructor using fields–> uses field generation
(2), shift+alt+s–>generate Constructors from.....–> does not use a field to obtain from the parent class

(iii), UTIL class

Package com.java1234.util;

Import Java.sql.DriverManager;

Import com.mysql.jdbc.Connection; /** * Database Tools class * @author h_pioneer * * */public class Dbutil {private String Dburl = "jdbc:mysql://localhost:3306/d
    B_book ";
    It can also be written as private String Dburl = "Jdbc:mysql:///db_book";
    Private String dbusername = "root";
    Private String Dbpassword = "123456";

    Private String Jdbcname = "Com.mysql.jdbc.Driver";
        /** * Get Database connection * @return * @throws Exception/public Connection Getcon () throws exception{
        Class.forName (Jdbcname);

    Connection con = (Connection) drivermanager.getconnection (Dburl,dbusername,dbpassword);//link database return con;  /** * Close Database connection * @param con * @throws Exception/public void Closecon (java.sql.Connection
        Con) throws Exception {if (con!=null) {con.close (); }/** * * @param args/public static void main (string[] args){Dbutil dbutil = new Dbutil ();
            try {Dbutil.getcon ();
        SYSTEM.OUT.PRINTLN ("Database connection succeeded");    catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
            The location and cause of the error in the program where the exception information is printed at the command line.
        SYSTEM.OUT.PRINTLN ("Database Connection");
 }

    }


}

Database Tools Class

Package com.java1234.util;

Import Org.junit.Test;

Import Com.mysql.jdbc.StringUtils;

/**
 * String Tool class
 * @author h_pioneer * * */Public
class Stringutil {
    /**
     * determine if NULL
     * @param Str
     * @return
     *

    /public static Boolean IsEmpty (String str) {
        if (str==null| | ""). Equals (Str.trim ())) {return
            true;
    } else{return
        false;
    }

    /**
     * Judgement is NOT NULL
     * @param str
     * @return
    /public static Boolean isnotempty (String str) {
        if ( Str!=null&&! "". Equals (Str.trim ())) {return
            true;
        } else{return
        false;}}

* * Focus::
Use of tool classes
(1)
Summary of the String tool class:
Http://www.cnblogs.com/DreamDrive/p/5760588.html
(2)
Summary of Database Tools classes:
http://kettas.iteye.com/blog/1222519

(iv), FRM class (with login and book category added as an example)

Package Com.java1234.view;
Import Java.awt.EventQueue;
Import Java.awt.Font;
Import java.awt.event.ActionEvent;

Import Java.awt.event.ActionListener;
Import Javax.swing.GroupLayout;
Import javax.swing.GroupLayout.Alignment;
Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;
Import Javax.swing.JPasswordField;
Import Javax.swing.JTextField;
Import javax.swing.LayoutStyle.ComponentPlacement;

Import Javax.swing.border.EmptyBorder;
Import Com.java1234.dao.UserDao;
Import Com.java1234.model.User;
Import Com.java1234.util.DbUtil;
Import Com.java1234.util.StringUtil;


Import com.mysql.jdbc.Connection;
    public class Logonfrm extends JFrame {private JPanel contentpane;
    Private final JTextField TextField = new JTextField ();

    Private JPasswordField Passwordtxt;
    Private Dbutil Dbutil = new Dbutil ();
    Private Userdao Userdao = new Userdao (); Private JtexTfield Usernametxt;
     /** * Launch the application. */public static void main (string[] args) {Eventqueue.invokelater (new Runnable () {public void Ru
                    N () {try {logonfrm frame = new logonfrm ();
                Frame.setvisible (TRUE);
                catch (Exception e) {e.printstacktrace ();
    }
            }
        });
     }/** * Create the frame.
        * * Public logonfrm () {setdefaultcloseoperation (jframe.exit_on_close);
        Setalwaysontop (TRUE);
        Settitle ("admin login");
        SetBounds (100, 100, 450, 300);
        ContentPane = new JPanel ();
        Contentpane.setborder (New Emptyborder (5, 5, 5, 5));

        Setcontentpane (ContentPane);
        JLabel label = new JLabel ("Book Management System");
        Label.setfont (New Font ("bold", Font.Bold, 25));

        Label.seticon (New ImageIcon (LogOnFrm.class.getResource ("/images/logo.png")); JLabel LBLNewlabel = new JLabel ("username:");
        Lblnewlabel.setfont (New Font ("Song Body", Font.plain, 13));

        Lblnewlabel.seticon (New ImageIcon (LogOnFrm.class.getResource ("/images/username.png"));
        JLabel lblnewlabel_1 = new JLabel ("Password:");
        Lblnewlabel_1.setfont (New Font ("Song Body", Font.plain, 13));
        Lblnewlabel_1.seticon (New ImageIcon (LogOnFrm.class.getResource ("/images/password.png")); TextField
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.