Java+MySQL 图书管理系统
一,功能
管理员登录
图书借阅信息管理
图书信息管理
管理员更改密码
退出系统
二,工具
eclipse version: 2018-09 (4.9.0)
mysql workbench 8.0 ce
mysql-connector-java-8.0.13.jar
三、效果图:
登录界面:
主界面:
借阅书籍管理:
个人书库管理:
更改密码:
四、数据库设计
1)图书表
2)用户表
两个数据表间没有关联:
五、java层次分析
(1)逻辑图
(2)包结构,采用mvc三层架构组织各个模块
六、主要java代码分析
dao类(以bookdao为例)
package pers.cyz.dao; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.util.arraylist; import java.util.list; import pers.cyz.model.book; import pers.cyz.util.dbutil; /** * 数据库图书表信息数据访问对象类,包含增加图书信息、删除图书信息 * 、更新图书信息、查询图书信息、查询借阅信息和归还图书 * * @author 1651200111 陈彦志 */ public class bookdao { /** * 增加图书信息 */ public void addbook(book book) throws exception{ // 首先拿到数据库的连接 connection con = dbutil.getconnection(); string sql="insert into tb_books" // isbn、书名、图书价格、图书作者、出版社 + "(isbn, book_name, book_price, book_author, published_house," // 分类号、借书人姓名、借书人电话、借书日期,已借天数 + "book_category, borrower_name, borrower_phone) " + "values(" /* * 参数用?表示,相当于占位符,然后在对参数进行赋值。当真正执行时, * 这些参数会加载在sql语句中,把sql语句拼接完整才去执行。这样就会减少对数据库的操作 */ + "?,?,?,?,?,?,?,?)"; /* * preparestatement这个方法会将sql语句加载到驱动程序conn集成程序中, * 但是并不直接执行,而是当它调用execute()方法的时候才真正执行; */ preparedstatement psmt = con.preparestatement(sql); // 先对应sql语句,给sql语句传递参数 psmt.setstring(1, book.getisbn()); psmt.setstring(2, book.getbookname()); psmt.setfloat(3, book.getprice()); psmt.setstring(4, book.getauthor()); psmt.setstring(5, book.getpublishhouse()); psmt.setstring(6, book.getbookcategory()); if (book.getborrowername() == null || book.getborrowername() == "") { psmt.setstring(7, null); } else { psmt.setstring(7, book.getborrowername()); } if (book.getborrowerphone() == null || book.getborrowerphone() == "") { psmt.setstring(8, null); } else { psmt.setstring(8, book.getborrowerphone()); } //执行sql语句 psmt.execute(); } /** * 删除图书信息 */ public void delbook(int id) throws sqlexception{ // 首先拿到数据库的连接 connection con=dbutil.getconnection(); string sql="" + "delete from tb_books "+ // 参数用?表示,相当于占位符 "where id = ?"; // 预编译sql语句 preparedstatement psmt = con.preparestatement(sql); // 先对应sql语句,给sql语句传递参数 psmt.setint(1, id); // 执行sql语句 psmt.execute(); } /** * 更新图书信息 */ public void changebook(book book) throws sqlexception{ // 首先拿到数据库的连接 connection con=dbutil.getconnection(); string sql="update tb_books " + "set isbn = ?, book_name = ?, book_price = ?, book_author = ?" + ",published_house = ?, book_category = ?, borrower_name = ?, borrower_phone = ? " // 参数用?表示,相当于占位符 + "where id = ?"; // 预编译sql语句 preparedstatement psmt = con.preparestatement(sql); // 先对应sql语句,给sql语句传递参数 psmt.setstring(1, book.getisbn()); psmt.setstring(2, book.getbookname()); psmt.setfloat(3, book.getprice()); psmt.setstring(4, book.getauthor()); psmt.setstring(5, book.getpublishhouse()); psmt.setstring(6, book.getbookcategory()); if (book.getborrowername().equals("")) { psmt.setstring(7, null); } else { psmt.setstring(7, book.getborrowername()); } if (book.getborrowerphone().equals("")) { psmt.setstring(8, null); } else { psmt.setstring(8, book.getborrowerphone()); } psmt.setint(9, book.getid()); // 执行sql语句 psmt.execute(); } /** * 查询书籍信息 */ public list<book> query() throws exception{ connection con = dbutil.getconnection(); statement stmt = con.createstatement(); resultset rs = stmt.executequery("select " // isbn、书名、作者、图书价格、出版社 + "id, isbn, book_name, book_author, book_price, published_house, " // 分类号、借书人姓名、借书人电话 + "book_category, borrower_name, borrower_phone " + "from tb_books"); list<book> booklist = new arraylist<book>(); book book = null; // 如果对象中有数据,就会循环打印出来 while (rs.next()){ book = new book(); book.setid(rs.getint("id")); book.setisbn(rs.getstring("isbn")); book.setbookname(rs.getstring("book_name")); book.setauthor(rs.getstring("book_author")); book.setprice(rs.getfloat("book_price")); book.setpublishhouse(rs.getstring("published_house")); book.setbookcategory(rs.getstring("book_category")); book.setborrowername(rs.getstring("borrower_name")); book.setborrowerphone(rs.getstring("borrower_phone")); booklist.add(book); } return booklist; } /** * 查询借阅信息 * * @return * booklist */ public list<book> borrowquery() throws exception{ connection con = dbutil.getconnection(); statement stmt = con.createstatement(); resultset rs = stmt.executequery("" // id、书名、借书人姓名、借书人电话 + "select id, book_name, borrower_name, borrower_phone " + "from tb_books " + "where borrower_name is not null" ); list<book> booklist = new arraylist<book>(); book book = null; // 如果对象中有数据,就会循环打印出来 while (rs.next()){ book = new book(); book.setid(rs.getint("id")); book.setbookname(rs.getstring("book_name")); book.setborrowername(rs.getstring("borrower_name")); book.setborrowerphone(rs.getstring("borrower_phone")); booklist.add(book); } return booklist; } /** * 更新图书信息,归还图书 */ public void returnbook(book book) throws sqlexception{ // 首先拿到数据库的连接 connection con=dbutil.getconnection(); string sql="update tb_books " // isbn、图书名称、作者、价格 + "set " // 借书人姓名、借书人电话 + "borrower_name = ?, borrower_phone = ? " // 参数用?表示,相当于占位符 + "where id = ?"; // 预编译sql语句 preparedstatement psmt = con.preparestatement(sql); // 先对应sql语句,给sql语句传递参数 psmt.setstring(1, book.getborrowername()); psmt.setstring(2, book.getborrowerphone()); psmt.setint(3, book.getid()); // 执行sql语句 psmt.execute(); } }
重点内容 :
jdbc进行简单的数据库增删改查
详细参考:https://www.cnblogs.com/qian123/p/5339164.html#_labeltop
model类(以book为例)
package pers.cyz.model; /** * 图书模型类,包含数据库图书表各对应的字段get、set方法 * * @author 1651200111 陈彦志 */ public class book { private int id; // isbn号 private string isbn; // 图书名称 private string bookname; // 图书价格 private float price; // 图书作者 private string author; // 出版社 private string publishedhouse; // 图书分类号 private string bookcategory; // 借书人姓名 private string borrowername; // 借书人电话 private string borrowerphone; /** * 获取id */ public int getid() { return id; } /** * 设置id */ public void setid(int id) { id = id; } /** * 获取isbn */ public string getisbn() { return isbn; } /** * 设置isbn */ public void setisbn(string isbn) { isbn = isbn; } /** * 获取图书名称 */ public string getbookname() { return bookname; } /** * 设置图书名称 */ public void setbookname(string bookname) { this.bookname = bookname; } /** * 获取图书价格 */ public float getprice() { return price; } /** * 设置图书价格 */ public void setprice(float price) { this.price = price; } /** * 获取图书作者 */ public string getauthor() { return author; } /** * 设置图书作者 */ public void setauthor(string author) { this.author = author; } /** * 获取出版社 */ public string getpublishhouse() { return publishedhouse; } /** * 设置出版社 */ public void setpublishhouse(string publishedhouse) { this.publishedhouse = publishedhouse; } /** * 获取图书分类信息 */ public string getbookcategory() { return bookcategory; } /** * 设置图书分类信息 */ public void setbookcategory(string bookcategory) { this.bookcategory = bookcategory; } /** * 获取借书人姓名 */ public string getborrowername() { return borrowername; } /** * 设置借书人姓名 */ public void setborrowername(string borrowername) { this.borrowername = borrowername; } /** * 获取借书人电话 */ public string getborrowerphone() { return borrowerphone; } /** * 设置借书人电话 */ public void setborrowerphone(string borrowerphone) { this.borrowerphone = borrowerphone; } }
重点内容 :
主要就是数据库对应表中各对应的字段get、set方法
eclipse技巧:
shift + alt + s -> generate getters and setters -> select all -> generate 自动生成set、get方法
controller类(以bookaction为例)
package pers.cyz.controller; import java.util.list; import javax.swing.jtable; import javax.swing.jtextfield; import pers.cyz.dao.bookdao; import pers.cyz.model.book; /** * 图书信息行为控制类,包含增加图书、删除图书 * 、 修改图书、和初始化个人书库管理窗体表格 * * @author 1651200111 陈彦志 */ public class bookaction { /** * 初始化窗体表格 * @return * results */ @suppresswarnings("rawtypes") public object[][] initializtable(string[] columnnames) throws exception{ bookdao bookdao = new bookdao(); list list = bookdao.query(); object[][] results = new object[list.size()][columnnames.length]; for(int i = 0; i < list.size(); i++) { book book = (book)list.get(i); results[i][0] = book.getid(); results[i][1] = book.getbookname(); results[i][2] = book.getauthor(); results[i][3] = book.getprice(); results[i][4] = book.getisbn(); results[i][5] = book.getpublishhouse(); results[i][6] = book.getbookcategory(); string borrowername = book.getborrowername(); if (borrowername == null) { borrowername = ""; results[i][7] = borrowername; } else { results[i][7] = borrowername; } string borrowerphone = book.getborrowerphone(); if (borrowerphone == null) { borrowerphone = ""; results[i][8] = borrowerphone; } else { results[i][8] = borrowerphone; } } return results; } /** * 添加图书信息 */ public void addbookinformation (jtextfield textfieldisbn, jtextfield textfieldname ,jtextfield textfieldprice, jtextfield textfieldauthor, jtextfield textfieldpublishedhouse , jtextfield textfieldbookcategory, jtextfield textfieldborrowname , jtextfield textfieldborrowphone) throws exception { bookdao bookdao=new bookdao(); book book=new book(); book.setisbn(textfieldisbn.gettext()); book.setbookname(textfieldname.gettext()); float price = float.parsefloat(textfieldprice.gettext()); book.setprice(price); book.setauthor(textfieldauthor.gettext()); book.setpublishhouse(textfieldpublishedhouse.gettext()); book.setbookcategory(textfieldbookcategory.gettext()); if (textfieldborrowname.gettext() == null ||textfieldborrowname.gettext() == "" ) { book.setborrowername(null); } else { book.setborrowername(textfieldborrowname.gettext()); } if (textfieldborrowphone.gettext() == null || textfieldborrowphone.gettext() == "") { book.setborrowerphone(null); } else { book.setborrowerphone(textfieldborrowphone.gettext()); } //添加图书 bookdao.addbook(book); } /** * 删除图书信息 */ public void delbookinformation (jtable table) throws exception { int selrow = table.getselectedrow(); int id = integer.parseint(table.getvalueat(selrow, 0).tostring()); bookdao bookdao=new bookdao(); book book=new book(); book.setid(id); // 删除图书信息 bookdao.delbook(id); } /** * 修改图书信息 */ public void changebookinformation (jtextfield textfieldisbn, jtextfield textfieldname ,jtextfield textfieldprice, jtextfield textfieldauthor, jtextfield textfieldpublishedhouse , jtextfield textfieldbookcategory, jtextfield textfieldborrowername , jtextfield textfieldborrowerphone, jtable table) throws exception{ bookdao bookdao=new bookdao(); book book=new book(); int selrow = table.getselectedrow(); int id = integer.parseint(table.getvalueat(selrow, 0).tostring()); book.setid(id); book.setisbn(textfieldisbn.gettext()); book.setbookname(textfieldname.gettext()); book.setauthor(textfieldauthor.gettext()); float price = float.parsefloat(textfieldprice.gettext()); book.setprice(price); book.setpublishhouse(textfieldpublishedhouse.gettext()); book.setbookcategory(textfieldbookcategory.gettext()); book.setborrowername(textfieldborrowername.gettext()); book.setborrowerphone(textfieldborrowerphone.gettext()); //修改图书 bookdao.changebook(book); } }
util类(以dbutil为例)
package pers.cyz.util; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; /** * 连接数据库类,包含一个对外提供获取数据库连接的方法 * * @author 1651200111 陈彦志 */ public class dbutil { // 数据库连接路径 private static final string url = "jdbc:mysql://127.0.0.1:3306/db_books?" + "useunicode = true & servertimezone = gmt" // mysql在高版本需要指明是否进行ssl连接 + "& characterencoding = utf8 & usessl = false"; private static final string name = "root"; private static final string password = "root"; private static connection conn = null; // 静态代码块(将加载驱动、连接数据库放入静态块中) static{ try { // 加载驱动程序 class.forname("com.mysql.cj.jdbc.driver"); // 获取数据库的连接 conn = drivermanager.getconnection(url, name, password); } catch (classnotfoundexception e) { e.printstacktrace(); } catch (sqlexception e) { e.printstacktrace(); } } // 对外提供一个方法来获取数据库连接 public static connection getconnection(){ return conn; } }
util类(以backgroundimage为例)
package pers.cyz.util; import java.awt.container; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; /** * 设置背景图片类 * * @author 1651200111 陈彦志 */ public class backgroundimage { public backgroundimage(jframe frame,container container,string imagename) { // 限定加载图片路径 imageicon icon= new imageicon("res/" + imagename); final jlabel labelbackground = new jlabel(); imageicon iconbookmanagesystembackground = icon; labelbackground.seticon(iconbookmanagesystembackground); // 设置label的大小 labelbackground.setbounds(0,0,iconbookmanagesystembackground.geticonwidth() ,iconbookmanagesystembackground.geticonheight()); // 将背景图片标签放入桌面面板的最底层 frame.getlayeredpane().add(labelbackground,new integer(integer.min_value)); // 将容器转换为面板设置为透明 jpanel panel = (jpanel)container; panel.setopaque(false); } }
重点内容 :
将图片标签放在窗体底层面板,然后将窗体转化为容器,将容器面板设为透明,背景图片就设置好了,之后就可以直接在该容器中添加组件
将所有两个或两个以上类需要用到的代码段全部封装到了公共类。
整体按照mvc三层架构组织
参考文章:https://www.cnblogs.com/qian123/p/5339164.html#_labeltop
参考文章:https://blog.csdn.net/acm_hmj/article/details/52830920
源码打包下载地址:
到此这篇关于java+mysql 图书管理系统的文章就介绍到这了,更多相关java 图书管理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!