package Tools;import java.sql.*;import org.junit.Test;public class ConnDB{Connection con;Statement stmt = null;ResultSet rs = null;String driverName="com.mysql.jdbc.Driver"; //驱动程序名String user="root"; //MySQL 配置时的用户名String password="root"; //MySQL配置时的密码String url="jdbc:mysql://localhost:3306/library"; //URL指向要访问的数据库地址///连接数据库public ConnDB(){try{Class.forName(driverName);//加载数据库程序con = DriverManager.getConnection(url, user, password); //使用getConnection()方法,连接MySQL数据库!!if (!con.isClosed()){System.out.println("连接成功~!");}else{System.out.println("连接失败~!");}}catch(Exception e){System.err.println(e.getMessage());}}//执行SQL查询,并返回查询结果集public ResultSet select(String sql){try{Class.forName(driverName);con = DriverManager.getConnection(url,user,password);stmt=con.createStatement();rs=stmt.executeQuery(sql);}catch(Exception e){System.err.println(e.getMessage());}return rs;}@Testpublic void testSelect(){}/*执行SQL更新,并返回影响到的记录条数*/public int update(String sql){int rs=0;try{Class.forName(driverName);con=DriverManager.getConnection(url,user,password);stmt=con.createStatement();rs=stmt.executeUpdate(sql);}catch(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.JTextComponent;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 JLabel("书本名称");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 JButton("下一条");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(BorderLayout.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.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);}}}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,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 JRadioButton();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);p1 = 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.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);frame.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);}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.getText()+"' or '"+t3.getText()+"'=出版社";ResultSet rs = con.select(sql);String[] tempData = new String[4];try {while (rs.next()) {for (int i = 0; i < 4; i++){tempData[i] = rs.getString(i + 1);jt.append(tempData[i]+" ");}jt.append("\n");}} catch (Exception e2) {// TODO: handle exception}}if(e.getSource()==cancel){frame.dispose();}}}package Library.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 ActionListener{JFrame frame;JPanel p1,p2,p3;JButton add,sel,del,exit;JTable 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.add(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<String> columnName=new Vector<String>();columnName.add("图书编号");columnName.add("书名");columnName.add("定价");columnName.add("出版社");Vector<Vector<String>> tableValue=new Vector<Vector<String>>();ConnDB c=new ConnDB();String sql = "SELECT * FROM library";ResultSet rs = c.select(sql);try {while (rs.next()) {Vector<String> row=new Vector<String>();String[] data = new String[4];for (int i = 0; i < 4; i++) {data[i] = rs.getString(i+1);//System.out.println(data[i]);row.add(data[i]);}tableValue.add(row);}} catch (Exception e) {// TODO: handle exception}tablemodel=new DefaultTableModel(tableValue,columnName); table = new JTable(tablemodel);table.addMouseListener(new MouseListener(){@Overridepublic void mouseClicked(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) {}});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(WindowConstants.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.showConfirmDialog(null, "是否删除?", "注意",JOptionPane.YES_NO_OPTION );System.out.println(n);if(n==0){int row=table.getSelectedRow();Object bh= tablemodel.getV alueAt(row, 0);ConnDB con = new ConnDB();String sql = "Delete FROM library where 图书编号="+bh;con.update(sql);tablemodel.removeRow(table.getSelectedRow());}else{System.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 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 JLabel("书本名称");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 JButton("下一条");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(BorderLayout.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.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);}}}。