圖書管理系統(tǒng)---java代碼_第1頁
圖書管理系統(tǒng)---java代碼_第2頁
圖書管理系統(tǒng)---java代碼_第3頁
圖書管理系統(tǒng)---java代碼_第4頁
圖書管理系統(tǒng)---java代碼_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、import java.awt.*; import java.awt.event.*;import java.sql.*;import java.util.*;import javax.swing.*;/ class Tsgl public static void main(String args) dbframe db = new dbframe(" 圖書管理程序 ");/ 圖書管理系統(tǒng)主界面class dbframe extends Frame implements ActionListener MenuBar daohang = new MenuBar(); / 建立

2、菜單欄Menu mfile = new Menu(" 功能 "); / 建立“功能”菜單組Menu mhelp = new Menu(" 幫助 "); / 建立“幫助”菜單組 MenuItem mdenglu = new MenuItem(" 登陸 ");MenuItem mchaxun = new MenuItem(" 查詢 ");MenuItem mtianjia = new MenuItem(" 添加 ");MenuItem mshanchu = new MenuItem(" 刪

3、除 ");MenuItem mexit = new MenuItem(" 退出 ");MenuItem mhelpp = new MenuItem(" 關于 ");Denglu pdenglu=new Denglu();Ptianjia ptianjia = new Ptianjia();Pmain pmain = new Pmain();Pchaxun pchaxun = new Pchaxun(); Pshanchu pshanchu = new Pshanchu();dbframe(String s) / 在窗口上添加菜單選項setTit

4、le(s); m(mdenglu); m(mtianjia); m(mchaxun); m(mshanchu); m(mexit); mhelp.add(mhelpp); daohang.add(mfile); daohang.add(mhelp); setMenuBar(daohang); add(pmain);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); setBounds(200, 200, 340, 250); setResizable(

5、false); setVisible(true); mexit.addActionListener(this); mdenglu.addActionListener(this); mtianjia.addActionListener(this); mchaxun.addActionListener(this); mshanchu.addActionListener(this); mhelpp.addActionListener(this); validate(); / 窗口事件監(jiān)聽public void actionPerformed(ActionEvent e) if (e.getSourc

6、e() = mexit) System.exit(0);if(e.getSource()=mdenglu) removeAll();add(pdenglu); validate();if (e.getSource() = mtianjia) removeAll(); add(ptianjia);validate(); / 圖書添加功能if (e.getSource() = mchaxun) removeAll();add(pchaxun);validate(); / 圖書查詢功能if (e.getSource() = mshanchu) removeAll();add(pshanchu);va

7、lidate(); / 圖書刪除功能if (e.getSource() = mhelpp) JOptionPane.showMessageDialog(this, " 歡迎使用圖書管理系統(tǒng) ", " 關于本系統(tǒng) ", JOptionPane.INFORMATION_MESSAGE); /各功能菜單事件監(jiān)聽/class Ptianjia extends Panel implements ActionListener TextField tname, tauthor, tpublish, tdate, tcomment;Label lname, lautho

8、r, lpublish, ldate, lcomment;Button btn;Ptianjia() setLayout(null);btn = new Button(" 添加"); / 創(chuàng)建“添加”按鈕 tname = new TextField();tauthor = new TextField(); tpublish = new TextField(); tdate = new TextField();tcomment = new TextField(); / 創(chuàng)建 5 個文本框Iname = new Label("書名”); lauthor = new L

9、abel(" 作者 "); lpublish = new Label(" 出版社 "); ldate = new Label(" 出版日期 "); lcomment = new Label(" 評論 "); add(lname); add(tname);add(lauthor); add(tauthor);add(lpublish);add(tpublish);add(ldate); add(tdate); add(lcomment);add(tcomment);add(btn); / 添加“按鈕”到窗口面板上 l

10、name.setBounds(10, 10, 70, 25); tname.setBounds(90, 10, 220, 25); lauthor.setBounds(10, 40, 70, 25); tauthor.setBounds(90, 40, 220, 25); lpublish.setBounds(10, 70, 70, 25); tpublish.setBounds(90, 70, 220, 25); ldate.setBounds(10, 100, 70, 25); tdate.setBounds(90, 100, 220, 25); lcomment.setBounds(10

11、, 130, 70, 25); tcomment.setBounds(90, 130, 220, 25); btn.setBounds(130, 160, 70, 25); / 設置按鈕位置 btn.addActionListener(this); / 設置按鈕監(jiān)聽 setSize(340, 250); / 設置窗口大小 setBackground(Color.white); / 設置窗口背景顏色 setVisible(true);validate();public void actionPerformed(ActionEvent e) String sname = tname.getText

12、();String sauthor = tauthor.getText();String spublish = tpublish.getText();String sdate = tdate.getText();String scomment = tcomment.getText();String insertstr = "insert into book values" + "(" + "'" + sname + "'"+ "," + "'" + s

13、author + "'" + "," + "'" + spublish + "'" + ","+ "'" + sdate + "'" + "," + "'" + scomment + "'" + ")" / SQL 語句 Connection con;Statement sta;ResultSet rs;try Class

14、.forNameC'oracle.jdbc.driver.OracleDriver"); /加載 JDBC驅動 catch (ClassNotFoundException ee) System.out.println("" + ee); try con = DriverManager.getConnection("jdbc:oracle:thin:localhost:1521:orcl", "sa", "orcl"); / 連接數(shù)據(jù)庫sta = con.createStatement();sta.

15、executeUpdate(insertstr); / 執(zhí)行 SQL語句con.close(); / 關閉數(shù)據(jù)庫 tname.setText("");tauthor.setText("");tpublish.setText("");tdate.setText("");tcomment.setText(""); / 重新初始化文本框內容JOptionPane.showMessageDialog(this, "添加成功 ", "圖書管理系統(tǒng) ",JOption

16、Pane.INFORMATION_MESSAGE); catch (SQLException eee) System.out.println("" + eee);tname.setText("");tauthor.setText("");tpublish.setText("");tdate.setText(""); tcomment.setText("");JOptionPane.showMessageDialog(this, "添加失敗 ", "

17、;圖書管理系統(tǒng) ",JOptionPane.WARNING_MESSAGE);class Pmain extends Panel / 窗口面板布局Label I1 = new Label("圖書管理系統(tǒng) ”,Label.CENTER);Label l2 = new Label(" 計算機學院 ", Label.RIGHT);Label l3 = new Label("", Label.RIGHT);Label 14 = new Label("作者:JHL", LabeI.RIGHT);Pmain() setLayo

18、ut(null);setBackground(Color.white);add(l1);add(l2);add(l3);add(l4);11. setBounds(10, 60, 320, 40);12. setBounds(240, 120, 80, 22);13. setBounds(240, 142, 80, 22);14. setBounds(240, 164, 80, 22); setSize(340, 250); setVisible(true);validate();class Pchaxun extends Panel implements ActionListener / 圖

19、書查詢 Choice cchaxun;TextField tchaxun;Button btnchaxun;TextArea tachaxun;Pchaxun() setLayout(null);cchaxun = new Choice(); / 創(chuàng)建下拉菜單cchaxun.add(” 書名”); cchaxun.add("作者"); cchaxun.add("出版社"); tchaxun = new TextField();btnchaxun = new Button(" 查詢"); / 創(chuàng)建按鈕 tachaxun = new Te

20、xtArea(); / 創(chuàng)建多行文本框 add(cchaxun);add(tchaxun); add(btnchaxun); add(tachaxun); tchaxun.setText("");cchaxun.setBounds(10, 10, 65, 20); tchaxun.setBounds(85, 10, 180, 20); btnchaxun.setBounds(275, 10, 40, 20); tachaxun.setBounds(10, 40, 305, 145); cchaxun.select(0); / 下拉菜單的初始選項 setSize(340, 2

21、50); / 設置窗口大小 setBackground(Color.white);setVisible(true);btnchaxun.addActionListener(this); validate();public void actionPerformed(ActionEvent e) int i = cchaxun.getSelectedIndex();String s = tchaxun.getText();空 ", " 查詢if (s.equals("")JOptionPane.showMessageDialog(this, "查詢

22、內容不能為系統(tǒng) ",JOptionPane.WARNING_MESSAGE);else Connection con;Statement sql;ResultSet rs;try Class.forName("oracle.jdbc.driver.OracleDriver"); / 加載 JDBC驅動 catch (ClassNotFoundException ee) System.out.println("" + ee);try con = DriverManager.getConnection("jdbc:oracle:thin:

23、localhost:1521:orcl","sa", "orcl");/連接數(shù)據(jù)庫sql = con.createStatement();if (i = 0) /根據(jù)下拉菜單的選項執(zhí)行不同的SQL語句rs = sql.executeQuery("select * from book where bookName ="+ "'" + s + "'");else if (i = 1)rs = sql.executeQuery("select * from book

24、 where author ="+ "'" + s + "'");elsers = sql.executeQuery("select * from book where publisher =" + "'" + s + "'");tachaxun.setText("");String panduan = "false"while (rs.next() panduan = "true"tachax

25、un.append(" 書名:" + rs.getString(1) + "n"); tachaxun.append(" 作者:" + rs.getString(2) + "n"); tachaxun.append(" 出版社:" + rs.getString(3) + "n"); tachaxun.append(" 出版日期 :" + rs.getString(4) + "n"); tachaxun.append(" 評論:

26、" + rs.getString(5) + "n"); / 顯示查詢到的圖書信息rs = sql.executeQuery("select count(*) from book"); / 統(tǒng)計所有圖書數(shù) while (rs.next() tachaxun.append(" 圖書館暫有 " + rs.getString(1) + "本圖書"); / 顯示所有圖書數(shù)量信息con.close(); / 關閉數(shù)據(jù)庫if (panduan.equals("false")JOptionPane.s

27、howMessageDialog(this, "沒有該記錄 ", "查詢系統(tǒng) ", JOptionPane.WARNING_MESSAGE); catch (SQLException eee) System.out.println(eee);class Pshanchu extends Panel implements ActionListener / 刪除功能模塊 Choice cshanchu;TextField tshanchu;Button btnshanchu;Label l1 = new Label("", Label.C

28、ENTER);Label l2 = new Label(" 計算機學院 ", Label.RIGHT);Label l3 = new Label("", Label.RIGHT);Label 14 = new Label("作者:ZCS", LabeI.RIGHT);Pshanchu() setLayout(null);cshanchu = new Choice(); / 創(chuàng)建下拉菜單cshanchu.add(” 書名");tshanchu = new TextField();btnshanchu = new Button(

29、" 刪除 "); / 創(chuàng)建按鈕 add(cshanchu);add(tshanchu);add(btnshanchu);add(l1);add(l2);add(l3);add(l4); / 添加標簽、按鈕等到窗口面板 tshanchu.setText("");cshanchu.setBounds(10, 10, 65, 20); tshanchu.setBounds(85, 10, 180, 20); btnshanchu.setBounds(275, 10, 40, 20);11. setBounds(10, 40, 300, 40);12. setB

30、ounds(240, 130, 80, 22);13. setBounds(240, 152, 80, 22);14. setBounds(240, 174, 80, 22); cshanchu.select(0);setSize(340, 250); / 設置窗口大小setBackground(Color.white); setVisible(true); btnshanchu.addActionListener(this);validate();public void actionPerformed(ActionEvent e) String s = tshanchu.getText();

31、if (s.equals("")JOptionPane.showMessageDialog(this, " 請輸入要刪除的圖書 ", " 刪除功能 ", JOptionPane.WARNING_MESSAGE);else Connection con;Statement sql;ResultSet rs;try Class.forName("oracle.jdbc.driver.OracleDriver"); catch (ClassNotFoundException ee) System.out.println(

32、"" + ee);try con = DriverManager.getConnection("jdbc:oracle:thin:localhost:1521:orcl","sa", "orcl");sql = con.createStatement();rs = sql.executeQuery("select * from book where bookName ="+ "'" + s + "'");if (!rs.next()JOpt

33、ionPane.showMessageDialog(this, "沒有該圖書 ", "刪除功能 ", JOptionPane.WARNING_MESSAGE);else int n = JOptionPane.showConfirmDialog(this, " 確認刪除嗎 ?", "刪除功能 ", JOptionPane.YES_NO_OPTION);if (n = JOptionPane.YES_OPTION) sql.executeUpdate("delete from book where bookName ="+ "'" + s + "'"); JOptionPane.showMessageDialog(this, " 刪除成功 ", " 刪除功能 ", JOptionPane.INFORMATION_MESSAGE); tshanchu.setText(""); else tshanchu.setText("");con.close(); catch (SQLException ee

溫馨提示

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

評論

0/150

提交評論