《最新java做的简单图书管理系统.doc》由会员分享,可在线阅读,更多相关《最新java做的简单图书管理系统.doc(59页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-datejava做的简单图书管理系统package Tools;package Tools;import java.sql.*;import org.junit.Test;public class ConnDBConnection con;Statement stmt = null;ResultSet rs = null;String driverName=com.mysql
2、.jdbc.Driver; /驱动程序名String user=root; /MySQL配置时的用户名String password=root; /MySQL配置时的密码String url=jdbc:mysql:/localhost:3306/library; /URL指向要访问的数据库地址/连接数据库public ConnDB()tryClass.forName(driverName); /加载数据库程序 con = DriverManager.getConnection(url, user, password); /使用getConnection()方法,连接MySQL数据库!if (!
3、con.isClosed()System.out.println(连接成功!);elseSystem.out.println(连接失败!);catch(Exception e)System.err.println(e.getMessage();/执行SQL查询,并返回查询结果集public ResultSet select(String sql)tryClass.forName(driverName);con = DriverManager.getConnection(url,user,password);stmt=con.createStatement();rs=stmt.executeQu
4、ery(sql);catch(Exception e)System.err.println(e.getMessage();return rs;Testpublic void testSelect()/*执行SQL更新,并返回影响到的记录条数*/public int update(String sql)int rs=0;tryClass.forName(driverName);con=DriverManager.getConnection(url,user,password);stmt=con.createStatement();rs=stmt.executeUpdate(sql);catch(
5、Exception e)System.err.println(e.getMessage();return rs;public void testupdate()public static void main(String args)new ConnDB();package Library.Management.System;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import javax.swing.text.JTe
6、xtComponent;import Tools.ConnDB;public class addInterface implements ActionListener JFrame frame;JPanel p1,p2,p3;JLabel l1,l2,l3,l4;JTextField t1,t2,t3,t4;JButton last,next,enter,cancel,delete;public addInterface()frame = new JFrame(增加/修改);l1 = new JLabel(图书编号);t1 = new JTextField(10);l2 = new JLabe
7、l(书本名称);t2 = new JTextField(10);l3 = new JLabel(定价 );t3 = new JTextField(10);l4 = new JLabel(出版社 );t4 = new JTextField(10);p1 = new JPanel();p1.add(l1);p1.add(t1);p1.add(l2);p1.add(t2);p2 = new JPanel();p2.add(l3);p2.add(t3);p2.add(l4);p2.add(t4);p3=new JPanel();last = new JButton(上一条);next = new JB
8、utton(下一条);enter = new JButton(确认);delete = new JButton(删除);cancel = new JButton(取消);p3.add(last);p3.add(next);p3.add(enter);p3.add(cancel);p3.add(delete);enter.addActionListener(this);cancel.addActionListener(this);frame.add(BorderLayout.NORTH,p1);frame.add(BorderLayout.CENTER,p2);frame.add(BorderL
9、ayout.SOUTH,p3);frame.setVisible(true);frame.setSize(500, 150);frame.setResizable(false);frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);/*public static void main(String args)new addInterface();*/Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSour
10、ce()=cancel)frame.dispose();if(e.getSource()=enter)ConnDB c=new ConnDB();String sql = insert library values(+t1.getText()+,+t2.getText()+,+t3.getText()+,+t4.getText()+);SuppressWarnings(unused)int rs = c.update(sql);String rowValues= t1.getText(),t2.getText(),t3.getText(),t4.getText();InterfaceMain.
11、tablemodel.addRow(rowValues);package Library.Management.System;/import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.ResultSet;import javax.swing.*;import Tools.ConnDB;public class selInterface implements ActionListener JFrame frame;JPanel p1,p2,p3
12、,p4,p5,p6;JLabel l1,l2,l3;JRadioButton bh,mc,cbs;ButtonGroup g;JTextField t1,t2,t3;JButton enter,cancel;JTextArea jt;JScrollPane scrollPane;public selInterface()frame = new JFrame(查询);bh = new JRadioButton();bh.addActionListener(this);l1 = new JLabel(图书编号);t1 = new JTextField(10);mc = new JRadioButt
13、on();mc.addActionListener(this);l2 = new JLabel(书本名称);t2 = new JTextField(10);cbs = new JRadioButton();cbs.addActionListener(this);l3 = new JLabel( 出 版 社 );t3 = new JTextField(10);t1.setEditable(false);t2.setEditable(false);t3.setEditable(false);g = new ButtonGroup();g.add(bh);g.add(mc);g.add(cbs);p
14、1 = new JPanel(); p1.add(bh);p1.add(l1);p1.add(t1);p2 = new JPanel(); p2.add(mc);p2.add(l2);p2.add(t2);p3 = new JPanel(); p3.add(cbs);p3.add(l3);p3.add(t3);p4 = new JPanel();enter = new JButton(确定);cancel = new JButton(取消);enter.addActionListener(this);cancel.addActionListener(this);p4.add(enter);p4
15、.add(cancel);p5 = new JPanel();jt=new JTextArea(10,20);jt.setEditable(false);scrollPane =new JScrollPane(jt);p5.add(scrollPane);p6 = new JPanel();/p6.setLayout(new GridLayout(5,1);p6.add(p1);p6.add(p2);p6.add(p3);p6.add(p5);p6.add(p4);frame.add(p6);frame.setVisible(true);frame.setSize(350, 400);fram
16、e.setResizable(false);frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);public static void main(String args)new selInterface();Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(bh.isSelected()t1.setEditable(true);t2.setEditable(false);t3.setEditable(false);
17、if(mc.isSelected()t1.setEditable(false);t2.setEditable(true);t3.setEditable(false);if(cbs.isSelected()t1.setEditable(false);t2.setEditable(false);t3.setEditable(true);if(e.getSource()=enter)String sql=null;ConnDB con=new ConnDB();sql = SELECT * FROM library where 图书编号=+t1.getText()+ or 书本名称=+t2.getT
18、ext()+ or +t3.getText()+=出版社;ResultSet rs = con.select(sql);String tempData = new String4;try while (rs.next() for (int i = 0; i 4; i+) tempDatai = rs.getString(i + 1);jt.append(tempDatai+ );jt.append(n); catch (Exception e2) / TODO: handle exceptionif(e.getSource()=cancel)frame.dispose();package Li
19、brary.Management.System;import java.awt.*;import java.awt.event.*;import java.sql.*;import java.util.Vector;import javax.swing.*;import javax.swing.table.DefaultTableModel;import Tools.ConnDB;public class InterfaceMain implements ActionListenerJFrame frame;JPanel p1,p2,p3;JButton add,sel,del,exit;JT
20、able table;static DefaultTableModel tablemodel;JScrollPane scrollPane;Connection con;public InterfaceMain()/主界面和四个按钮还有监控按钮(北部)frame = new JFrame(图书管理系统);p1 = new JPanel();add = new JButton(增加);sel = new JButton(查询);del = new JButton(删除);exit = new JButton(退出);p1.add(add);p1.add(sel);p1.add(del);p1.a
21、dd(exit);add.addActionListener(this);sel.addActionListener(this);del.addActionListener(this);exit.addActionListener(this);/分割线(中部)p2 =new JPanel();JSeparator js=new JSeparator();js.setPreferredSize(new Dimension(1000,1);p2.add(js);/表格columnName是第一列的列名 tableValues是表格内容(南部)Vector columnName=new Vector
22、();columnName.add(图书编号);columnName.add(书名);columnName.add(定价);columnName.add(出版社);VectorVector tableValue=new VectorVector();ConnDB c=new ConnDB();String sql = SELECT * FROM library;ResultSet rs = c.select(sql);try while (rs.next() Vector row=new Vector();String data = new String4;for (int i = 0; i
23、4; i+) datai = rs.getString(i+1);/System.out.println(datai);row.add(datai);tableValue.add(row); catch (Exception e) / TODO: handle exceptiontablemodel=new DefaultTableModel(tableValue,columnName);table = new JTable(tablemodel);table.addMouseListener(new MouseListener()Overridepublic void mouseClicke
24、d(MouseEvent e) int row = table.getSelectedRow();SuppressWarnings(unused)Object o=tablemodel.getValueAt(row, 0);Overridepublic void mousePressed(MouseEvent e) Overridepublic void mouseReleased(MouseEvent e) Overridepublic void mouseEntered(MouseEvent e) Overridepublic void mouseExited(MouseEvent e)
25、);scrollPane = new JScrollPane(table);p3 = new JPanel();p3.add(scrollPane);/设置整体面板属性frame.add(BorderLayout.NORTH,p1);frame.add(BorderLayout.CENTER,p2);frame.add(BorderLayout.SOUTH,p3);frame.setVisible(true);frame.setSize(500, 500);frame.setResizable(false);frame.setDefaultCloseOperation(WindowConsta
26、nts.EXIT_ON_CLOSE);/主方法public static void main(String args)new InterfaceMain();/四个按钮的时间处理Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource()=add)new addInterface();if(e.getSource()=sel)new selInterface();if(e.getSource()=del)int n=JOptionPane.showConf
27、irmDialog(null, 是否删除?, 注意!,JOptionPane.YES_NO_OPTION );System.out.println(n);if(n=0)int row=table.getSelectedRow();Object bh= tablemodel.getValueAt(row, 0); ConnDB con = new ConnDB(); String sql = Delete FROM library where 图书编号=+bh; con.update(sql); tablemodel.removeRow(table.getSelectedRow();elseSy
28、stem.out.println(取消);if(e.getSource()=exit)System.exit(0);package Library.Management.System;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import javax.swing.text.JTextComponent;import Tools.ConnDB;public class addInterface implements Ac
29、tionListener JFrame frame;JPanel p1,p2,p3;JLabel l1,l2,l3,l4;JTextField t1,t2,t3,t4;JButton last,next,enter,cancel,delete;public addInterface()frame = new JFrame(增加/修改);l1 = new JLabel(图书编号);t1 = new JTextField(10);l2 = new JLabel(书本名称);t2 = new JTextField(10);l3 = new JLabel(定价 );t3 = new JTextFiel
30、d(10);l4 = new JLabel(出版社 );t4 = new JTextField(10);p1 = new JPanel();p1.add(l1);p1.add(t1);p1.add(l2);p1.add(t2);p2 = new JPanel();p2.add(l3);p2.add(t3);p2.add(l4);p2.add(t4);p3=new JPanel();last = new JButton(上一条);next = new JButton(下一条);enter = new JButton(确认);delete = new JButton(删除);cancel = ne
31、w JButton(取消);p3.add(last);p3.add(next);p3.add(enter);p3.add(cancel);p3.add(delete);enter.addActionListener(this);cancel.addActionListener(this);frame.add(BorderLayout.NORTH,p1);frame.add(BorderLayout.CENTER,p2);frame.add(BorderLayout.SOUTH,p3);frame.setVisible(true);frame.setSize(500, 150);frame.se
32、tResizable(false);frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);/*public static void main(String args)new addInterface();*/Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource()=cancel)frame.dispose();if(e.getSource()=enter)ConnDB c=new ConnDB();String sql = insert library values(+t1.getText()+,+t2.getText()+,+t3.getText()+,+t4.getText()+);SuppressWarnings(unused)int rs = c.update(sql);String rowValues= t1.getText(),t2.getText(),t3.getText(),t4.getText();InterfaceMain.tablemodel.addRow(rowValues);-