java课程设计----图书馆管理系统

更新时间:2023-07-24 10:04:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

java课程设计----图书馆管理系统

图书馆管理系统课程设计报告

1目录

引言----------------------------------------------------------------2

正文----------------------------------------------------------------3

结论和建议----------------------------------------------------------4

致谢----------------------------------------------------------------5

参考文献------------------------------------------------------------5

附录------------------------------------------------------------5—20

java课程设计----图书馆管理系统

2 引 言

当今时代是飞速发展的信息时代。在各行各业中离不开信息处理,这正是计算机被广泛应用于信息管理系统的环境。计算机的最大好处在于利用它能够进行信息管理。使用计算机进行信息控制,不仅提高了工作效率,而且大大的提高了其安全性。尤其对于复杂的信息管理,计算机能够充分发挥它的优越性。计算机进行信息管理与信息管理系统的开发密切相关,系统的开发是系统管理的前提。图书管理系统就是为了管理好图书馆信息而设计的。

图书馆作为一种信息资源的集散地,图书和用户借阅资料繁多,包含很多的信息数据的管理,现今,有很多的图书馆都是初步开始使用,甚至尚未使用计算机进行信息管理。根据调查得知,他们以前对信息管理的主要方式是基于文本、表格等纸介质的手工处理,对于图书借阅情况(如借书天数、超过限定借书时间的天数)的统计和核实等往往采用对借书卡的人工检查进行,对借阅者的借阅权限、以及借阅天数等用人工计算、手抄进行。数据信息处理工作量大,容易出错;由于数据繁多,容易丢失,且不易查找。总的来说,缺乏系统,规范的信息管理手段。尽管有的图书馆有计算机,但是尚未用于信息管理,没有发挥它的效力,资源闲置比较突出,这就是管理信息系统的开发的基本环境。

数据处理手工操作,工作量大,出错率高,出错后不易更改。图书馆采取手工方式对图书借阅情况进行人工管理,由于信息比较多,图书借阅信息的管理工作混乱而又复杂;一般借阅情况是记录在借书证上,图书的数目和内容记录在文件中,图书馆的工作人员和管理员也只是当时对它比较清楚,时间一长,如再要进行查询,就得在众多的资料中翻阅、查找了,造成查询费时、费力。如要对很长时间以前的图书进行更改就更加困难了。

基于这些问题,有必要建立一个图书管理系统,使图书管理工作规范化,系统化,程序化,避免图书管理的随意性,提高信息处理的速度和准确性,能够及时、准确、有效的查询和修改图书情况。

java课程设计----图书馆管理系统

3正 文

3.1功能需求分析

(1)作为学生对图书管理系统的要求有:

1. 能按各种方式(比如书名、编号、作者)查询图书馆的藏书情况。 2. 能够方便地借阅图书、续借图书、归还图书。 3. 能够查询自己的基本资料、借阅图书情况。 4. 能够熟悉图书管理系统的使用。

(2)作为图书管理员,他们对图书管理系统的要求有: 1. 能方便的对图书进行录入登记,注销陈旧的书籍。

2. 能够方便地对新生进行登记,或注销已经毕业的学生信息(基本信息,借阅信息)。

3. 能够随时发布一些诸如各学院学生借阅图书超期情况、馆内藏书情况、借情况息,以便各学院能够随时获知本院学生的一些借书信息。

3.2系统用例分析

图书管理系统中包含书籍的采购、入库、编目、出借、归还、淘汰等功能,以及读者信息的管理。本系统对前期的工作进行了简化,删掉了书籍的采购、入库、编目等繁琐的工作,仅对系统主要部分进行分析。为了提高服务效果,图书馆系统实行开架阅览,并为读者提供客户端,读者可以查询到馆藏书目和个人在借图书的信息。这项功能也可以通过互联网实现。图书管理员通过系统记录图书的出借和归还,以及书目的维护和读者信息的维护。

3.3系统模块分析

3.3.1新书入库模块:

(1)新书入库界面包括图书相关的属性:编号、书名、作者、类别、出版日期、出版社、定价、数量、备注等。 3.3.2图书整理模块:

(1)图书整理界面包括:图书修改删除、图书类别编辑

图书修改删除界面除了包括图书的相关属性还包括图书修改功能。

图书类别编辑界面包括对类别编号、类别名称进行添加、删除、

修改的功模块。

3.3.3办借书卡模块:

办借书卡界面包括借书卡编号、持借书卡人的姓名、性别、组名、证件号。

java课程设计----图书馆管理系统

3.3.4查询模块:

(1)借阅信息查询:

根据查询方式如 按借书证号、按姓名等在文本框中输入相关内容即可

查询还可查询到期图书

(2)图书信息查询:

查询方式有:按书号、按书名、按作者、按类别、按出版社。在关键

字中输入相关内容,点击“开始查询”按钮,在DataList中列出查询结果。如果选中“模糊查询”可查询更多的相关信息。

(3)用户信息查询:

查询方式有:按借书证号、按姓名。在关键字框中输入借书证号或者读者姓名。

组用户管理模块:

(4)组用户管理界面:

操作方式包括:修改、删除、添加。

组用户信息包括:组编号、组名称、最长天数、最多本数。 3.3.5图书借还模块:

图书借阅界面包括图书编号、书名、类别、出版社、出版日期、借书证号等

归还图书界面包括图书的属性、归还者的信息。

4 总结

通过完成本次图书管理系统实践,使我系统的经历了一次JAVA软件开发的整个设计开发过程,把所学的知识综合的运用到这次实践中,重新复习了课本上讲到的知识,觉得有一种温故而知新的感觉,通过实践同时也收获了许多课本上没有的东西。

尤其是在开发图书管理系统的过程当中,也遇到了许多技术上或者其他方面的困难,但是也算经过自己的努力,基本上克服了这些困难。由于我的水平确实有限,加之也没有实际的项目经验,设计时间短,所以该系统还有许多不尽如人意的地方,比如功能较少,出错处理不够等多方面问题 ,距离真正实用的图书管理系统还有许多差距,但我相信,软件开发是一个不断完善的过程,我会在以后得学习当中继续完善的 。

在这次设计过程中,培养了我的自学能力,在遇到困难的时候,能够认真的对待,不放弃,无论是查看各种书籍还是到图书馆中查找各种资料,想尽一切办法将这个问题解决。我总结了许多编程经验,这将在以后的学习生活中有很大的帮助。

java课程设计----图书馆管理系统

5、致谢

感谢那些给我带来帮助的书籍和internet,还有周边的同学们。还要感谢我们的指导老师给我们这么多学生的文档认真批改,让我们知道在课程设计与实现的过程中存在的问题。

6、参考文献

[1] 王克宏、郝建文. Java技术教程 [M]. 北京:清华大学出版社,2002年 [2] 基于JAVA的学生信息管理系统 李伟 电子科技大学 【硕士】电子科技大学 2009-04-01 0 578

[3] J2EE框架及全文检索技术在信息管理系统中的应用 徐立峰 电子科技大学 【硕士】电子科技大学 2009-10-01

7、附录

附部分主要源代码:

import javax.swing.*; import java.awt.*;

import java.awt.event.*; import javax.swing.table.*; import java.sql.*; import edu.njust.cs.*;

public class SimpleBookManager{

public static void main(String []args){

SetFont.setFont(new Font("楷体", 0, 12)); Connection con=null; try{ }

if(con!=null){

JFrame f=new JFrame();

});

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){ }

System.exit(0);

con=SqlUtil.acquireConnection("127.0.0.1",

"1433","bookTest","admin","xyz");

}catch(Exception e){

System.out.println(e); System.exit(-1);

f.getContentPane().add(new BookManager(f,con));

java课程设计----图书馆管理系统

}

}

}

f.show();

class BookManager extends JPanel implements ActionListener{

private String [] columnNames={"图书编号","书名","定价","出版社"};

private Class []dataType={String.class,String.class,Double.class,String.class};

private CustomTableModel model=null; private JTable table=null; private Connection con=null;

private JButton btnAdd=new TextAndPicButton("image/addrow20.gif"," 增 加 ");

private JButton btnSearch=new TextAndPicButton("image/search20.gif"," 查 询 ");

private JButton btnDelete=new TextAndPicButton("image/deleterow20.gif"," 删 除 private JButton btnExit=new TextAndPicButton("image/exit20.gif"," 退 出 ");

private JFrame f=null;

public BookManager(JFrame f,Connection con){

this.f=f; this.con=con; //构建工具条

JToolBar toolBar=new JToolBar(); toolBar.add(btnAdd); toolBar.add(btnSearch); toolBar.add(btnDelete); toolBar.add(btnExit); //注册事件侦听器

btnAdd.addActionListener(this); btnSearch.addActionListener(this); btnDelete.addActionListener(this); btnExit.addActionListener(this); //构建表格

model=new CustomTableModel(0,columnNames.length,columnNames,dataType); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION ); //设定表格每列的宽度 setTableColumnWidth(0,100); setTableColumnWidth(1,200); setTableColumnWidth(2,100);

");

table = new JTable(model);

java课程设计----图书馆管理系统

}

//布局工具条及表格

this.setLayout(new BorderLayout()); this.add(toolBar,BorderLayout.NORTH);

this.add(new JScrollPane(table),BorderLayout.CENTER);

//设置表格指定列的宽度

public void setTableColumnWidth(int column,int width){ }

public void actionPerformed(ActionEvent e){ }

//增加一条记录到数据库 public void add(){

}

} else

JOptionPane.showMessageDialog(this,"新增时出错!",

"提示",RMATION_MESSAGE);

BookEditor be=new BookEditor(f,"增加",true); be.show();

//只有点击了信息对话框中的确定按钮,才增加记录 if(be.getActionCode()==be.OK){

if(SqlUtil.addRowToDB(con,insertSql,lineForDBAdd)) { //成功写入数据库的记录,追加到表格的末行显示

Object []lineForTable=

String insertSql="insert into bookInfo VALUES (?,?,?,?)"; Object []lineForDBAdd={be.getID(),be.getBookName(),

be.getBookPrice(),be.getBookPress()};

Object s=e.getSource(); if(s==this.btnAdd) add(); else if(s==this.btnSearch)

search();

else if(s==this.btnDelete) delete(); else if(s==this.btnExit) System.exit(0); if(column<columnNames.length&&column>=0){ }

TableColumn col=table.getColumn(columnNames[column]); col.setPreferredWidth(width);

SqlUtil.getLineForTableFromLineForDB(lineForDBAdd,dataType);

model.insertRow(model.getRowCount(),lineForTable); //将表格的最后一行选中

table.changeSelection(model.getRowCount()-1,0,false,false);

java课程设计----图书馆管理系统

//查询数据库

public void search(){ }

//删除选定的记录 public void delete(){ }

//内部类,用于显示/编辑图书信息的一个模式对话框

class BookEditor extends JDialog implements ActionListener {

final int OK=1;

final int CANCEL=-1; final int CLOSE=0;

private int actionCode=CANCEL; int selected=table.getSelectedRow();

if(selected>=0&&selected<model.getRowCount()){ } else

JOptionPane.showMessageDialog(f,"请选定要删除的一行!"

,"提示",RMATION_MESSAGE);

int result=JOptionPane.showConfirmDialog (f,"您确定要删除该条图书信息 ,"确认",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); if (result!=JOptionPane.YES_OPTION) return ;

String deleteSql="delete from bookInfo where bookID=?"; Object []keys={model.getValueAt(selected,0).toString()}; if(SqlUtil.deleteFromDB(con,deleteSql,keys))

model.removeRow(selected);

SearchDialog sd=new SearchDialog(f,"查询",true); sd.show();

if(sd.getActionCode()==sd.OK){ }

String readSql=sd.getSQL();

SqlUtil.readDBToTable(con,readSql,model,dataType);

吗?"

private JLabel labID=new JLabel(" 图书编号 "); private JTextField txtID=new JTextField();

private JLabel labBookName=new JLabel(" 图书名称 "); private JTextField txtBookName=new JTextField(); private JTextField txtBookPrice=new JTextField(); private JTextField txtBookPress=new JTextField(); private JButton btnPre=new JButton("上一个"); private JButton btnNext=new JButton("下一个");

private JLabel labBookPrice=new JLabel(" 单 价 "); private JLabel labBookPress=new JLabel(" 出 版 社 ");

java课程设计----图书馆管理系统

private JButton btnCancel=new JButton("取消"); private JFrame f=null;

public BookEditor(JFrame f,String s,boolean b) {

LayoutUtil.add(p,GridBagConstraints.NONE,

GridBagConstraints.CENTER,0,0,0,1,1,1,labBookPrice); GridBagConstraints.CENTER,100,0,1,1,1,1,txtBookPrice); GridBagConstraints.CENTER,0,0,2,1,1,1,labBookPress); GridBagConstraints.CENTER,100,0,3,1,1,1,txtBookPress);

LayoutUtil.add(p,GridBagConstraints.HORIZONTAL, LayoutUtil.add(p,GridBagConstraints.NONE, LayoutUtil.add(p,GridBagConstraints.HORIZONTAL, //注册事件侦听器

btnOk.addActionListener(this); btnCancel.addActionListener(this); btnPre.addActionListener(this); btnNext.addActionListener(this);

btnOk.setIcon(new ImageIcon("image/ok20.gif"));

btnCancel.setIcon(new ImageIcon("image/cancel20.gif")); btnPre.setIcon(new ImageIcon("image/pre20.gif")); btnNext.setIcon(new ImageIcon("image/next20.gif")); btnNext.setHorizontalTextPosition(SwingConstants.LEFT); //在增加模式下前后移动按钮置为不可用

//读者扩展该程序具有编辑功能时,可以将前后移动按钮置为可用 if(s.equals("增加")){ }

btnPre.setEnabled(false); btnNext.setEnabled(false);

super(f,s,b); this.f=f;

//面板p中显示图书信息 JPanel p=new JPanel();

p.setLayout(new GridBagLayout());

LayoutUtil.add(p,GridBagConstraints.NONE,

GridBagConstraints.CENTER,0,0,0,0,1,1,labID); GridBagConstraints.CENTER,100,0,1,0,1,1,txtID); GridBagConstraints.CENTER,0,0,2,0,1,1,labBookName); GridBagConstraints.CENTER,100,0,3,0,1,1,txtBookName);

LayoutUtil.add(p,GridBagConstraints.HORIZONTAL, LayoutUtil.add(p,GridBagConstraints.NONE, LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,

//面板ap中显示4个按钮 JPanel ap=new JPanel();

java课程设计----图书馆管理系统

}

LayoutUtil.add(ap,GridBagConstraints.NONE,

GridBagConstraints.CENTER,0,0,0,0,1,1,this.btnPre); GridBagConstraints.CENTER,0,0,1,0,1,1,this.btnNext); GridBagConstraints.CENTER,100,0,2,0,1,1,new JLabel()); GridBagConstraints.CENTER,0,0,3,0,1,1,this.btnOk); GridBagConstraints.CENTER,0,0,4,0,1,1,this.btnCancel);

LayoutUtil.add(ap,GridBagConstraints.NONE, LayoutUtil.add(ap,GridBagConstraints.HORIZONTAL, LayoutUtil.add(ap,GridBagConstraints.NONE, LayoutUtil.add(ap,GridBagConstraints.NONE,

getContentPane().add(p,BorderLayout.CENTER);

getContentPane().add(ap,BorderLayout.SOUTH); //将对话框窗口定位在父窗口的居中位置 setSizeAndPosition(550,120);

});

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){ }

actionCode=CLOSE;

//将对话框窗口定位在父窗口的居中位置

public void setSizeAndPosition(int w,int h){ }

public void actionPerformed(ActionEvent e){ } }

public String getBookName(){ }

return txtBookName.getText().trim();

Object s=e.getSource(); if(s==this.btnCancel) }

else if(s==this.btnOk) okClicked();

{

this.actionCode=this.CANCEL; this.setVisible(false); this.setSize(w,h); Dimension d=f.getSize(); Point pp=f.getLocation();

this.setLocation(pp.x+(d.width-w)/2,pp.y+(d.height-h)/2);

public String getID(){

return txtID.getText().trim();

//将图书单价由字符串类型转化为Double类型

//如果不是合法的数值字符串,取值Double.NEGATIVE_INFINITY

java课程设计----图书馆管理系统

}

}

}

public String getBookPress(){ }

public int getActionCode(){ }

public void okClicked() { }

//图书编号不为空,且价格为数值类型时 隐藏对话框 if(!this.txtID.getText().trim().equals("")){ } else

JOptionPane.showMessageDialog(f,"请设定图书编号!","提示",

RMATION_MESSAGE);

try{

Double.parseDouble(this.txtBookPrice.getText().trim()); }catch(Exception e){ }

this.actionCode=this.OK; this.setVisible(false);

JOptionPane.showMessageDialog(f,"价格非法!","提示",

RMATION_MESSAGE);

return this.actionCode;

return txtBookPress.getText().trim(); double result=Double.NEGATIVE_INFINITY; try{ }

return new Double(result);

result=Double.parseDouble(this.txtBookPrice.getText().trim()); result=Double.NEGATIVE_INFINITY; }catch(Exception e){

return ;

class SearchDialog extends JDialog implements ActionListener{

final int OK=1;

final int CANCEL=-1; final int CLOSE=0;

private int actionCode=CANCEL;

private JRadioButton radID=new JRadioButton(" 图书编号 "); private JTextField txtID=new JTextField();

private JRadioButton radBookName=new JRadioButton(" 图书名称 ");

java课程设计----图书馆管理系统

private JRadioButton radBookPress=new JRadioButton(" 出 版 社 "); private JTextField txtBookPress=new JTextField(); private JButton btnOk=new JButton("确定(Yes)"); private JButton btnCancel=new JButton("取消(Esc)"); public SearchDialog(JFrame f,String s,boolean b){

super(f,s,b); this.f=f;

ButtonGroup group=new ButtonGroup(); group.add(radID); group.add(radBookName); group.add(radBookPress); txtID.setEditable(false); radBookName.setSelected(true); txtBookPress.setEditable(false); radID.addActionListener(this); radBookName.addActionListener(this); radBookPress.addActionListener(this); //面板p中显示查询条件 JPanel p=new JPanel();

p.setLayout(new GridBagLayout());

LayoutUtil.add(p,GridBagConstraints.NONE,

GridBagConstraints.CENTER,0,0,0,0,1,1,radID); GridBagConstraints.CENTER,100,0,1,0,1,1,txtID); GridBagConstraints.WEST,0,0,0,1,1,1,radBookName); GridBagConstraints.CENTER,100,0,1,1,1,1,txtBookName); GridBagConstraints.CENTER,0,0,0,2,1,1,radBookPress); GridBagConstraints.CENTER,100,0,1,2,1,1,txtBookPress);

LayoutUtil.add(p,GridBagConstraints.HORIZONTAL, LayoutUtil.add(p,GridBagConstraints.NONE, LayoutUtil.add(p,GridBagConstraints.HORIZONTAL, LayoutUtil.add(p,GridBagConstraints.NONE, LayoutUtil.add(p,GridBagConstraints.HORIZONTAL, btnOk.setIcon(new ImageIcon("image/ok20.gif"));

btnCancel.setIcon(new ImageIcon("image/cancel20.gif")); btnOk.addActionListener(this); btnCancel.addActionListener(this); //面板ap中显示确定 取消按钮 JPanel ap=new JPanel(); ap.add(btnOk); ap.add(btnCancel);

getContentPane().add(p,BorderLayout.CENTER); getContentPane().add(ap,BorderLayout.SOUTH);

private JFrame f=null;

java课程设计----图书馆管理系统

}

this.addWindowListener(new WindowAdapter(){

});

public void windowClosing(WindowEvent e){ }

actionCode=CLOSE;

//将对话框窗口定位在父窗口的居中位置

public void setSizeAndPosition(int w,int h){ }

public void setTxtEditable(boolean b1,boolean b2,boolean b3){ }

public void actionPerformed(ActionEvent e){ }

public int getActionCode(){ }

public String getSQL(){

String sql="select * from bookInfo where "; if(radID.isSelected())

sql=sql+" bookID like '%"+txtID.getText().trim()+"%'";

sql=sql+" bookName like '%"+txtBookName.getText().trim()+"%'"; sql=sql+" bookPress like '%"+txtBookPress.getText().trim()+"%'"; else if(radBookName.isSelected()) else if(radBookPress.isSelected()) return actionCode; Object s=e.getSource(); if(s==this.radID)

setTxtEditable(true,false,false);

else if(s==this.radBookName) setTxtEditable(false,true,false); else if(s==this.radBookPress) setTxtEditable(false,false,true); else if(s==this.btnOk){ }

else if(s==this.btnCancel){ }

this.actionCode=this.CANCEL; this.setVisible(false); this.actionCode=this.OK; this.setVisible(false); txtID.setEditable(b1); txtBookName.setEditable(b2); txtBookPress.setEditable(b3); this.setSize(w,h); Dimension d=f.getSize(); Point pp=f.getLocation();

this.setLocation(pp.x+(d.width-w)/2,pp.y+(d.height-h)/2);

java课程设计----图书馆管理系统

}

}

package edu.njust.cs; import java.sql.*; import javax.swing.*; import java.util.*; import java.io.*; public class SqlUtil{

//读入配置文件

public static Properties loadProperty(String fileName){ }

Properties prop=new Properties(); try{ }

return prop;

FileInputStream in=new FileInputStream( prop.load(in); in.close();

System.getProperties().get("user.dir")+"/"+fileName);

}catch(IOException e){

e.printStackTrace();

JOptionPane.showMessageDialog(null, "配置文件丢失!\n建议重新安装程序",

"信息",

JOptionPane.ERROR_MESSAGE);

prop=null;

//建立和MS SQL SERVER的连接

public static Connection acquireConnection(

String host,String port,String dbName,String user, String pwd) throws ClassNotFoundException,SQLException{ Connection connection=null; try{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); String url ="jdbc:microsoft:sqlserver://"+

host+":"+port+";User="+user+";Password="+pwd;

connection= DriverManager.getConnection(url); connection.setCatalog(dbName);

e.printStackTrace(); throw e;

e.printStackTrace(); throw e;

}catch(ClassNotFoundException e){

}catch(SQLException e){

java课程设计----图书馆管理系统

}

return connection;

//使用指定的SQL语句和数据,向数据库插入一条记录 public static boolean addRowToDB(

Connection con,String insertSql,Object []lineForDBAdd){ }

//将适合数据库存储的一行数据(LineForDB) //转化为适合表格显示的数据(LineForTable)

//依据数据类型的不同,将代表空值的特殊值转化为空值 public static Object [] getLineForTableFromLineForDB(

Object []lineForDB,Class []dataType){

Object []lineForTable=new Object[lineForDB.length]; for(int i=0;i<lineForDB.length;i++){

if(dataType[i]==ng.String.class)

lineForTable[i]=lineForDB[i];

lineForTable[i]=null;

boolean flag=true;

PreparedStatement update=null; try{ }

return flag;

update=con.prepareStatement(insertSql); if(lineForDBAdd!=null)

for(int i=0;i<lineForDBAdd.length;i++)

update.setObject(i+1,lineForDBAdd[i]);

update.executeUpdate(); e.printStackTrace(); flag=false;

JOptionPane.showMessageDialog(null,

"从数据库插入数据时发生错误"+e, "信息",

JOptionPane.ERROR_MESSAGE);

}catch(SQLException e){

}finally{

if(update!=null)

try{ }

update.close(); ex.printStackTrace(); }catch(SQLException ex){

else if(dataType[i]==ng.Double.class){

if(((Double)lineForDB[i]).doubleValue()==Double.NEGATIVE_INFINITY)

java课程设计----图书馆管理系统

}

}

}

lineForTable[i]=lineForDB[i];

else if(dataType[i]==ng.Integer.class){ }

lineForTable[i]=null;

lineForTable[i]=lineForDB[i];

if(((Integer)lineForDB[i]).doubleValue()==Integer.MIN_VALUE) else

return lineForTable;

//清空表格中所有的数据

public static void clearAllRowsInTable(CustomTableModel model){ }

//从数据库读取数据到表格, dataType指明表格中每一列的数据类型 //当前只考虑了String, Double ,Integer,Boolean ,Timestamp类型. public static void readDBToTable(Connection con,String readSql,

CustomTableModel model, Class []dataType){

clearAllRowsInTable(model); PreparedStatement query=null; try{

query= con.prepareStatement(readSql); query.clearParameters();

ResultSet rs=query.executeQuery(); while(rs.next()){

int column=model.getColumnCount(); Object []line=new Object[column]; for(int i=0;i<column;i++){

}

else if(dataType[i]==ng.Integer.class){

if(rs.getInt(i+1)==Integer.MIN_VALUE) line[i]=null; else

line[i]=new Integer(rs.getInt(i+1));

if(dataType[i]==ng.String.class)

line[i]=rs.getString(i+1).trim();

if(rs.getDouble(i+1)==Double.NEGATIVE_INFINITY) line[i]=null; else

line[i]=new Double(rs.getDouble(i+1)); else if(dataType[i]==ng.Double.class){

while(model.getRowCount()>0)

model.removeRow(0);

java课程设计----图书馆管理系统

}

}

}

}

model.addRow(line);

}catch(SQLException e){

e.printStackTrace();

JOptionPane.showMessageDialog(null,

"从数据库读取数据时发生错误!"+ "SQL语句为:"+readSql+e,"提示", JOptionPane.ERROR_MESSAGE);

}finally{

if(query!=null)

try{ }

query.close(); ex.printStackTrace(); }catch(SQLException ex){

//使用指定的SQL语句删除数据库中的记录 public static boolean deleteFromDB(

Connection con,String deleteSql,Object []keys){ boolean flag=true;

PreparedStatement update=null; try{ }

update=con.prepareStatement(deleteSql); if(keys!=null)

for(int i=0;i<keys.length;i++)

update.setObject(i+1,keys[i]);

update.executeUpdate(); flag=false;

"从数据库删除时发生错误!\n"+e, "提示",

JOptionPane.ERROR_MESSAGE);

}catch(SQLException e){

JOptionPane.showMessageDialog(null,

}finally{

if(update!=null)

try{ }

update.close(); ex.printStackTrace(); }catch(SQLException ex){

java课程设计----图书馆管理系统

}

}

package edu.njust.cs; import javax.swing.*; import java.awt.*; public class LayoutUtil{

public static void add(Container c,int fill,int anchor,int weightx,int weighty, int x,int y,int width,int height,Component comp){ }

//使用JDBC直接建立数据库连接, 从配置文件读取驱动程序信息 import java.sql.*;

import java.util.Properties; import java.io.*; public class JdbcProp{

public static void main(String[] args){

try{

Properties prop=new Properties();

prop.load(new FileInputStream("db.cfg"));

GridBagConstraints constraints=new GridBagConstraints(); }

public static void add(Container c,int fill,int anchor,int weightx,int weighty,int

GridBagConstraints constraints=new GridBagConstraints(); }

constraints.insets=insets; constraints.fill=fill; constraints.anchor=anchor; constraints.weightx=weightx; constraints.weighty=weighty; constraints.gridx=x; constraints.gridy=y;

constraints.gridwidth=width; constraints.gridheight=height; c.add(comp,constraints); constraints.fill=fill; constraints.anchor=anchor; constraints.weightx=weightx; constraints.weighty=weighty; constraints.gridx=x; constraints.gridy=y;

constraints.gridwidth=width; constraints.gridheight=height; c.add(comp,constraints);

x,int y,int width,int height,Component comp,Insets insets){

java课程设计----图书馆管理系统

}

}

String ip=prop.getProperty("dbIP"); String port=prop.getProperty("dbPort");

String userName=prop.getProperty("dbUserName"); String password=prop.getProperty("dbPassword"); String dbName=prop.getProperty("defaultDbName"); String url ="jdbc:microsoft:sqlserver://"+ip+":"+port; Class.forName(driver);

Connection con=DriverManager.getConnection(url,userName,password); con.setCatalog(dbName);

Statement stmt = con.createStatement(); while(rs.next())

rs.close(); stmt.close();

System.out.println(rs.getString(1)+" "+rs.getString(2)

+" "+rs.getFloat(3)+" "+rs.getString(4));

ResultSet rs=stmt.executeQuery("select * from bookInfo");

}catch(Exception e){ e.printStackTrace(); }

package edu.njust.cs; import javax.swing.table.*;

public class CustomTableModel extends DefaultTableModel{ }

String [] columnNames; Class []dataType;

public CustomTableModel(int r,int c,String []cn,Class []dataType){ }

public boolean isCellEditable(int row, int col){ }

public String getColumnName(int c){ } }

return columnNames[c];

return false; super(r,c); columnNames=cn;

this.dataType=dataType;

public Class getColumnClass(int c){

return dataType[c];

java课程设计----图书馆管理系统

// 使用JDBC-ODBC桥建立数据库连接 import java.sql.*; public class JdbcOdbc{ public static void main(String[] args){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection

con=DriverManager.getConnection("jdbc:odbc:Book","admin","abc");

Statement stmt = con.createStatement(); ResultSet rs=stmt.executeQuery("select * from bookInfo");

while(rs.next()){ System.out.println(rs.getString(1)+"

"+rs.getFloat(3)+" "+rs.getString(4));

}

rs.close();

stmt.close();

}catch(Exception e){ e.printStackTrace(); }

}

}

"+rs.getString(2)+"

本文来源:https://www.bwwdw.com/article/icnm.html

Top