图书管理系统---java代码.doc_第1页
图书管理系统---java代码.doc_第2页
图书管理系统---java代码.doc_第3页
免费预览已结束,剩余7页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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(); / 建立菜单栏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 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(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(false);setVisible(true);mexit.addActionListener(this);mdenglu.addActionListener(this);mtianjia.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.getSource() = 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, 欢迎使用图书管理系统, 关于本系统,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();tauthor = 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(tpublish);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, 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(); public 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 + + , + + spublish + + ,+ + 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, orcl); / 连接数据库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 (SQLException 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(计算机学院, 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, 22);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 TextField();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);cchaxun.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, 查询内容不能为 空, 查询系统,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);/ 连接数据库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.setText();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.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);class 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);cshanchu = 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);btnshanchu.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 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.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 = 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.close(); catch (SQLException eee) System.out.prin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论