📄 book.java
字号:
//图书入库
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.text.*;
import java.math.*;
public class Book implements ActionListener
{public JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7;
public JTextField jtfd1,jtfd2,jtfd3,jtfd5,jtfd6,
jtfd7,jtfd9,jtfd11,jtfd12,jtfd13,jtfd14;
public JComboBox jtfd4,jtfd8,jtfd10;
public JTextArea jta;
public String str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14,str15;
public Connection con;
public JFrame frame;
public BookInfomation book;
public Book(BookInfomation book)
{JFrame.setDefaultLookAndFeelDecorated(true);
frame=new JFrame("图书入库");
ImageIcon icon=new ImageIcon("images/home.gif");
frame.setIconImage(icon.getImage());
frame.setResizable(false);
Font font1=new Font("宋体",Font.PLAIN,13);
Container content=frame.getContentPane();
Toolkit tool=frame.getToolkit();
Dimension wndsize=tool.getScreenSize();
this.book=book;
//--初始化各个组件--
JLabel lb0=new JLabel("图书名称:");
JLabel lb1=new JLabel("图书编号:");
JLabel lb2=new JLabel("条形编码:");
JLabel lb3=new JLabel("分类方法:");
JLabel lb4=new JLabel("图书作者:");
JLabel lb5=new JLabel("图书类型:");
JLabel lb6=new JLabel("图书价格:");
JLabel lb7=new JLabel("出版社名:");
JLabel lb8=new JLabel("图书页码:");
JLabel lb9=new JLabel("书架名称:");
JLabel lb10=new JLabel("出版日期:");
JLabel lb11=new JLabel("入库日期:");
JLabel lb12=new JLabel("库存数量:");
JLabel lb13=new JLabel("操 作 员:");
JLabel lb14=new JLabel("图书摘要:");
jtfd1=new JTextField();
jtfd2=new JTextField();
jtfd3=new JTextField();
jtfd4=new JComboBox();
try{ View();}
catch(SQLException eg){}
jtfd5=new JTextField();
jtfd6=new JTextField();
jtfd7=new JTextField();
jtfd8=new JComboBox();
try{ View1();}
catch(SQLException eg){}
jtfd9=new JTextField();
jtfd10=new JComboBox();
try{ View2();}
catch(SQLException eg){}
jtfd11=new JTextField();
jtfd12=new JTextField();
jtfd13=new JTextField("1");
jtfd14=new JTextField();
jta=new JTextArea();
jta.setLineWrap(true);
JScrollPane js=new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
btn1=new JButton("入 库");
btn2=new JButton("出 库");
btn7=new JButton("出 库");
btn5=new JButton("更 新");
btn6=new JButton("更 新");
btn3=new JButton("撤 消");
btn4=new JButton("退 出");
jtfd13.setEnabled(false);
btn5.setVisible(false);
btn6.setVisible(true);
btn2.setVisible(false);
btn7.setVisible(true);
//--设置各个组件的大小和位置--
lb0.setBounds(10,10,60,25);
jtfd1.setBounds(80,10,150,25);
lb1.setBounds(240,10,60,25);
jtfd2.setBounds(310,10,150,25);
lb2.setBounds(10,40,60,25);
jtfd3.setBounds(80,40,150,25);
lb3.setBounds(240,40,60,25);
jtfd6.setBounds(310,40,150,25);
lb4.setBounds(10,70,60,25);
jtfd5.setBounds(80,70,150,25);
lb5.setBounds(240,70,60,25);
jtfd4.setBounds(310,70,150,25);
lb6.setBounds(10,100,60,25);
jtfd7.setBounds(80,100,150,25);
lb7.setBounds(240,100,60,25);
jtfd8.setBounds(310,100,150,25);
lb8.setBounds(10,130,60,25);
jtfd9.setBounds(80,130,150,25);
lb9.setBounds(240,130,60,25);
jtfd10.setBounds(310,130,150,25);
lb10.setBounds(10,160,60,25);
jtfd11.setBounds(80,160,150,25);
lb11.setBounds(240,160,60,25);
jtfd12.setBounds(310,160,150,25);
lb12.setBounds(10,190,60,25);
jtfd13.setBounds(80,190,150,25);
lb13.setBounds(240,190,60,25);
jtfd14.setBounds(310,190,150,25);
lb14.setBounds(10,220,60,25);
js.setBounds(80,225,380,120);
btn1.setBounds(15,365,80,25);
btn2.setBounds(105,365,80,25);
btn7.setBounds(105,365,80,25);
btn5.setBounds(195,365,80,25);
btn6.setBounds(195,365,80,25);
btn3.setBounds(285,365,80,25);
btn4.setBounds(375,365,80,25);
//--设置各个组件的字体--
lb0.setFont(font1);
jtfd1.setFont(font1);
lb1.setFont(font1);
jtfd2.setFont(font1);
lb2.setFont(font1);
jtfd3.setFont(font1);
lb3.setFont(font1);
jtfd4.setFont(font1);
lb4.setFont(font1);
jtfd5.setFont(font1);
lb5.setFont(font1);
jtfd6.setFont(font1);
lb6.setFont(font1);
jtfd7.setFont(font1);
lb7.setFont(font1);
jtfd8.setFont(font1);
lb8.setFont(font1);
jtfd9.setFont(font1);
lb9.setFont(font1);
jtfd10.setFont(font1);
lb10.setFont(font1);
jtfd11.setFont(font1);
lb11.setFont(font1);
jtfd12.setFont(font1);
lb12.setFont(font1);
jtfd13.setFont(font1);
lb13.setFont(font1);
jtfd14.setFont(font1);
lb14.setFont(font1);
jta.setFont(font1);
btn1.setFont(font1);
btn2.setFont(font1);
btn3.setFont(font1);
btn4.setFont(font1);
btn5.setFont(font1);
btn6.setFont(font1);
btn7.setFont(font1);
//--设置各个组件的事件--
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
//--添加各个组件--
content.setLayout(null);
content.add(lb0);
content.add(jtfd1);
content.add(lb1);
content.add(jtfd2);
content.add(lb2);
content.add(jtfd3);
content.add(lb3);
content.add(jtfd6);
content.add(lb4);
content.add(jtfd5);
content.add(lb5);
content.add(jtfd4);
content.add(lb6);
content.add(jtfd7);
content.add(lb7);
content.add(jtfd8);
content.add(lb8);
content.add(jtfd9);
content.add(lb9);
content.add(jtfd10);
content.add(lb10);
content.add(jtfd11);
content.add(lb11);
content.add(jtfd12);
content.add(lb12);
content.add(jtfd13);
content.add(lb13);
content.add(jtfd14);
content.add(lb14);
content.add(js);
content.add(btn1);
content.add(btn2);
content.add(btn7);
content.add(btn5);
content.add(btn6);
content.add(btn3);
content.add(btn4);
content.setBounds(0,0,480,435);
frame.setBounds(300,200,480,435);
frame.setResizable(false);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{str1=jtfd1.getText().trim();
str2=jtfd2.getText().trim();
str3=jtfd3.getText().trim();
str4=(String)jtfd4.getSelectedItem();
str5=jtfd5.getText().trim();
str6=jtfd6.getText().trim();
str7=jtfd7.getText().trim();
str8=(String)jtfd8.getSelectedItem();
str9=jtfd9.getText().trim();
str10=(String)jtfd10.getSelectedItem();
str11=jtfd11.getText().trim();
str12=jtfd12.getText().trim();
str13=jtfd13.getText().trim();
str14=jtfd14.getText().trim();
str15=jta.getText().trim();
Object obj=(JButton)e.getSource();
if(obj==btn1)//图书入库按钮
{ try{ Add();}
catch(SQLException eg){}
}
if(obj==btn5)//图书更新按钮
{ try{ Update1();}
catch(SQLException eg){}
}
if(obj==btn6)
{ new Update(this);}
if(obj==btn2)//图书删除按钮
{ try{ Delete();}
catch(SQLException eg){}
}
if(obj==btn7)
{ new DeleteBook(this);}
if(obj==btn4)
{
frame.dispose();
}
}
////////////////////////////////查询图书类型设置信息代码///////////////////////////////////////////////
public void View() throws SQLException
{
try{
String strurl =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DataBase/Library.mdb";
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (ClassNotFoundException eg) {}
con= DriverManager.getConnection(strurl);
Statement stmt= con.createStatement();
ResultSet rs = stmt.executeQuery("select * from 图书类型设置 ORDER BY 编号");
while(rs.next())
{String bianhao=rs.getString(1);
String typename=rs.getString(2);
jtfd4.addItem(typename);
}
con.close();
}
catch(Exception es)
{es.printStackTrace();}
}
////////////////////////////////查询出版社设置信息代码///////////////////////////////////////////////
public void View1() throws SQLException
{
try{
String strurl =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DataBase/Library.mdb";
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (ClassNotFoundException eg) {}
con= DriverManager.getConnection(strurl);
Statement stmt= con.createStatement();
ResultSet rs = stmt.executeQuery("select * from 出版设置 ORDER BY 编号");
while(rs.next())
{String bianhao=rs.getString(1);
String typename=rs.getString(2);
jtfd8.addItem(typename);
}
con.close();
}
catch(Exception es)
{es.printStackTrace();}
}
////////////////////////////////查询书架设置信息代码///////////////////////////////////////////////
public void View2() throws SQLException
{
try{
String strurl =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DataBase/Library.mdb";
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (ClassNotFoundException eg) {}
con= DriverManager.getConnection(strurl);
Statement stmt= con.createStatement();
ResultSet rs = stmt.executeQuery("select * from 书架设置 ORDER BY 书架编号");
while(rs.next())
{String bianhao=rs.getString(1);
String typename=rs.getString(2);
jtfd10.addItem(typename);
}
con.close();
}
catch(Exception es)
{es.printStackTrace();}
}
////////////////////////////////添加图书代码///////////////////////////////////////////////
public void Add() throws SQLException
{ String status="在架",bc="0",count="1";
String strurl =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DataBase/Library.mdb";
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (ClassNotFoundException es) {}
con= DriverManager.getConnection(strurl);
Statement stmt = con.createStatement();
if(str1.equals("")|str2.equals("")|str3.equals("")|str4.equals("")|str5.equals(""))
{ JOptionPane.showMessageDialog(frame,"各项不能为空!");
return;
}
else if(str6.equals("")|str7.equals("")|str8.equals("")|str9.equals("")|str10.equals(""))
{ JOptionPane.showMessageDialog(frame,"各项不能为空!");
return;
}
else if(str11.equals("")|str12.equals("")|str13.equals("")|str14.equals("")|str15.equals(""))
{ JOptionPane.showMessageDialog(frame,"各项不能为空!");
return;
}
else { ResultSet rs = stmt.executeQuery("select * from 图书资料 where 编号='" + str2 + "'OR 条形码='"+str3+"'");
if(rs.next())
{ JOptionPane.showMessageDialog(null, "此编号图书或条形码已存在,请您重新输入!", "警告",JOptionPane.WARNING_MESSAGE);
jtfd2.setText("");
jtfd3.setText("");
}
else
{ stmt.executeUpdate("INSERT INTO 图书资料(图书名称,编号,条形码,分类法,作者,类型,价格,出版社,页码,书架名称,出版日期,入库日期,库存量,借阅次数,图书状态,操作员,摘要) values ('"+str1+"','"+str2+"','"+str3+"','"+str4+"','"+str5+"','"+str6+"','"+str7+"','"+str8+"','"+str9+"','"+str10+"','"+str11+"','"+str12+"','"+str13+"','"+bc+"','"+status+"','"+str14+"','"+str15+"')");
JOptionPane.showMessageDialog(null, "恭喜您添加新图书成功!");
try{ book.setnull();}
catch(SQLException ed){}
try{ book.view();}
catch(SQLException ed){}
}
}
con.close();
}
////////////////////////////////更新图书代码///////////////////////////////////////////////
public void Update1() throws SQLException
{
String strurl =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DataBase/Library.mdb";
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (ClassNotFoundException es) {}
con= DriverManager.getConnection(strurl);
Statement stmt = con.createStatement();
if(str1.equals("")|str2.equals("")|str3.equals("")|str4.equals("")|str5.equals(""))
{ JOptionPane.showMessageDialog(frame,"各项不能为空!");
return;
}
else if(str6.equals("")|str7.equals("")|str8.equals("")|str9.equals("")|str10.equals(""))
{ JOptionPane.showMessageDialog(frame,"各项不能为空!");
return;
}
else if(str11.equals("")|str12.equals("")|str13.equals("")|str14.equals("")|str15.equals(""))
{ JOptionPane.showMessageDialog(frame,"各项不能为空!");
return;
}
else { ResultSet rs = stmt.executeQuery("select * from 图书资料 ");
if(rs.next())
{
stmt.executeUpdate("update 图书资料 set 图书名称='"+str1+"',分类法='"+str4+"',作者='"+str5+"',类型='"+str6+"',价格='"+str7+"',出版社='"+str8+"',页码='"+str9+"',书架名称='"+str10+"',出版日期='"+str11+"',入库日期='"+str12+"',库存量='"+str13+"',操作员='"+str14+"',摘要='"+str15+"'where 条形码='"+str3+"'");
JOptionPane.showMessageDialog(null, "恭喜您更新图书资料成功!");
try{ book.setnull();}
catch(SQLException ed){}
try{ book.view();}
catch(SQLException ed){}
btn6.setVisible(true);
btn5.setVisible(false);
}
}
con.close();
}
////////////////////////////////删除图书设置信息///////////////////////////////////////////////
public void Delete() throws SQLException
{
String strurl =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DataBase/Library.mdb";
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (ClassNotFoundException es) {}
con= DriverManager.getConnection(strurl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from 图书资料 ");
if(rs.next())
{if(JOptionPane.showConfirmDialog(book, "确实要删除该图书的所有信息吗?\n删除的信息将不能恢复,是否继续?","删除确定", JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == 0)
{stmt.executeUpdate("delete from 图书资料 where 条形码='"+str3+"'");
JOptionPane.showMessageDialog(null, "图书资料删除成功!");
}
try{ book.setnull();}
catch(SQLException ed){}
try{ book.view();}
catch(SQLException ed){}
}
con.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -