📄 library.java
字号:
}
else if(NewBookAuthorField.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null,"新作者不能为空");
}
else if(NewBookPriceField.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null,"新书籍价格不能为空");
}
else if(NewBookStatusField.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null,"新书籍状态不能为空");
}
else
{
try{Updatebook();}
catch(SQLException ee){
System.out.println("SQLException:"+ee.getMessage());}
}}}
public void Showinfo()throws SQLException
{
String Bno,Bname,Bpress,Bauthor,Bprice,Bstatus;
int i=0;
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){
System.out.println("ForName:"+e.getMessage());}
String conURL="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};"+"DBQ=library.mdb";
Connection ExlCon=DriverManager.getConnection(conURL);
Statement Exlstmt=ExlCon.createStatement();
ResultSet rs=Exlstmt.executeQuery("SELECT * FROM book");
while (rs.next())
{
Bno=rs.getString("book_no");
Bname=rs.getString("bookname");
Bpress=rs.getString("press");
Bauthor=rs.getString("author");
Bprice=rs.getString("price");
Bstatus=rs.getString("status");
if(Bno.equals(BooknoField.getText().trim()))
{
BooknoField.setText(Bno);
BookNameField.setText(Bname);
BookPressField.setText(Bpress);
BookAuthorField.setText(Bauthor);
BookPriceField.setText(Bprice);
BookStatusField.setText(Bstatus);
i=1;
break;
}
}
ExlCon.close();
if (i==0)
{
JOptionPane.showMessageDialog(null,"不存在该书号对应的书籍档案!");
}
}
public void Updatebook()throws SQLException
{try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){
System.out.println("ForName:"+e.getMessage());}
String conURL="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};"+"DBQ=library.mdb";
Connection ExlCon=DriverManager.getConnection(conURL);
Statement Exlstmt=ExlCon.createStatement();
String s1="'"+BooknoField.getText().trim()+"'",
s2="'"+NewBookNameField.getText().trim()+"'",
s3="'"+NewBookPressField.getText().trim()+"'",
s4="'"+NewBookAuthorField.getText().trim()+"'",
s5="'"+NewBookPriceField.getText().trim()+"'",
s6="'"+NewBookStatusField.getText().trim()+"'";
String s7="UPDATE book SET bookname="+s2+",press="+s3+",author="+s4+",price="+s5+",status="+s6+"WHERE book_no ="+s1;
Exlstmt.executeUpdate(s7);
ExlCon.close();
JOptionPane.showMessageDialog(null,"更新成功,请校验");
}}
//删除图书档案
class Bookdel extends JFrame implements ActionListener
{
JPanel panel1,panel2;
Container c;
JLabel BooknameLabel,BooknoLabel;
JTextField BooknoField,BooknameField;
JButton YesBtn,CancelBtn;
Bookdel()
{
super("删除书籍档案");
c=getContentPane();
c.setLayout(new BorderLayout());
BooknoLabel=new JLabel("书籍编号",JLabel.CENTER);
BooknameLabel=new JLabel("书名",JLabel.CENTER);
BooknoField=new JTextField(7);
BooknameField=new JTextField(6);
YesBtn=new JButton("确定");
CancelBtn=new JButton("取消");
YesBtn.addActionListener(this);
CancelBtn.addActionListener(this);
panel1=new JPanel();
panel1.setLayout(new GridLayout(2,2));
panel1.add(BooknoLabel);
panel1.add(BooknoField);
panel1.add(BooknameLabel);
panel1.add(BooknameField);
panel2=new JPanel();
panel2.add(YesBtn);
panel2.add(CancelBtn);
c.add(panel1,BorderLayout.CENTER);
c.add(panel2,BorderLayout.SOUTH);
setSize(250,110);
show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==CancelBtn)
{
this.dispose();
}
else if(e.getSource()==YesBtn)
{
if(BooknoField.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null,"书号不能为空!");
}
else if(BooknameField.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null,"书名不能为空!");
}
else
{
try{Delbook();}
catch(SQLException ee){
System.out.println("SQLException:"+ee.getMessage());}
}
}
}
public void Delbook()throws SQLException
{try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){
System.out.println("ForName:"+e.getMessage());}
String conURL="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};"+"DBQ=library.mdb";
Connection ExlCon=DriverManager.getConnection(conURL);
Statement Exlstmt=ExlCon.createStatement();
String c1="'"+BooknoField.getText().trim()+"'",
c2="'"+BooknameField.getText().trim()+"'";
String b5="select * from book where book_no="+c1+"and bookname="+c2;
ResultSet b4=Exlstmt.executeQuery(b5);
if(b4.next())
{
String a1="'"+BooknoField.getText().trim()+"'";
String temp="DELETE FROM book WHERE book_no="+a1;
Exlstmt.executeUpdate(temp);
JOptionPane.showMessageDialog(null,"书籍档案已删除!");
ExlCon.close();
}
else {
JOptionPane.showMessageDialog(null,"书名或者书号不正确或者不存在该书籍!");
}
}}
//about
class About extends JFrame implements ActionListener
{JLabel txt1,txt2,txt3;
JButton btn1;
About()
{ super("关于");
setLayout(null);
setSize(300,200);
setVisible(true);
btn1=new JButton("返回");
txt1=new JLabel("made by 龚维");
txt2=new JLabel("班级号:040902");
txt3=new JLabel("学号:032865");
btn1.addActionListener(this);
add(txt1);
add(txt2);
add(txt3);
add(btn1);
txt1.setBounds(80,30,160,40);
txt2.setBounds(80,50,160,40);
txt3.setBounds(80,70,160,40);
btn1.setBounds(120,130,60,27);
validate();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btn1)
{
dispose();
}}
}
//书籍借出
class Bookborrow extends JFrame implements ActionListener
{
JPanel panel1,panel2;
Container c;
JLabel BooknoLabel,BookNameLabel,BookPressLabel,BookAuthorLabel,BookPriceLabel,BookStatusLabel,
StudentnoLabel,NameLabel,SexLabel,ClassnoLabel,DateLabel;
JTextField BooknoField,BookNameField,BookPressField,BookAuthorField,BookPriceField,BookStatusField,
StudentnoField,NameField,SexField,ClassnoField,DateField;
JButton ShowBtn,UpdateBtn,CancelBtn;
Bookborrow()
{
super("书籍出借管理");
int y,m,d;
Calendar cal=Calendar.getInstance();
y=cal.get(Calendar.YEAR);
m=cal.get(Calendar.MONTH);
d=cal.get(Calendar.DATE);
c=getContentPane();
c.setLayout(new BorderLayout());
BooknoLabel=new JLabel("借出书号",JLabel.CENTER);
BookNameLabel=new JLabel("书名",JLabel.CENTER);
BookPressLabel=new JLabel("出版社",JLabel.CENTER);
BookAuthorLabel=new JLabel("作者名",JLabel.CENTER);
BookPriceLabel=new JLabel("价格",JLabel.CENTER);
BookStatusLabel=new JLabel("状态",JLabel.CENTER);
BooknoField=new JTextField(10);
BookNameField=new JTextField(10);
BookPressField=new JTextField(10);
BookAuthorField=new JTextField(10);
BookPriceField=new JTextField(10);
BookStatusField=new JTextField(10);
DateField=new JTextField(10);
BookNameField.setEditable(false);
BookPressField.setEditable(false);
BookAuthorField.setEditable(false);
BookPriceField.setEditable(false);
BookStatusField.setEditable(false);
DateField.setEditable(false);
StudentnoLabel=new JLabel("借书学生号",JLabel.CENTER);
NameLabel=new JLabel("原姓名",JLabel.CENTER);
SexLabel=new JLabel("性别",JLabel.CENTER);
ClassnoLabel=new JLabel("班级号",JLabel.CENTER);
DateLabel=new JLabel("借书日期",JLabel.CENTER);
StudentnoField=new JTextField(10);
SexField=new JTextField(10);
ClassnoField=new JTextField(10);
NameField=new JTextField(10);
NameField.setEditable(false);
SexField.setEditable(false);
ClassnoField.setEditable(false);
ShowBtn=new JButton("显示资料");
UpdateBtn=new JButton("确认借出");
CancelBtn=new JButton("返回");
ShowBtn.addActionListener(this);
UpdateBtn.addActionListener(this);
CancelBtn.addActionListener(this);
panel1=new JPanel();
panel1.setLayout(new GridLayout(11,2));
panel1.add(BooknoLabel);
panel1.add(BooknoField);
panel1.add(BookNameLabel);
panel1.add(BookNameField);
panel1.add(BookPressLabel);
panel1.add(BookPressField);
panel1.add(BookAuthorLabel);
panel1.add(BookAuthorField);
panel1.add(BookPriceLabel);
panel1.add(BookPriceField);
panel1.add(BookStatusLabel);
panel1.add(BookStatusField);
panel1.add(StudentnoLabel);
panel1.add(StudentnoField);
panel1.add(NameLabel);
panel1.add(NameField);
panel1.add(SexLabel);
panel1.add(SexField);
panel1.add(ClassnoLabel);
panel1.add(ClassnoField);
panel1.add(DateLabel);
panel1.add(DateField);
DateField.setText(+y+"年"+m+"月"+d+"日");
panel2=new JPanel();
panel2.add(ShowBtn);
panel2.add(UpdateBtn);
panel2.add(CancelBtn);
UpdateBtn.setEnabled(false);
c.add(panel1,BorderLayout.CENTER);
c.add(panel2,BorderLayout.SOUTH);
setSize(280,350);
show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ShowBtn)
{try{Showinfo();}
catch(SQLException ee){
System.out.println("SQLException:"+ee.getMessage());}
try{Showall();}
catch(SQLException ee){
System.out.println("SQLException:"+ee.getMessage());}
UpdateBtn.setEnabled(true);
}
else if(e.getSource()==CancelBtn)
{
dispose();
}
else if(e.getSource()==UpdateBtn)
{
try{Borrowbook();}
catch(SQLException ee){
System.out.println("SQLException:"+ee.getMessage());}
}}
public void Showinfo()throws SQLException
{
String Bno,Bname,Bpress,Bauthor,Bprice,Bstatus;
int a=0;
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){
System.out.println("ForName:"+e.getMessage());}
String conURL="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};"+"DBQ=library.mdb";
Connection ExlCon=DriverManager.getConnection(conURL);
Statement Exlstmt=ExlCon.createStatement();
ResultSet rs=Exlstmt.executeQuery("SELECT * FROM book");
while (rs.next())
{
Bno=rs.getString("book_no");
Bname=rs.getString("bookname");
Bpress=rs.getString("press");
Bauthor=rs.getString("author");
Bprice=rs.getString("price");
Bstatus=rs.getString("status");
if(Bno.equals(BooknoField.getText().trim()))
{
BooknoField.setText(Bno);
BookNameField.setText(Bname);
BookPressField.setText(Bpress);
BookAuthorField.setText(Bauthor);
BookPriceField.setText(Bprice);
BookStatusField.setText(Bstatus);
a=1;
break;
}
}
ExlCon.close();
if (a==0)
{
JOptionPane.showMessageDialog(null,"不存在该书号对应的书籍档案!");
}
}
public void Showall()throws SQLException
{
String Sno,Sname,Ssex,Scno;
int i=0;
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){
System.out.println("ForName:"+e.getMessage());}
String conURL="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};"+"DBQ=library.mdb";
Connection ExlCon=DriverManager.getConnection(conURL);
Statement Exlstmt=ExlCon.createStatement();
ResultSet rs=Exlstmt.executeQuery("SELECT * FROM student");
while (rs.next())
{
Sno=rs.getString("student_no");
Sname=rs.getString("name");
Ssex=rs.getString("sex");
Scno=rs.getString("class_no");
if(Sno.equals(StudentnoField.getText().trim()))
{
NameField.setText(Sname);
SexField.setText(Ssex);
ClassnoField.setText(Scno);
i=1;
break;
}
}
ExlCon.close();
if (i==0)
{
JOptionPane.showMessageDialog(null,"不存在该学号对应的学生档案!");
}}
public void Borrowbook()throws SQLException
{
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -