图书信息管理java实现代码
程序员文章站
2023-12-15 09:24:58
图书信息管理与读者信息管理相似,共包括两部分,图书添加和图书查询与修改,图书信息添加要求正确输入图书的八项信息,并且isbn编号唯一,实现过程中要将isbn作为主键。下面看...
图书信息管理与读者信息管理相似,共包括两部分,图书添加和图书查询与修改,图书信息添加要求正确输入图书的八项信息,并且isbn编号唯一,实现过程中要将isbn作为主键。下面看实现过程:
数据库中书籍:
添加书籍过程:
数据库内容增加:
图书的查询与修改:查询过程可以根据不同的条件查询,如isbn,作者,出版社,图书名称等,默认将所有图书全部显示在列表中,输入查询条件后再表格中显示查询结果。
本模块用到标签面板,可以很方便的查询所需要的信息。修改过程输入isbn编号后按回车键将在下方显示所有的图书信息,然后修改图书信息后所有内容就会更新。
图书查询:
图书信息修改:
添加图书源代码如下:addbook.java
package pdsu.bbm.view; import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.font; import java.awt.image; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.text.parseexception; import java.text.simpledateformat; import java.util.date; import java.util.iterator; import java.util.list; import javax.swing.buttongroup; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield; import pdsu.bbm.dao.bookdao; import pdsu.bbm.dao.booktypedao; import pdsu.bbm.model.book; import pdsu.bbm.model.booktype; public class addbook extends jframe implements actionlistener { private jpanel contentpane; private jlabel label_1; private jlabel lbll; private jlabel label_2; private jlabel label_3; private jlabel label_4; private jlabel label_5; jpanel centerppanel; jbutton button1, button2, button3; private jcombobox combobox_1; image icon = new imageicon("image/icon.png").getimage(); private jtextfield isbnfiled; private jtextfield publishfield; private jtextfield publishdatefield; private jtextfield publishtime; private jtextfield unitpricefield; private jtextfield booknamefield; private jtextfield authorfiled; public static void main(string[] args) { new addbook(); } public addbook() { settitle("添加图书信息"); setsize(555, 333); seticonimage(icon); setlocationrelativeto(null); settitle("添加图书信息"); setsize(555, 334); setlocationrelativeto(null); setvisible(true); centerppanel = new jpanel(); centerppanel.setlayout(null); jlabel label1 = new jlabel("isbn:"); label1.setfont(new font("宋体", font.plain, 20)); label1.setbounds(71, 26, 95, 42); centerppanel.add(label1); isbnfiled = new jtextfield(); isbnfiled.setbounds(120, 30, 110, 30); centerppanel.add(isbnfiled); isbnfiled.setcolumns(10); jlabel label = new jlabel("类 别:"); label.setfont(new font("宋体", font.plain, 20)); label.setbounds(306, 30, 116, 35); centerppanel.add(label); label_1 = new jlabel("书 名:"); label_1.setfont(new font("宋体", font.plain, 20)); label_1.setbounds(50, 75, 154, 50); centerppanel.add(label_1); lbll = new jlabel("作 者:"); lbll.setfont(new font("宋体", font.plain, 20)); lbll.setbounds(306, 75, 137, 50); centerppanel.add(lbll); label_2 = new jlabel("出版社:"); label_2.setfont(new font("宋体", font.plain, 20)); label_2.setbounds(50, 130, 154, 50); centerppanel.add(label_2); label_3 = new jlabel("出版日期:"); label_3.setfont(new font("宋体", font.plain, 20)); label_3.setbounds(285, 135, 137, 50); centerppanel.add(label_3); publishfield = new jtextfield(); publishfield.setcolumns(10); publishfield.setbounds(120, 143, 110, 30); centerppanel.add(publishfield); publishdatefield = new jtextfield(); publishdatefield.setcolumns(10); publishdatefield.setbounds(380, 143, 120, 30); centerppanel.add(publishdatefield); label_4 = new jlabel("印刷次数:"); label_4.setfont(new font("宋体", font.plain, 20)); label_4.setbounds(28, 190, 154, 50); centerppanel.add(label_4); publishtime = new jtextfield(); publishtime.setcolumns(10); publishtime.setbounds(120, 203, 110, 30); centerppanel.add(publishtime); label_5 = new jlabel("单 价:"); label_5.setfont(new font("宋体", font.plain, 20)); label_5.setbounds(305, 194, 84, 42); centerppanel.add(label_5); unitpricefield = new jtextfield(); unitpricefield.setcolumns(10); unitpricefield.setbounds(380, 203, 120, 30); centerppanel.add(unitpricefield); getcontentpane().add(centerppanel, borderlayout.center); booknamefield = new jtextfield(); booknamefield.setcolumns(10); booknamefield.setbounds(120, 86, 110, 30); centerppanel.add(booknamefield); authorfiled = new jtextfield(); authorfiled.setcolumns(10); authorfiled.setbounds(380, 86, 120, 30); centerppanel.add(authorfiled); list<booktype> rs = booktypedao.selectbooktype(); iterator<booktype> iterator = rs.iterator(); string[] alltypename = new string[rs.size()]; int i = 0; while (iterator.hasnext()) { string typename = iterator.next().gettypename(); alltypename[i] = typename; i++; } combobox_1 = new jcombobox(alltypename); combobox_1.setbounds(380, 30, 120, 30); combobox_1.setfont(new font("宋体", font.plain, 16)); centerppanel.add(combobox_1); buttongroup group = new buttongroup(); jpanel panel2 = new jpanel(); panel2.setlayout(new flowlayout()); button1 = new jbutton("添加"); button1.setfont(new font("宋体", font.plain, 20)); button2 = new jbutton("关闭"); button2.setfont(new font("宋体", font.plain, 20)); button3 = new jbutton("重置"); button3.setfont(new font("宋体", font.plain, 20)); panel2.add(button1); panel2.add(button3); panel2.add(button2); button1.addactionlistener(this); button2.addactionlistener(this); button3.addactionlistener(this); getcontentpane().add(panel2, borderlayout.south); setvisible(true); } @suppresswarnings("unused") public void actionperformed(actionevent e) { if (e.getsource() == button1) { string isbn = isbnfiled.gettext().tostring(); string bookname = booknamefield.gettext(); string author = authorfiled.gettext(); string selecttype = combobox_1.getselecteditem().tostring(); string publish = publishfield.gettext(); string publishdate = publishdatefield.gettext(); string time = publishtime.gettext().trim(); string price = unitpricefield.gettext().trim(); if (isbn.equals("") || bookname.equals("") || author.equals("") || selecttype.equals("") || publish.equals("") || publishdate.equals("") || time.equals("") || price.equals("")) { joptionpane.showmessagedialog(this, "请输入完整信息"); return; } else { booktype bt = new booktype(); book book = new book(); book.setisbn(isbn); book.setbookname(bookname); book.setauthor(author); bt.settypename(selecttype); int m = 0; list<booktype> list = booktypedao .selectidbytypename(selecttype); iterator<booktype> ite = list.iterator(); while (ite.hasnext()) { m = ite.next().getid(); } book.settypeid(m + ""); book.setpublish(publish); try { book.setpublishtime(integer.parseint(time)); book.setunitprice(integer.parseint(price)); } catch (numberformatexception e1) { e1.printstacktrace(); } simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); date regdate = null; try { regdate =sdf.parse(publishdate); } catch (parseexception e1) { joptionpane.showmessagedialog(this, "请输入正确的日期"); publishdatefield.settext(""); } book.setpublishdate(regdate); if(regdate==null) return; if (bookdao.selectbookbyisbn(isbn) != null) { joptionpane.showmessagedialog(this, "该isbn编号已存在"); return; } else if (bookdao.insertbook(book) != 0) { joptionpane.showmessagedialog(this, "添加成功"); } else joptionpane.showmessagedialog(this, "添加失败"); } } if (e.getsource() == button3) { isbnfiled.settext(""); booknamefield.settext(""); authorfiled.settext(""); publishfield.settext(""); publishdatefield.settext(""); publishtime.settext(""); unitpricefield.settext(""); } if (e.getsource() == button2) { dispose(); new mainwindow(); } } }
图书查询与修改源代码:bookselectandmodify.java
package pdsu.bbm.view; import java.awt.borderlayout; import java.awt.dimension; import java.awt.font; import java.awt.gridlayout; import java.awt.image; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; import java.util.iterator; import java.util.list; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtabbedpane; import javax.swing.jtable; import javax.swing.jtextfield; import javax.swing.swingconstants; import javax.swing.table.tablemodel; import pdsu.bbm.dao.bookdao; import pdsu.bbm.dao.booktypedao; import pdsu.bbm.model.book; import pdsu.bbm.model.booktype; public class bookselectandmodify extends jframe implements actionlistener { // 设置序列号 private static final long serialversionuid = 1l; private jtabbedpane jtabbedpane; // 标签面板 // selectjp查询面板 select_conditionjp下拉列表面板 select_resultjp结果按钮面板updatejp修改面板 // updatejp修改面板,bookjp中间面板 buttonjp2按钮面板 private jpanel selectjp, select_conditionjp, select_resultjp, buttonjp1, bookjp, updatejp, buttonjp2;// private jtextfield selectjtf, isbnjtf, booknamejtf, authorjtf, printtimejtf, publishjtf, publishdatejtf, unitpricejtf; private jlabel isbnjl, booknamejl, authorjl, categoryjl, printtimejl, publishjl, publishdatejl, unitpricejl; // 重点! private jtable jtable;// 定义表格 private jcombobox choicejcb, booktypejcb; private jscrollpane jscrollpane; private jbutton selectjb, exitjb, updatejb, resetjb;// 查询按钮,退出按钮,修改按钮,关闭按钮 private tablemodel getselect; private string[] title = { "isbn", "图书序列", "图书名称", "作者", " 出版社 ", "出版日期", "印刷次数", "单价" }; image icon = new imageicon("image/icon.png").getimage(); public bookselectandmodify() { super(); seticonimage(icon); settitle("图书查询与修改"); setbounds(100, 100, 555, 400); setlocationrelativeto(null); // jtabbedpane选项卡 jtabbedpane = new jtabbedpane(); add(jtabbedpane); selectjp = new jpanel(); selectjp.setlayout(new borderlayout()); jtabbedpane.add("图书信息查询", selectjp); // 查询条件面板 select_conditionjp = new jpanel(); choicejcb = new jcombobox(); string array[] = { "isbn", "图书名称", "图书序列", "作者", "出版社","所有" };// 设置为一维数组 for (int i = 0; i < array.length; i++) { choicejcb.additem(array[i]); } select_conditionjp.add(choicejcb); selectjtf = new jtextfield(20); select_conditionjp.add(selectjtf); selectjp.add(select_conditionjp, borderlayout.north); // 查询结果面板 select_resultjp = new jpanel(); bookdao dao = new bookdao(); list<book> list = dao.selectbook(); jtable = new jtable(getselect(list), title); // !设置表格大小不变 jtable.setautoresizemode(jtable.auto_resize_off); // !设置列宽 jtable.getcolumnmodel().getcolumn(4).setpreferredwidth(175); jscrollpane = new jscrollpane(jtable);// 把表格加入滚动面板 // 显示滚动面板边框 jscrollpane.setpreferredsize(new dimension(450, 170)); select_resultjp.add(jscrollpane); selectjp.add(select_resultjp, borderlayout.center); // 查询按钮面板 buttonjp1 = new jpanel(); selectjb = new jbutton("查询"); selectjb.setfont(new font("宋体", font.plain, 20)); selectjb.addactionlistener(this); buttonjp1.add(selectjb); exitjb = new jbutton("退出"); exitjb.setfont(new font("宋体", font.plain, 20)); exitjb.addactionlistener(this); buttonjp1.add(exitjb); selectjp.add(buttonjp1, borderlayout.south); // 信息修改页面 updatejp = new jpanel(); updatejp.setlayout(new borderlayout()); jtabbedpane.addtab("图书信息修改", updatejp); bookjp = new jpanel(); final gridlayout gridlayout = new gridlayout(8, 2); // 设置边框间的距离 gridlayout.setvgap(8); gridlayout.sethgap(8); bookjp.setlayout(gridlayout); isbnjl = new jlabel("isbn:"); isbnjl.setfont(new font("宋体",font.plain,20)); isbnjl.sethorizontalalignment(swingconstants.center); bookjp.add(isbnjl); isbnjtf = new jtextfield(20); isbnjtf.addactionlistener(this); bookjp.add(isbnjtf); categoryjl = new jlabel("类 别:"); categoryjl.setfont(new font("宋体",font.plain,20)); categoryjl.sethorizontalalignment(swingconstants.center); bookjp.add(categoryjl); // 下拉列表 list<booktype> list1 = booktypedao.selectbooktype(); iterator<booktype> it = list1.iterator(); string ty[] = new string[list1.size()]; int i = 0; booktypejcb = new jcombobox(); while (it.hasnext()) { ty[i] = it.next().gettypename(); booktypejcb.additem(ty[i]); i++; } bookjp.add(booktypejcb); booknamejl = new jlabel("书 名:"); booknamejl.setfont(new font("宋体",font.plain,20)); booknamejl.sethorizontalalignment(swingconstants.center); bookjp.add(booknamejl); booknamejtf = new jtextfield(); booknamejtf.setcolumns(20); bookjp.add(booknamejtf); authorjl = new jlabel("作 者:"); authorjl.setfont(new font("宋体",font.plain,20)); authorjl.sethorizontalalignment(swingconstants.center); bookjp.add(authorjl); authorjtf = new jtextfield(); authorjtf.setcolumns(20); bookjp.add(authorjtf); publishjl = new jlabel("出版社:"); publishjl.setfont(new font("宋体",font.plain,20)); publishjl.sethorizontalalignment(swingconstants.center); bookjp.add(publishjl); publishjtf = new jtextfield(); bookjp.add(publishjtf); publishdatejl = new jlabel("出版日期:"); publishdatejl.setfont(new font("宋体",font.plain,20)); publishdatejl.sethorizontalalignment(swingconstants.center); bookjp.add(publishdatejl); publishdatejtf = new jtextfield(); publishdatejtf.sethorizontalalignment(swingconstants.left); bookjp.add(publishdatejtf); printtimejl = new jlabel("印刷次数:"); printtimejl.setfont(new font("宋体",font.plain,20)); printtimejl.sethorizontalalignment(swingconstants.center); bookjp.add(printtimejl); printtimejtf = new jtextfield(); bookjp.add(printtimejtf); unitpricejl = new jlabel("单 价:"); unitpricejl.setfont(new font("宋体",font.plain,20)); unitpricejl.sethorizontalalignment(swingconstants.center); bookjp.add(unitpricejl); unitpricejtf = new jtextfield(); bookjp.add(unitpricejtf); // 按钮面板 // 按钮面板设计 buttonjp2 = new jpanel(); updatejb = new jbutton("修改"); updatejb.setfont(new font("宋体", font.plain, 20)); updatejb.addactionlistener(this); resetjb = new jbutton("关闭"); resetjb.setfont(new font("宋体", font.plain, 20)); resetjb.addactionlistener(this); buttonjp2.add(updatejb); buttonjp2.add(resetjb); updatejp.add(bookjp, borderlayout.center); updatejp.add(buttonjp2, borderlayout.south); setvisible(true); setdefaultcloseoperation(jframe.exit_on_close); } // 将查询表格加入面板 private object[][] getselect(list<book> list) { // todo auto-generated method stub object[][] objects = new object[list.size()][title.length]; for (int i = 0; i < list.size(); i++) { book book = list.get(i); objects[i][0] = book.getisbn(); objects[i][1] = book.gettypeid();// 图书序列 objects[i][2] = book.getbookname();// 图书名称 objects[i][3] = book.getauthor();// 图书作者 objects[i][4] = book.getpublish();// 出版社 objects[i][5] = book.getpublishdate();// 出版日期 objects[i][6] = book.getpublishtime();// 印刷次数 objects[i][7] = book.getunitprice();// 单价 } return objects; } public void actionperformed(actionevent e) { // todo auto-generated method stub if(e.getsource()==isbnjtf){ string isbn=isbnjtf.gettext(); string typename=null; string id=bookdao.selectbookbyisbn(isbn).gettypeid(); typename=booktypedao.selectbyid(id); system.out.println(typename); booktypejcb.setselecteditem(typename); booknamejtf.settext(bookdao.selectbookbyisbn(isbn).getbookname()); authorjtf.settext(bookdao.selectbookbyisbn(isbn).getauthor()); publishjtf.settext(bookdao.selectbookbyisbn(isbn).getpublish()); publishdatejtf.settext(bookdao.selectbookbyisbn(isbn).getpublishdate()+""); printtimejtf.settext(bookdao.selectbookbyisbn(isbn).getpublishtime()+""); unitpricejtf.settext(bookdao.selectbookbyisbn(isbn).getunitprice()+""); } if (e.getsource() == selectjb) {// 按照isbn编码查找 int r = choicejcb.getselectedindex(); if (r == 0) { string name = selectjtf.gettext().trim(); // 强制转换为线性表类型 list<book> list = new arraylist<book>(); book book = bookdao.selectbookbyisbn(name); if (book == null) { joptionpane.showmessagedialog(this, "该编码不存在!"); } else { list.add(book); object[][] data = getselect(list); jtable = new jtable(data, title); jtable.getcolumnmodel().getcolumn(4).setpreferredwidth(175); jtable.setautoresizemode(jtable.auto_resize_off); jscrollpane.setviewportview(jtable); } } if (r == 1) {// 根据图书名称查询信息 string name = selectjtf.gettext().tostring(); list<book> list = bookdao.selectbookbyname(name); // 判断线性表是否为空 if (list == null || list.size() == 0) { joptionpane.showmessagedialog(this, "该图书不存在!"); } else { object[][] data = getselect(list); jtable = new jtable(data, title); tianjia(); } } if (r == 2) {// 根据图书序列查询信息 string name = selectjtf.gettext().tostring(); list<book> list = bookdao.selectbookbytype(name); // 判断线性表是否为空 if (list == null || list.size() == 0) { joptionpane.showmessagedialog(this, "该图书不存在!"); } else { object[][] data = getselect(list); jtable = new jtable(data, title); tianjia(); } } if (r == 3) { // 根据作者进行查找 string name = selectjtf.gettext().tostring(); // 根据作者进行查找 list<book> list = bookdao.selectbookbyauthor(name); // 判断线性表是否为空 if (list == null || list.size() == 0) { joptionpane.showmessagedialog(this, "该图书不存在!"); } else { object[][] data = getselect(list); jtable = new jtable(data, title); tianjia(); } } if (r == 4) {// 根据出版社进行查找 string name = selectjtf.gettext().tostring(); list<book> list = bookdao.selectbookbypublish(name); if (list == null || list.size() == 0) { joptionpane.showmessagedialog(this, "该图书不存在!"); } else { object[][] data = getselect(list); jtable = new jtable(data, title); tianjia(); } } if(r==5){ list<book> list =bookdao.selectbook(); } } if (e.getsource() == updatejb) { string isbn = isbnjtf.gettext().trim(); string typename = ((string) booktypejcb.getselecteditem()).trim(); string id = booktypedao.selectbytypename(typename); string bookname = booknamejtf.gettext(); string author = authorjtf.gettext(); string publish = publishjtf.gettext(); string publishdate = publishdatejtf.gettext(); simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); date regdate = null; try { regdate = sdf.parse(publishdate); } catch (exception e2) { e2.printstacktrace(); joptionpane.showmessagedialog(this, "请输入正确的日期格式!"); } if (regdate == null) return; int publishtime = integer.parseint(printtimejtf.gettext()); int unitprice = integer.parseint(unitpricejtf.gettext()); // 封装对象 book book = new book(); book.setisbn(isbn); book.settypeid(id); book.setbookname(bookname); book.setauthor(author); book.setpublish(publish); book.setpublishdate(regdate); book.setpublishtime(publishtime); book.setunitprice(unitprice); book.settypename(typename); int result = bookdao.update(book); list<book> list2 = new arraylist<book>(); list2.add(book); // system.out.println(list2); if (result == 1) { joptionpane.showmessagedialog(this, "修改成功!"); bookdao bk=new bookdao(); list<book> list =bk.selectbook(); jtable =new jtable(getselect(list),title); tianjia(); } else { joptionpane.showmessagedialog(this, "修改失败!"); } } if (e.getsource() == resetjb) { dispose(); new mainwindow(); } if (e.getsource() == exitjb) { dispose(); new mainwindow(); } } private void tianjia() { jtable.getcolumnmodel().getcolumn(4).setpreferredwidth(175); jtable.setautoresizemode(jtable.auto_resize_off); jscrollpane.setviewportview(jtable); } public static void main(string[] args) { new bookselectandmodify(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。