📄 borrowbook.java
字号:
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class BorrowBook extends JFrame implements ActionListener {
JLabel BorrowedBookStudentLabel,BorrowedBookIdLabel;
JTextField BorrowedBookStudentTextField,BorrowedDateTextField;
JButton Jb1,Jb2;
public BorrowBook() {
Container c=this.getContentPane();
c.setLayout(null);
BorrowedBookStudentLabel=new JLabel("读者编号:");
BorrowedBookIdLabel=new JLabel("图书编号:");
BorrowedBookStudentTextField=new JTextField(20);
BorrowedDateTextField=new JTextField(20);
Jb1=new JButton("确定");
Jb2=new JButton("取消");
Jb1.addActionListener(this);
Jb2.addActionListener(this);
JPanel p1, p2,p3,p23;
p1=new JPanel();
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
p1.add(Box.createHorizontalStrut(60));
p1.add(Jb1);
p1.add(Box.createHorizontalStrut(40));
p1.add(Jb2);
p1.add(Box.createHorizontalStrut(60));
p2=new JPanel(new GridLayout(2,1,10,10));
p3=new JPanel(new GridLayout(2,1,10,10));
p23=new JPanel();
p23.setLayout(new BoxLayout(p23,BoxLayout.X_AXIS));
p23.add(Box.createHorizontalStrut(20));
p23.add(p2);
p23.add(Box.createHorizontalStrut(30));
p23.add(p3);
p23.add(Box.createHorizontalStrut(15));
p2.add(BorrowedBookStudentLabel);
p2.add(BorrowedBookIdLabel);
p3.add(BorrowedBookStudentTextField);
p3.add(BorrowedDateTextField);
p23.setBounds(0,10,300,60);
p1.setBounds(0,80,300,50);
c.add(p23);
c.add(p1);
this.setSize(300,160);
this.setVisible(true);
setResizable(false);
this.setTitle("图书借阅");
}
public void actionPerformed(ActionEvent e){
Connection con;
Statement sql2;
PreparedStatement sql,sql1;
ResultSet rs;
boolean boo=false,boo1=false;
String str1,str2,str3,str4;
if(e.getSource()==Jb1){
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException f){}
try {
String bianhao1,bianhao2,date1,date2;
bianhao1=BorrowedBookStudentTextField.getText();
bianhao2=BorrowedDateTextField.getText();
if(bianhao1.equals(""))
JOptionPane.showMessageDialog(this,"读者编号不能为空!","提示对话框",JOptionPane.WARNING_MESSAGE);
else{
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql=con.prepareStatement("SELECT rid,num,yijie FROM reader where rid=?");
sql.setString(1,bianhao1);
rs=sql.executeQuery();
if(rs.next()){
if(rs.getInt(2)==rs.getInt(3)){
JOptionPane.showMessageDialog(this,"此读者借书卡已满!","提示对话框",JOptionPane.WARNING_MESSAGE);
}
else{
boo=true;
}
}
else JOptionPane.showMessageDialog(this,"你输入读者不存在!","警告对话框",JOptionPane.WARNING_MESSAGE);
con.close();
}
if(bianhao2.equals(""))
JOptionPane.showMessageDialog(this,"图书编号不能为空!","提示对话框",JOptionPane.WARNING_MESSAGE);
else{
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql=con.prepareStatement("SELECT bid,station FROM book where bid=?");
sql.setString(1,bianhao2);
rs=sql.executeQuery();
if(rs.next()){
if(rs.getString(2).trim().equals("out")){
JOptionPane.showMessageDialog(this,"此图书已借出!","提示对话框",JOptionPane.WARNING_MESSAGE);
}
else boo1=true;
}
else JOptionPane.showMessageDialog(this,"你输入图书不存在!","提示对话框",JOptionPane.ERROR_MESSAGE);
con.close();
}
if(boo&&boo1){
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql1=con.prepareStatement("insert into borrow values (?,?,getdate(),dateadd(day,30,getdate()))");
sql1.setString(1,bianhao1);
sql1.setString(2,bianhao2);
sql1.executeUpdate();
con.close();
JOptionPane.showMessageDialog(this,"借阅成功!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql2=con.createStatement();
sql2.executeUpdate("update reader set yijie=yijie+"+1+" where rid= "+"'"+bianhao1+"'");
con.close();
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql2=con.createStatement();
String s="out";
sql2.executeUpdate("update book set station="+"'"+s+"'"+" where bid= "+"'"+bianhao2+"'");
con.close();
}
}
catch(SQLException e2)
{ System.out.println(e2);
}
}
else this.dispose();
}
public static void main(String args[])
{
new BorrowBook();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -