欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

图书管理系统java版

程序员文章站 2024-03-12 16:38:32
本文的目的就是通过图书管理系统掌握数据库编程技术,能正确连接数据库,能对数据库中信息进行查询、插入、删除、修改。 内容:在数据库中创建一张书目信息表,包括书名、作者、出版...

本文的目的就是通过图书管理系统掌握数据库编程技术,能正确连接数据库,能对数据库中信息进行查询、插入、删除、修改。
内容:在数据库中创建一张书目信息表,包括书名、作者、出版社、出版日期、书号、价格字段。设计一个gui界面进行书目管理。在该界面上有四个选项卡,分别是查询、插入、删除、修改。点击查询选项卡,出现的界面上有书名、作者、出版社、书号四个文本框,一个按钮和一个只读文本区。文本框内容可以为空,输入相应的查询信息后(例如根据书名查询可以仅输入书名),点击界面上的“查询”按钮,可以在界面下方的文本区中显示出符合条件的书目详细信息。点击插入选项卡,出现的界面上有书名、作者、出版社、出版日期、书号、价格文本框,一个按钮。在文本框中输入信息后,点击“插入”按钮,该书目信息插入数据库表中。点击删除选项卡,出现的界面上有书名文本框和一个按钮,输入书名后点击“删除”按钮,该书目信息从数据库表中删除。点击修改选项卡,出现的界面上有书名、作者、出版社、出版日期、书号、价格文本框,一个按钮。输入的书名必须是已存在的,否则会弹出消息框显示出错信息。输入信息后,点击“修改”按钮,数据库表中的相应书目信息被修改为新值。
源码:

bookinfo.java

 * 项目名称:图书管理系统 
 * 版本: 1.0 
 * 创建者: 张俊强 
 * 创建时间: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{ 
 //主角面上的控件 
 private jlabel 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 bookinfo=new bookinfo(); 
  bookinfo.setdefaultcloseoperation(jframe.exit_on_close); 
  bookinfo.setbounds(350, 150, 600, 400); 
  bookinfo.setvisible(true); 
//  bookinfo.importsql();//导出数据 
  bookinfo.setminwindowlayout();//设置数据 
 } 
 public bookinfo() throws sqlexception{ 
  //创建主界面上的控件 
  inputlabel=new jlabel("请输入书名:"); 
  inputtext=new jtextfield(10); 
  searchbut=new jbutton("查询"); 
  booktablemodel=new booktablemodel(); 
   
  booktable=new jtable(booktablemodel); 
  bookscroll=new jscrollpane(booktable); 
   
  addbut=new jbutton("添加"); 
  modifybut=new jbutton("修改"); 
  deletebut=new jbutton("删除"); 
  refreshbut=new jbutton("刷新"); 
  searchbut.addactionlistener(this); 
  addbut.addactionlistener(this); 
  refreshbut.addactionlistener(this); 
  modifybut.addactionlistener(this); 
  deletebut.addactionlistener(this); 
 
 } 
 
 void setminwindowlayout(){ 
  //主界面布局 
  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,"输入不能为空", "提示",joptionpane.plain_message); 
   } 
  } 
  else if(e.getsource()==addbut){ 
   @suppresswarnings("unused") 
   addbookdialog addwin=new addbookdialog(this,"添加图书",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,"未选中", "提示",joptionpane.plain_message); 
   } 
   else{ 
    //system.out.print(bookname); 
    int n = joptionpane.showconfirmdialog(null, "确认删除吗?", "确认删除框", 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,"删除成功", "提示",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,"未选中", "提示",joptionpane.plain_message); 
   } 
   else{ 
    @suppresswarnings("unused") 
    modifybook modifywin=new modifybook(this,"修改信息",true,booktablemodel,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.*; 
 
/* 
 * 图书表模型 
 * */ 
import javax.swing.table.*; 
@suppresswarnings("serial") 
public class booktablemodel extends abstracttablemodel{ 
 //表的元素 
 private vector<vector<string>> rowdata; 
 private vector<string> colname; 
 // 数据库 
 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("书号"); 
  getcolname().add("书名"); 
  getcolname().add("作者"); 
  getcolname().add("出版社"); 
  getcolname().add("出版时间"); 
  getcolname().add("价格"); 
  /* 
   * 数据库的导入 
   * */ 
  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") 
  boolean 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() {//得到列数 
  // todo auto-generated method stub 
  return this.colname.size(); 
 } 
 
 @override 
 public int getrowcount() {//得到行数 
  // todo auto-generated method stub 
  return this.rowdata.size(); 
 } 
 
 @override 
 public object getvalueat(int row, int col) {//得到某行某列的数据 
  // todo auto-generated method stub 
  return (this.rowdata.get(row)).get(col); 
 } 
 
 @override 
 public string getcolumnname(int column) { 
  // todo auto-generated method stub 
  return this.colname.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 booknumlabel; 
 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){ 
  //父窗口,窗口名,是否是模式窗口 
  super(owner,title,model); 
  booknumlabel=new jlabel("书 号:"); 
  booknamelabel=new jlabel("书 名:"); 
  bookwriterlabel=new jlabel("作 者:"); 
  bookpublishlabel=new jlabel("出版社:"); 
  bookpricelabel=new jlabel("价 格:"); 
  booktimelabel=new jlabel("出版时间:"); 
   
  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("确认"); 
  cancelbut=new jbutton("取消"); 
  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("输入失败"); 
    joptionpane.showmessagedialog(this,"输入不能有空", "提示",joptionpane.plain_message); 
   } 
   else{ 
    //system.out.println("输入成功"); 
    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,"添加成功", "提示",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 booknumlabel; 
 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("书 号:"); 
  booknamelabel=new jlabel("书 名:"); 
  bookwriterlabel=new jlabel("作 者:"); 
  bookpublishlabel=new jlabel("出版社:"); 
  bookpricelabel=new jlabel("价 格:"); 
  booktimelabel=new jlabel("出版时间:"); 
   
  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("确认修改"); 
  cancelbut=new jbutton("取消"); 
  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("输入失败"); 
    joptionpane.showmessagedialog(this,"修改不能有空", "提示",joptionpane.plain_message); 
   } 
   else{ 
    int n = joptionpane.showconfirmdialog(null, "确认修改吗?", "确认修改框", 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,"修改成功", "提示",joptionpane.plain_message); 
     this.setvisible(false); 
    } else if (n == joptionpane.no_option) { 
     return; 
    } 
   } 
  } 
  if(e.getsource()==cancelbut){ 
   this.setvisible(false); 
  } 
 }  
} 

程序运行结果:
主界面:

图书管理系统java版

查询界面:

图书管理系统java版

添加图书界面:

图书管理系统java版

图书管理系统java版

修改界面:

图书管理系统java版

图书管理系统java版

删除操作:

图书管理系统java版

图书管理系统java版

数据库界面:

图书管理系统java版

关于管理系统的更多内容请点击《管理系统专题》进行学习

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。