簡(jiǎn)易圖書管理系統(tǒng)_第1頁(yè)
簡(jiǎn)易圖書管理系統(tǒng)_第2頁(yè)
簡(jiǎn)易圖書管理系統(tǒng)_第3頁(yè)
簡(jiǎn)易圖書管理系統(tǒng)_第4頁(yè)
簡(jiǎn)易圖書管理系統(tǒng)_第5頁(yè)
已閱讀5頁(yè),還剩9頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、本程序以java為設(shè)計(jì)語(yǔ)言,能夠?qū)崿F(xiàn)一個(gè)簡(jiǎn)單的圖書管理系統(tǒng),可以實(shí)現(xiàn)簡(jiǎn)單的增加圖書,查詢圖書,刪除圖書的功能,其中用到數(shù)據(jù)庫(kù)的連接,窗體的布局管理。本程序有以下幾個(gè)功能函數(shù)組成所有模塊都在名為bookmanager的包中1:主函數(shù)mainframe.java2:增加模塊 adddialog.java3:查詢模塊 searchdialog.java4:查詢結(jié)果模塊 searchresult.java5:書本類book.java6:數(shù)據(jù)庫(kù)連接類 dbcon.java1. 主界面2. 添加模塊3. 查詢模塊4. 輸出模塊5.6.源程序如下1.mainframe.javapackage bookman

2、ager;import 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 impleme

3、nts ActionListenerprivate JToolBar tb;String btntitle="增加","查詢","刪除","退出"String tabeltitle="圖書編號(hào)","書名","定價(jià)","出版社"JButton btn=new JButton4;JTable table; DefaultTableModel model;public mainframe()super("圖書管理系統(tǒng)");

4、/初始化各個(gè)按鈕tb=new JToolBar("工具欄");for(int i=0;i<btntitle.length;i+)btni=new JButton(btntitlei);btni.addActionListener(this);tb.add(btni);/創(chuàng)建表模型model=new DefaultTableModel(tabeltitle,10);table=new JTable(model);/設(shè)定單元格的高度table.setRowHeight(25);add(tb,BorderLayout.NORTH );add(new JScrollPane(

5、table),BorderLayout.CENTER );setSize(900,600);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );setLocation(100,30);init();public void 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 sea

6、rchdialog(m,25,25);sd.dialog.setVisible(true);init();if(o=btn2)int result=JOptionPane.showConfirmDialog(null,"確實(shí)要?jiǎng)h除該條圖書信息嗎?","確認(rèn)",JOptionPane.YES_NO_OPTION ,JOptionPane.QUESTION_MESSAGE );if(result=0)Connection con=dbcon.getconnectin();Object ob=model.getValueAt(table.getSelected

7、Row(),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 PreparedStatement stmt=con.prepareStatement("select * from book");ResultSet rs=stmt.executeQuery(); list=new LinkedList();while(rs.next(

8、)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),publish);list.add(b);model.setRowCount(0);for(int i=0;i<list.size();i+)book b=(book)list.get(i);Object str=b.getid(),b.getname(),b.getpri

9、ce(),b.getpublish();model.addRow(str); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();public static void main(String args)new mainframe().show();*2.Adddialog.java添加模塊package bookmanager;import java.awt.*;import java.awt.event.*;import java.util.LinkedList;import javax.sw

10、ing.*;public class adddialog implements ActionListener JLabel label=new JLabel4;JTextField txt=new JTextField4;String ltitle="圖書編號(hào)","圖書名稱","單 價(jià)","出 版 社"String btitle="上一個(gè)","下一個(gè)","確 定","取 消"JButton btn=new JButton4;JDialo

11、g 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("請(qǐng)選擇操作");/p.setBorder(BorderFactory.createLineBorder(Color.red );/p.

12、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;i<ltitle.length ;i+)labeli=new JLabel(ltitlei);labeli.setFont(new Font("宋體",Font.PLAIN ,18);txti=new JT

13、extField();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(1

14、10,10,200,25);label1.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);bt

15、n3.setBounds(175,250,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();/txt

16、1.setText(b1.getname();/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()=b

17、tn2)String id=txt0.getText();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) JOpti

18、onPane.showMessageDialog(null,"價(jià)格必須是數(shù)字,請(qǐng)重新輸入!","錯(cuò)誤",JOptionPane.WARNING_MESSAGE ); return price;/設(shè)定上一個(gè),下一個(gè)按鈕的可用性/private void init()/if(list.size()=0)/btn0.setEnabled(false);/btn1.setEnabled(false);/else/btn0.setEnabled(true);/btn1.setEnabled(true);/3.searchdialog.java 查找模塊packag

19、e bookmanager;import javax.swing.*;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 searchdialog implements ActionListener ,ItemListenerJRadioButton r

20、adio=new JRadioButton3;ButtonGroup group;String rtitle="圖書編號(hào)","圖書名稱","出 版 社"String btitle=" 確定(Yes)","取消(Esc)"JTextField txtfield=new JTextField3;JButton btn=new JButton2;JDialog dialog;searchresult st;public searchdialog(JFrame f,int x,int y)/初始化單選按

21、鈕group=new ButtonGroup();st=new searchresult();for(int i=0;i<radio.length;i+)radioi=new JRadioButton(rtitlei);radioi.addActionListener(this);radioi.setFont(new Font("宋體",Font.PLAIN ,15);radioi.addItemListener(this);group.add(radioi);txtfieldi=new JTextField();txtfieldi.setEditable(false

22、);for(int i=0;i<2;i+)btni=new JButton(btitlei);btni.addActionListener(this);btni.setFont(new Font("宋體",Font.PLAIN ,15);dialog =new JDialog(f,"查詢",true); /初始化radio0.setSelected(true);txtfield0.setEditable(true );dialog.setLocation(x,y);dialog.setLayout(null);/設(shè)定各個(gè)組件的邊界radio0.se

23、tBounds(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);txtfield2.setBounds(110,80,250,25);for(int i=0;i<radio.length ;i+)dialog.add(radioi);dialog.add(txtfieldi);btn0.setBounds(20,135,120,25);btn1.setB

24、ounds(200,135,120,25);dialog.add(btn0);dialog.add(btn1);dialog.setSize(400,250);dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );public void actionPerformed(ActionEvent e)if(e.getSource()=btn0) st.init();Connection con=dbcon.getconnectin();PreparedStatement stmt=null;ResultSet rs=null;Linke

25、dList list=new LinkedList();try if(radio0.isSelected()stmt=con.prepareStatement("select * from book where bid like '%"+txtfield0.getText().trim()+"%'");if(radio1.isSelected()stmt=con.prepareStatement("select * from book where bname like '%"+txtfield1.getText

26、().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); String name=rs.getString(2); String price=rs.getS

27、tring(3); String publish=rs.getString(4); book b=new book(id,name,Float.valueOf(price),publish); list.add(b); for(int i=0;i<list.size();i+) book b=(book)list.get(i); Object ob=b.getid(),b.getname(),b.getprice(),b.getpublish(); st.model.addRow(ob); JOptionPane.showMessageDialog(null,"總共找到&quo

28、t;+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;i<radio.length;i+)if(radioi.isSelected()txtfieldi.setEditable(true);else

29、txtfieldi.setEditable(false);4.searchresult.java查找結(jié)果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="

30、;圖書編號(hào)","圖書名稱","單價(jià)","出 版 社"JDialog dialog;public searchresult()super("查詢結(jié)果");model=new DefaultTableModel(rtitle,0);table=new JTable(model);add(new JScrollPane(table),BorderLayout.CENTER );table.setRowHeight(25);table.setFont(new Font("楷體",Font.PL

31、AIN ,18);setSize(600,400);public void init()model.setRowCount(0);public void actionPerformed(ActionEvent e)/以后擴(kuò)展用,暫不實(shí)現(xiàn)任何功能*5.dbcon.java數(shù)據(jù)庫(kù)連接類package bookmanager;import java.io.*;import .URL;import java.sql.*;import javax.swing.JOptionPane;public class dbcon public final static Connection getconnecti

32、n()Connection con=null;tryClass.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");String url="jdbc:sqlserver:/localhost:1433;integratedSecurity=true;"con=DriverManager.getConnection(url);con.setCatalog("book");catch(ClassNotFoundException ex)ex.printStackTrace();c

33、atch(SQLException ex)ex.printStackTrace();return con;/添加數(shù)據(jù)public final static void insert(Connection con,Object 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,"添加成功!","操作提示",JOptionPane.NO_OPTION ); catch (SQLException e) JOptionPane.showMessageDialog(null,&q

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論