1、本程序以java为设计语言,可以实现一种简朴图书管理系统,可以实现简朴增长图书,查询图书,删除图书功能,其中用到数据库连接,窗体布局管理。本程序有如下几种功能函数构成所有模块都在名为bookmanager包中1:主函数mainframe.java2:增长模块 adddialog.java3:查询模块 searchdialog.java4:查询成果模块 searchresult.java5:课本类book.java6:数据库连接类 dbcon.java1. 主界面2. 添加模块3. 查询模块4. 输出模块5.6.源程序如下1.mainframe.javapackage bookmanager;i
2、mport javax.swing.*;import javax.swing.table.DefaultTableModel;import java.awt.*;import java.awt.event.*;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.LinkedList;public class mainframe extends JFrame implements Ac
3、tionListenerprivate JToolBar tb;String btntitle=增长,查询,删除,退出;String tabeltitle=图书编号,书名,定价,出版社;JButton btn=new JButton4;JTable table; DefaultTableModel model;public mainframe()super(图书管理系统);/初始化各个按钮tb=new JToolBar(工具栏);for(int i=0;ibtntitle.length;i+)btni=new JButton(btntitlei);btni.addActionListener(
4、this);tb.add(btni);/创立表模型model=new DefaultTableModel(tabeltitle,10);table=new JTable(model);/设定单元格高度table.setRowHeight(25);add(tb,BorderLayout.NORTH );add(new JScrollPane(table),BorderLayout.CENTER );setSize(900,600);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );setLocation(100,30);init();public v
5、oid actionPerformed(ActionEvent e)mainframe m=new mainframe();Object o=e.getSource();if(o=btn0)adddialog ad=new adddialog(m,25,25);init();if(o=btn1)searchdialog sd=new searchdialog(m,25,25);sd.dialog.setVisible(true);init();if(o=btn2)int result=JOptionPane.showConfirmDialog(null,的确要删除该条图书信息吗?,确认,JOp
6、tionPane.YES_NO_OPTION ,JOptionPane.QUESTION_MESSAGE );if(result=0)Connection con=dbcon.getconnectin();Object ob=model.getValueAt(table.getSelectedRow(),0);dbcon.delete(con,ob);init();else return ;if(o=btn3)System.exit(0);public void init()Connection con=dbcon.getconnectin();LinkedList list=null;try
7、 PreparedStatement stmt=con.prepareStatement(select * from book);ResultSet rs=stmt.executeQuery(); list=new LinkedList();while(rs.next()String id=rs.getString(1);String name=rs.getString(2);String price=rs.getString(3);String publish=rs.getString(4);book b=new book(id,name,Float.valueOf(price),publi
8、sh);list.add(b);model.setRowCount(0);for(int i=0;ilist.size();i+)book b=(book)list.get(i);Object str=b.getid(),b.getname(),b.getprice(),b.getpublish();model.addRow(str); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();public static void main(String args)new mainframe().sh
9、ow();*2.Adddialog.java添加模块package bookmanager;import java.awt.*;import java.awt.event.*;import java.util.LinkedList;import javax.swing.*;public class adddialog implements ActionListener JLabel label=new JLabel4;JTextField txt=new JTextField4;String ltitle=图书编号,图书名称,单 价,出 版 社;String btitle=上一种,下一种,确
10、定,取 消;JButton btn=new JButton4;JDialog dialog;JPanel p;LinkedList list;public adddialog(JFrame f,int x,int y)dialog=new JDialog(f,增长,true);dialog.setLocation(x,y);p=new JPanel(); list=new LinkedList();/p.setBorder(BorderFactory.createTitledBorder(请选取操作);/p.setBorder(BorderFactory.createLineBorder(Co
11、lor.red );/p.setBorder(BorderFactory.createBevelBorder(0,Color.red ,Color.green );p.setBorder(BorderFactory.createBevelBorder(0,Color.red ,Color.green ,Color.blue ,Color.yellow );for(int i=0;iltitle.length ;i+)labeli=new JLabel(ltitlei);labeli.setFont(new Font(宋体,Font.PLAIN ,18);txti=new JTextField(
12、);btni=new JButton(btitlei);btni.addActionListener(this);btni.setFont(new Font(幼圆,Font.PLAIN ,15);dialog.add(labeli);dialog.add(txti);dialog.add(btni);dialog.setLayout(null);p.setBounds(10,150,310,160); p.setBackground(Color.green );label0.setBounds(10,10,90,25);txt0.setBounds(110,10,200,25);label1.
13、setBounds(10,45,90,25);txt1.setBounds(110,45,200,25);label2.setBounds(10,80,90,25);txt2.setBounds(110,80,200,25);label3.setBounds(10,115,90,25);txt3.setBounds(110,115,200,25);dialog.add(p);btn0.setBounds(60,180,80,25);btn1.setBounds(175,180,80,25);btn2.setBounds(60,250,80,25);btn3.setBounds(175,250,
14、80,25);dialog.setSize(400,400);dialog.show();dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );public void actionPerformed(ActionEvent e)int size=list.size();/init();/list.add(b);/if(e.getSource()=btn0)/book b1=(book)list.get(size);/size-;/txt0.setText(b1.getid();/txt1.setText(b1.getname(
15、);/txt2.setText(Float.toString(b1.getprice();/txt3.setText(b1.getpublish();/if(e.getSource()=btn1)/book b2=(book)list.get(size);/size+;/txt0.setText(b2.getid();/txt1.setText(b2.getname();/txt2.setText(Float.toString(b2.getprice();/txt3.setText(b2.getpublish();/if(e.getSource()=btn2)String id=txt0.ge
16、tText();String name=txt1.getText();float price=getprice(txt2.getText();String publish=txt3.getText();Object str=id,name,price,publish;dbcon.insert(dbcon.getconnectin(),str);private float getprice(String str) float price=0; try price=Float.valueOf(str); catch(Exception ex) JOptionPane.showMessageDial
17、og(null,价格必要是数字,请重新输入!,错误,JOptionPane.WARNING_MESSAGE ); return price;/设定上一种,下一种按钮可用性/private void init()/if(list.size()=0)/btn0.setEnabled(false);/btn1.setEnabled(false);/else/btn0.setEnabled(true);/btn1.setEnabled(true);/3.searchdialog.java 查找模块package bookmanager;import javax.swing.*;import java.
18、awt.*;import java.awt.event.*;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.LinkedList;public class searchdialog implements ActionListener ,ItemListenerJRadioButton radio=new JRadioButton3;ButtonGroup group;String
19、 rtitle=图书编号,图书名称,出 版 社;String btitle= 拟定(Yes),取消(Esc);JTextField txtfield=new JTextField3;JButton btn=new JButton2;JDialog dialog;searchresult st;public searchdialog(JFrame f,int x,int y)/初始化单选按钮group=new ButtonGroup();st=new searchresult();for(int i=0;iradio.length;i+)radioi=new JRadioButton(rtitl
20、ei);radioi.addActionListener(this);radioi.setFont(new Font(宋体,Font.PLAIN ,15);radioi.addItemListener(this);group.add(radioi);txtfieldi=new JTextField();txtfieldi.setEditable(false);for(int i=0;i2;i+)btni=new JButton(btitlei);btni.addActionListener(this);btni.setFont(new Font(宋体,Font.PLAIN ,15);dialo
21、g =new JDialog(f,查询,true); /初始化radio0.setSelected(true);txtfield0.setEditable(true );dialog.setLocation(x,y);dialog.setLayout(null);/设定各个组件边界radio0.setBounds(10,10,90,25);txtfield0.setBounds(110,10,250,25);radio1.setBounds(10,45,90,25);txtfield1.setBounds(110,45,250,25);radio2.setBounds(10,80,90,25)
22、;txtfield2.setBounds(110,80,250,25);for(int i=0;iradio.length ;i+)dialog.add(radioi);dialog.add(txtfieldi);btn0.setBounds(20,135,120,25);btn1.setBounds(200,135,120,25);dialog.add(btn0);dialog.add(btn1);dialog.setSize(400,250);dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );public void acti
23、onPerformed(ActionEvent e)if(e.getSource()=btn0) st.init();Connection con=dbcon.getconnectin();PreparedStatement stmt=null;ResultSet rs=null;LinkedList list=new LinkedList();try if(radio0.isSelected()stmt=con.prepareStatement(select * from book where bid like %+txtfield0.getText().trim()+%);if(radio
24、1.isSelected()stmt=con.prepareStatement(select * from book where bname like %+txtfield1.getText().trim()+%);if(radio2.isSelected()stmt=con.prepareStatement(select * from book where bpublish like %+txtfield2.getText().trim()+%); rs=stmt.executeQuery(); while(rs.next() String id=rs.getString(1); Strin
25、g name=rs.getString(2); String price=rs.getString(3); String publish=rs.getString(4); book b=new book(id,name,Float.valueOf(price),publish); list.add(b); for(int i=0;ilist.size();i+) book b=(book)list.get(i); Object ob=b.getid(),b.getname(),b.getprice(),b.getpublish(); st.model.addRow(ob); JOptionPa
26、ne.showMessageDialog(null,总共找到+list.size()+条记录!,信息,JOptionPane.INFORMATION_MESSAGE ); st.show(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();public void itemStateChanged(ItemEvent e)for(int i=0;iradio.length;i+)if(radioi.isSelected()txtfieldi.setEditable(true);else
27、txtfieldi.setEditable(false);4.searchresult.java查找成果package bookmanager;import javax.swing.*;import javax.swing.table.DefaultTableModel;import java.awt.*;import java.awt.event.*;public class searchresult extends JFrame implements ActionListenerJTable table;DefaultTableModel model;String rtitle=图书编号,
28、图书名称,单价,出 版 社;JDialog dialog;public searchresult()super(查询成果);model=new DefaultTableModel(rtitle,0);table=new JTable(model);add(new JScrollPane(table),BorderLayout.CENTER );table.setRowHeight(25);table.setFont(new Font(楷体,Font.PLAIN ,18);setSize(600,400);public void init()model.setRowCount(0);public
29、 void actionPerformed(ActionEvent e)/后来扩展用,暂不实现任何功能*5.dbcon.java数据库连接类package bookmanager;import java.io.*;import .URL;import java.sql.*;import javax.swing.JOptionPane;public class dbcon public final static Connection getconnectin()Connection con=null;tryClass.forName(com.microsoft.sqlserver.jdbc.SQ
30、LServerDriver);String url=jdbc:sqlserver:/localhost:1433;integratedSecurity=true;con=DriverManager.getConnection(url);con.setCatalog(book);catch(ClassNotFoundException ex)ex.printStackTrace();catch(SQLException ex)ex.printStackTrace();return con;/添加数据public final static void insert(Connection con,Ob
31、ject obj)PreparedStatement stmt=null;try stmt=con.prepareStatement(insert into book values(?,?,?,?);stmt.setString(1,obj0.toString();stmt.setString(2,obj1.toString();stmt.setFloat(3,Float.valueOf(obj2.toString();stmt.setString(4,obj3.toString();stmt.execute();JOptionPane.showMessageDialog(null,添加成功!
32、,操作提示,JOptionPane.NO_OPTION ); catch (SQLException e) JOptionPane.showMessageDialog(null,添加数据时发生错误!,信息,JOptionPane.INFORMATION_MESSAGE );/查询数据public final static ResultSet query(Connection con,Object ob)ResultSet rs=null;PreparedStatement stmt=null;try stmt=con.prepareStatement(select * from book);
33、rs=stmt.executeQuery(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return rs;/删除数据public final static void delete(Connection con,Object ob)try PreparedStatement stmt; stmt=con.prepareStatement(delete from book where bid=?);stmt.setObject(1,ob);stmt.executeUpdate();JO
34、ptionPane.showMessageDialog(null,信息删除成功!,信息,JOptionPane.INFORMATION_MESSAGE ); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finally6.课本类 book.javapackage bookmanager;public class book private String id,name,publish;private float price;public book(String id,String name,float price,String publish)this.id=id;this.name=name;this.price =price;this.publish =publish; public String getid() return id; public String getname() return name; public float getprice() return price; public String getpublish() return publish;