综合设计报告小型图书管理系统.doc
综 合 设 计 报 告小型图书管理系统学生姓名:学 号:年级专业:学 院:提交日期:2012年2月23日小型图书管理系统一、实验目的1、 熟练掌握SQL命令2、 掌握Swing图形用户界面编程。3、 掌握java与数据库的连接技术。4、 掌握java.sql包中提供各种类,编写程序实现数据库信息的存取和查询。5、 培养独立查找资料,并解决问题的能力。二、实验任务1、建立一个数据库Library,其中含有如下表格(字段):Books(ISBN,Title,Authors,Publisher,EditionNumber,PublicationDate,Type)Reader(ReaderID,FirstName,LastName,Address,PhoneNumber,Limits)Record(RecordID,ISBN,ReaderID,BorrowingDate,ReturnDate)Administrator(administratorID,Password)Student(ReaderID,Password)使用Microsoft SQL Server 2005数据库系统,提前设置好Books,Reader等的数据。2、使用java编程实现如下功能:(1)用户可通过相应界面,依据图书的ISBN,Title,Authors,Publisher,PublicationDate,Type的组合条件,查询数据库中的符合条件的书籍,程序需将查询结果以表格的方式展示给用户,用户可选择结果排序的依据。(2)提供用户相应的方式借书。当用户选择借阅某本书籍时,应查询该书是否已经被借出:如果被借出,则显示信息(“该书已被借出,归还时间XXXXX”);如该书未被借出,则查阅读者权限(是否超出该读者所能借阅的最大数目书籍),如未超出显示借书成功,并向数据库中增加一条借阅记录,如果超出,则显示信息(“已超过您的最大借阅数目)。(3)提供用户相应的方式还书。还书成功时,将该次的借阅记录删除。(4)修改数据库设计,通过相应界面,提供Reader、Books的增加、删除、修改功能。(5)修改数据库设计,可查询某用户的所有借阅记录。三、开发工具与平台1.开发工具:Eclipse默认是一个和Jbuilder类似的Java开发工具,但它不仅仅只 是Java开发工具,只要装上相应的插件,eclipse也可作为其它语言的开发工具。如C/C+插件(CDT)。2.开发平台:JDK1.73.数据库:Microsoft SQL Server 2005四、设计内容1.界面设计 (1)登录界面(可选择学生用户或者管理员身份)主要采用GridLayout布局管理。(2) 学生用户界面(主要通过JTree和卡片布局) A.卡片布局主要是实现: 当用户通过JTree选择不同功能时,实现右边界面的切换。 B.界面如下:(3) 管理界面的设计此界面主要采用BorderLayout和Box布局方式。2. 逻辑设计图书馆管理系统学生端管理员端查询图书归还图书借阅图书学生用户管理添加图书借阅图书归还图书管理添加加用户管理员规则查询图书2.1、学生用户端l 查询图书,学生用户可以进行简单查询和模糊查询;l 归还图书,学生自动输入自己的ID号,再输入所要还的书,点击确认归还;l 借阅图书,借阅图书,可由学生自行操作,当然这有点不符合图书馆的原则。2.2、管理员端l 学生用户管理,原本打算实现用户信息的增、删、改、查,时间关系此功能并没有实现。l 图书管理,包括图书的增、删、改、查,时间关系,目前也只做了增加图书和查询图书的功能。l 借阅管理,主要是学生借阅图书、归还图书和交纳罚款的管理。l 查询图书,可以进行简单查询和模糊查询。l 归还图书管理,实现学生的还书管理。l 添加新用户,目前只做了学生用户的添加,管理员得添加还要涉及超级管理员的管理,又时间较短,故也没有去实现。3. 程序测试 在程序测试中,主要问题在于组件放到有布局管理器的容器中时,其大小的设置有没有受到影响,比如setAlignmentX(alignmentX)在其他布局管理其中不起作用,只在BOx布局管理器中起作用,网格布局管理器的组件大小(按钮等)大小也没办法自定义大小,介于此,我在写 addBookFrame()和addUserFrame()两个界面时,采用 setLayout(null),使我能够完全按照自己的意愿来设置各组件的大小,同时也发现,其实布局管理器给我们提供的界面管理,也是非常非常好用好看的。五、附录 用eclipse编写的包及代码: 一 mon.Frame: 管理学生用户和管理员得公共界面,包括借书界面(borrowPanel.java)、登录界面(login.java)、还书界面(returnPanel.java),查询(lookPanel.java)。public class borrowPanel extends JPanel implements ActionListener private final int MAX_STUDENT_BORROW = 6; private JButton yes; private JButton no; private JButton look; private JTable result; private JPanel pNorth; private JPanel p1; private JPanel p2; private JLabel bookISBN; private JTextField isbn; private String c ="ISBN","书名","作者","出版社","编号","出版日期","类型" private String data = new String3535; private JScrollPane s; private String readerId; private boolean isLook =false;/是否存在; private String date;/借阅日期; private String rdate;/还书日期; private String DBDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver" private String url = "jdbc:sqlserver:/localhost:1433;DatabaseName=Library" private static final long serialVersionUID = -8959055752439578441L; public borrowPanel(String readerId) super(); setName("借阅图书"); setSize(450,400); setLayout(new BorderLayout(); setBackground(Color.RED); this.readerId = readerId;/当前登录的读者ID号; Init(); this.add(pNorth,BorderLayout.NORTH); s.setAutoscrolls(true); this.add(s,BorderLayout.CENTER); setVisible(true); private void Init() yes = new JButton("确认借阅");yes.setBorder(BorderFactory.createRaisedBevelBorder();yes.setPreferredSize(new Dimension(150, 30);no = new JButton("取消");no.setBorder(BorderFactory.createRaisedBevelBorder();no.setPreferredSize(new Dimension(150, 30);look = new JButton("查询");look.setBorder(BorderFactory.createRaisedBevelBorder();look.setPreferredSize(new Dimension(150, 30);bookISBN = new JLabel("请输入所要借阅的图书ISBN号:");isbn = new JTextField(15);pNorth = new JPanel();pNorth.setLayout(new BorderLayout();p1 =new JPanel();p2 =new JPanel();p1.setLayout(new FlowLayout();p2.setLayout(new FlowLayout();p2.add(bookISBN,SwingConstants.CENTER);p2.add(isbn);p1.add(yes);p1.add(no);p1.add(look);pNorth.add(p1,BorderLayout.CENTER);pNorth.add(p2,BorderLayout.NORTH);result = new JTable(data,c);s = new JScrollPane(result);look.addActionListener(this);yes.addActionListener(this);no.addActionListener(this);public void actionPerformed(ActionEvent e) if(e.getSource() = yes)SimpleDateFormat f = new SimpleDateFormat("yyyy.MM.dd");date = f.format(new Date();rdate = returndate(date);if(result.getSelectedRow()>=0)&&(isLook)borrow();else if(e.getSource() = look) isLook = look();else if(e.getSource() = no)private String returndate(String date2) /归还日期一个月String d = date2.split(".");String str1 ="" ,str2 = "",str3= ""if(d1.equals("10")|d1.equals("11")|d1.equals("12")if(d1.equals("12") str1 = String.valueOf(Integer.parseInt(d0)+1); str2 = "01" str3 = d2;elsestr1 =d0;int s = Integer.parseInt(d1)-1;str2 = String.valueOf(s);str3 = d2;else int l = Integer.parseInt(d1.substring(1, 2)+1;str1 = d0;str2 = String.valueOf(l);str3 = d2;return str1+"."+str2+"."+str3;private void borrow() Connection cn; Statement sm;String bookIsbn = isbn.getText().trim();String sql1 = "select * from Record where ISBN = '"+bookIsbn+"'"/从Record中查看是否被借走;String sql2 = "insert into Record values(null,'"+bookIsbn+"','"+readerId+"','"+date+"','"+rdate+"')"/插入到Record;String sql3 = "select * from Reader where ReaderID = '"+readerId+"'"/查看读者已借阅的图书数;ResultSet rs1,rs2;int isInsert;int readerBook = 0;/已借阅的图书数;try Class.forName(DBDriver); / 加载驱动器cn = DriverManager.getConnection(url, "sa", "123456"); / 连接数据库sm = cn.createStatement();/查询该读者已借阅的图书数;rs2 = sm.executeQuery(sql3);while(rs2.next()+readerBook;if(readerBook = MAX_STUDENT_BORROW)JOptionPane.showMessageDialog(null, "该读者借阅图书数已超过上限,不能再继续借书");return;else /查询此图书被借阅的情况;rs1 =sm.executeQuery(sql1);if(!rs1.next()/判断是否已经被别人借走;isInsert = sm.executeUpdate(sql2);if(isInsert = 1)/借书成功JOptionPane.showMessageDialog(null,"借阅成功");else JOptionPane.showMessageDialog(null, "借书功能维护中,暂时无法借阅");/end if(isInsert = 1)else String str = rs1.getString("ReturnDate");JOptionPane.showMessageDialog(null, "此图书已被借走,不在架"+"归还日期"+str); / end else cn.close();catch(Exception e)JOptionPane.showMessageDialog(null, "数据库连接失败。");private boolean look() /按ISBN号查询Connection cn; Statement sm;String bookIsbn = isbn.getText().trim();String sql = "select * from books where ISBN = '"+bookIsbn+"'"ResultSet rs;try Class.forName(DBDriver); / 加载驱动器cn = DriverManager.getConnection(url, "sa", "123456"); / 连接数据库sm = cn.createStatement();rs = sm.executeQuery(sql);if(rs.next()/如果存在此书; data00 = rs.getString(1); data01 = rs.getString(2); data02 = rs.getString(3); data03 = rs.getString(4); data04 = rs.getString(5); data05 = rs.getString(6); data06 = rs.getString(7); repaint(); cn.close(); return true; else cn.close();return false;catch(Exception e)JOptionPane.showMessageDialog(null, "查询失败,请检查连接");return false;public class loginFrame extends JFrame implements ActionListener /* 用户登录界面 */private JLabel numLabel;private JLabel codeLabel;private JLabel background;private JTextField num;private JPasswordField code;private JButton loginButton;private JButton exitButton;private JPanel pCenter;private JPanel pSouth;private JRadioButton adiministrator;private JRadioButton student;private static final long serialVersionUID = -8959055752439578441L;loginFrame()super();setTitle("用户登录");setSize(350,300);/设置窗体大小setResizable(false);/设置不可调窗体大小Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();/返回窗口的大小setLocation(dimension.width-getWidth()/2,(dimension.height-getHeight()/2);createLoginFrame();getContentPane().add(background,BorderLayout.NORTH);getContentPane().add(pCenter,BorderLayout.CENTER);getContentPane().add(pSouth,BorderLayout.SOUTH);pack();setVisible(true);private void createLoginFrame() numLabel = new JLabel("用户名",SwingUtilities.CENTER);codeLabel = new JLabel("密码",SwingUtilities.CENTER);background = new JLabel(new ImageIcon("C:UsersHZLworkspaceLibrarybackgroundlogin.png");num = new JTextField();code = new JPasswordField();code.setEchoChar('*');/设置此 JPasswordField 的回显字符。loginButton = new JButton("登录");loginButton.setBorder(BorderFactory.createRaisedBevelBorder();loginButton.setPreferredSize(new Dimension(150, 30);exitButton = new JButton("退出");exitButton.setBorder(BorderFactory.createRaisedBevelBorder();exitButton.setPreferredSize(new Dimension(150, 30);pCenter = new JPanel();pSouth = new JPanel();adiministrator = new JRadioButton("管理员");adiministrator.addActionListener(this);student = new JRadioButton("学生");student.addActionListener(this);loginButton.addActionListener(this);exitButton.addActionListener(this);adiministrator.addActionListener(this);student.addActionListener(this);ButtonGroup group = new ButtonGroup();group.add(adiministrator);group.add(student);pCenter.setLayout(new GridLayout(2,2,3,3); pSouth.setLayout(new GridLayout(2,2,4,4);pCenter.add(numLabel);pCenter.add(num);pCenter.add(codeLabel);pCenter.add(code);loginButton.setPreferredSize(new Dimension(2,2);exitButton.setPreferredSize(new Dimension(2,2);pSouth.add(student);pSouth.add(adiministrator);pSouth.add(loginButton);pSouth.add(exitButton);public static void main(String args) new loginFrame();public void actionPerformed(ActionEvent e) if(e.getSource() = loginButton)&&adiministrator.isSelected()if(new Check(num.getText().trim().toString(),String.valueOf(code.getPassword().trim(),'A').CheckRight()dispose();new administratorFrame(num.getText().trim();else JOptionPane.showMessageDialog(null, "密码错误或账号不存在,请重新登录");/end loginButton adiministrator.isSelected() else if(e.getSource() = loginButton)&&student.isSelected() if(new Check(num.getText().trim(),String.valueOf(code.getPassword().trim(),'S').CheckRight()dispose();new studentFrame(num.getText().trim();else JOptionPane.showMessageDialog(null, "密码错误或账号不存在,请重新登录");/end loginButton student.isSelected() else if(e.getSource() = exitButton) System.exit(0); public class lookPanel extends JPanel implements ActionListener private JLabel labelBook;private JTextField textBook;private JPanel p1;private JPanel p2;private JScrollPane s;private CardLayout card;private JButton lookAllButton;private JButton lookButton;private JButton rt;private JTable lookTable;private String c = "ISBN", "书名", "作者", "出版社", "编号", "出版日期", "类型" ;private String data = new String2525;private String DBDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"private String url = "jdbc:sqlserver:/localhost:1433;DatabaseName=Library"private static final long serialVersionUID = -8959055752439578441L;public lookPanel() super();setName("查询图书");setSize(250, 300);setBackground(Color.RED);Init();p1.setLayout(new GridLayout(8, 2, 3, 3);for (int i = 0; i < labelBook.length; +i) p1.add(labelBooki);p1.add(textBooki);p1.add(lookButton);p1.add(lookAllButton);lookButton.addActionListener(this);lookAllButton.addActionListener(this);rt.addActionListener(this);lookTable = new JTable(data, c);s = new JScrollPane(lookTable);p2.setLayout(new BorderLayout();p2.add(s, BorderLayout.CENTER);p2.add(rt, BorderLayout.SOUTH);this.setLayout(card);this.add(p1, "1");this.add(p2, "2");setVisible(true);private void Init() p1 = new JPanel();p2 = new JPanel();rt = new JButton("返回查询条件设置");rt.setBorder(BorderFactory.createRaisedBevelBorder();rt.setPreferredSize(new Dimension(150, 30);lookButton = new JButton("依据参数查询");lookButton.setBorder(BorderFactory.createRaisedBevelBorder();lookButton.setPreferredSize(new Dimension(150, 30);lookAllButton = new JButton("查询全部");lookAllButton.setBorder(BorderFactory.createRaisedBevelBorder();lookAllButton.setPreferredSize(new Dimension(150, 30);labelBook = new JLabel7;textBook = new JTextField7;for (int t = 0; t < labelBook.length; +t) labelBookt = new JLabel("", JLabel.CENTER);textBookt = new JTextField();labelBook0.setText("ISBN");labelBook1.setText("书名");labelBook2.setText("作者");labelBook3.setText("出版社");labelBook4.setText("编号");labelBook5.setText("出版日期");labelBook6.setText("类型");card = new CardLayout();public void actionPerformed(ActionEvent e) if (e.getSource() = lookAllButton) lookAllBook();card.show(this, "2"); else if (e.getSource() = lookButton) lookBook();card.show(this, "2"); else if (e.getSource() = rt) card.show(this, "1");private void lookBook() Connection con;Statement stmt;ResultSet result;String sql;String s = new String7;for(int i =0;i<s.length;+i)si = ""if(!textBook0.getText().trim().equals("")s0 = textBook0.getText().trim();sql = "select * from Books where ISBN = '"+s0+"'"else if(!textBook4.getText().trim().equals("")s4 = textBook4.getText().trim();sql = "select * from Books where EditionNumber = '"+s6+"'"else sql = "select * from Books where Title like '%"+ s1+"%' or Type like '%"+s6+"%'" +"or Authors like '%"+s2+"%' or Publisher like '%"+s3+"%' or PublicationDate like '%"+s5+"%'" ; int c = 0;try Class.forName(DBDriver); / 加载驱动器con = DriverManager.getConnection(url, "sa", "12