《图书管理系统---java代码(共11页).doc》由会员分享,可在线阅读,更多相关《图书管理系统---java代码(共11页).doc(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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(图书管理程序);/ 图书管理系统主界面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(删除);MenuItem mexit = new MenuItem(退出);MenuItem mhelpp = new MenuItem(关于);Denglu
3、pdenglu=new Denglu();Ptianjia ptianjia = new Ptianjia();Pmain pmain = new Pmain();Pchaxun pchaxun = new Pchaxun();Pshanchu pshanchu = new Pshanchu();dbframe(String s) / 在窗口上添加菜单选项setTitle(s);mfile.add(mdenglu);mfile.add(mtianjia);mfile.add(mchaxun);mfile.add(mshanchu);mfile.add(mexit);mhelp.add(mhel
4、pp);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(false);setVisible(true);mexit.addActionListener(this);mdenglu.addActionListener(this);mt
5、ianjia.addActionListener(this);mchaxun.addActionListener(this);mshanchu.addActionListener(this);mhelpp.addActionListener(this);validate(); / 窗口事件监听public void actionPerformed(ActionEvent e) if (e.getSource() = mexit)System.exit(0);if(e.getSource()=mdenglu)removeAll();add(pdenglu);validate();if (e.ge
6、tSource() = mtianjia) removeAll();add(ptianjia);validate(); / 图书添加功能if (e.getSource() = mchaxun) removeAll();add(pchaxun);validate(); / 图书查询功能if (e.getSource() = mshanchu) removeAll();add(pshanchu);validate(); / 图书删除功能if (e.getSource() = mhelpp) JOptionPane.showMessageDialog(this, 欢迎使用图书管理系统, 关于本系统,
7、JOptionPane.INFORMATION_MESSAGE); / 各功能菜单事件监听/class Ptianjia extends Panel implements ActionListener TextField tname, tauthor, tpublish, tdate, tcomment;Label lname, lauthor, lpublish, ldate, lcomment;Button btn;Ptianjia() setLayout(null);btn = new Button(添加); / 创建“添加”按钮tname = new TextField();tauth
8、or = new TextField();tpublish = new TextField();tdate = new TextField();tcomment = new TextField(); / 创建5个文本框lname = new Label(书名);lauthor = new Label(作者);lpublish = new Label(出版社);ldate = new Label(出版日期);lcomment = new Label(评论);add(lname);add(tname);add(lauthor);add(tauthor);add(lpublish);add(tpub
9、lish);add(ldate);add(tdate);add(lcomment);add(tcomment);add(btn); / 添加“按钮”到窗口面板上lname.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、(10, 100, 70, 25);tdate.setBounds(90, 100, 220, 25);lcomment.setBounds(10, 130, 70, 25);tcomment.setBounds(90, 130, 220, 25);btn.setBounds(130, 160, 70, 25); / 设置按钮位置btn.addActionListener(this); / 设置按钮监听setSize(340, 250); / 设置窗口大小setBackground(Color.white); / 设置窗口背景颜色setVisible(true);validate(); pub
11、lic void actionPerformed(ActionEvent e) String sname = tname.getText();String sauthor = tauthor.getText();String spublish = tpublish.getText();String sdate = tdate.getText();String scomment = tcomment.getText();String insertstr = insert into book values + ( + + sname + + , + + sauthor + + , + + spub
12、lish + + ,+ + sdate + + , + + scomment + + ); / SQL语句Connection con;Statement sta;ResultSet rs;try Class.forName(oracle.jdbc.driver.OracleDriver); / 加载JDBC驱动 catch (ClassNotFoundException ee) System.out.println( + ee);try con = DriverManager.getConnection(jdbc:oracle:thin:localhost:1521:orcl, sa, or
13、cl); / 连接数据库sta = con.createStatement();sta.executeUpdate(insertstr); / 执行SQL语句con.close(); / 关闭数据库tname.setText();tauthor.setText();tpublish.setText();tdate.setText();tcomment.setText(); / 重新初始化文本框内容JOptionPane.showMessageDialog(this, 添加成功, 图书管理系统,JOptionPane.INFORMATION_MESSAGE); catch (SQLExcepti
14、on eee) System.out.println( + eee);tname.setText();tauthor.setText();tpublish.setText();tdate.setText();tcomment.setText();JOptionPane.showMessageDialog(this, 添加失败, 图书管理系统,JOptionPane.WARNING_MESSAGE);class Pmain extends Panel / 窗口面板布局Label l1 = new Label(图书管理系统, Label.CENTER);Label l2 = new Label(计
15、算机学院, Label.RIGHT);Label l3 = new Label(, Label.RIGHT);Label l4 = new Label(作者:JHL, Label.RIGHT);Pmain() setLayout(null);setBackground(Color.white);add(l1);add(l2);add(l3);add(l4);l1.setBounds(10, 60, 320, 40);l2.setBounds(240, 120, 80, 22);l3.setBounds(240, 142, 80, 22);l4.setBounds(240, 164, 80, 2
16、2);setSize(340, 250);setVisible(true);validate();class Pchaxun extends Panel implements ActionListener / 图书查询Choice cchaxun;TextField tchaxun;Button btnchaxun;TextArea tachaxun;Pchaxun() setLayout(null);cchaxun = new Choice(); / 创建下拉菜单cchaxun.add(书名);cchaxun.add(作者);cchaxun.add(出版社);tchaxun = new Te
17、xtField();btnchaxun = new Button(查询); / 创建按钮tachaxun = new TextArea(); / 创建多行文本框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);ccha
18、xun.select(0); / 下拉菜单的初始选项setSize(340, 250); / 设置窗口大小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, 查
19、询内容不能为 空, 查询系统,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:localhost:1521:orcl, sa, orcl);
20、/ 连接数据库sql = con.createStatement();if (i = 0) / 根据下拉菜单的选项执行不同的SQL语句rs = sql.executeQuery(select * from book where bookName =+ + s + );else if (i = 1)rs = sql.executeQuery(select * from book where author =+ + s + );elsers = sql.executeQuery(select * from book where publisher =+ + s + );tachaxun.setTe
21、xt();String panduan = false;while (rs.next() panduan = true;tachaxun.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(评论: + rs.getString(5) + n); / 显示查询到的图书信息rs = sql.
22、executeQuery(select count(*) from book); / 统计所有图书数while (rs.next() tachaxun.append(图书馆暂有 + rs.getString(1) + 本图书); / 显示所有图书数量信息con.close(); / 关闭数据库if (panduan.equals(false)JOptionPane.showMessageDialog(this, 没有该记录, 查询系统,JOptionPane.WARNING_MESSAGE); catch (SQLException eee) System.out.println(eee);c
23、lass Pshanchu extends Panel implements ActionListener / 删除功能模块Choice cshanchu;TextField tshanchu;Button btnshanchu;Label l1 = new Label(, Label.CENTER);Label l2 = new Label(计算机学院, Label.RIGHT);Label l3 = new Label(, Label.RIGHT);Label l4 = new Label(作者:ZCS, Label.RIGHT);Pshanchu() setLayout(null);cs
24、hanchu = new Choice(); / 创建下拉菜单cshanchu.add(书名);tshanchu = new TextField();btnshanchu = new Button(删除); / 创建按钮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);btnshanc
25、hu.setBounds(275, 10, 40, 20);l1.setBounds(10, 40, 300, 40);l2.setBounds(240, 130, 80, 22);l3.setBounds(240, 152, 80, 22);l4.setBounds(240, 174, 80, 22);cshanchu.select(0);setSize(340, 250); / 设置窗口大小setBackground(Color.white);setVisible(true);btnshanchu.addActionListener(this);validate();public void
26、 actionPerformed(ActionEvent e) String s = tshanchu.getText();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
27、.println( + 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()JOptionPane.showMessageDialog(this, 没有该图书, 删除功能,JOptionPane.WARNING_MESSAGE);else int n =
28、 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(); elsetshanchu.setText();con.clo
29、se(); catch (SQLException eee) System.out.println(eee);class Denglu extends Panel implements ActionListener Label lzhanghao;TextField tzhanghao;Label lmima;TextField tmima;Button bdenglu;Denglu()setLayout(null);lzhanghao=new Label(账号);tzhanghao=new TextField();lmima=new Label(密码);tmima=new TextField
30、();bdenglu=new Button(登陆);add(lzhanghao);add(tzhanghao);add(lmima);add(tmima);add(bdenglu);tzhanghao.setText();tmima.setText();lzhanghao.setBounds(50, 25, 50, 30);tzhanghao.setBounds(100, 30, 200, 20);lmima.setBounds(50, 80, 50, 30);tmima.setBounds(100, 80, 200, 20);bdenglu.setBounds(140, 130, 50, 3
31、0);setBackground(Color.white);setVisible(true);bdenglu.addActionListener(this);validate();public void actionPerformed(ActionEvent e) String x=tzhanghao.getText();String y=tmima.getText();if(x.equals(weifang)&y.equals(123) )JOptionPane.showMessageDialog(this, 登陆成功, 提示,JOptionPane.INFORMATION_MESSAGE);elseJOptionPane.showMessageDialog(this, 账号或者密码错误,提示,JOptionPane.WARNING_MESSAGE);专心-专注-专业