📄 returnbook.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class ReturnBook extends JFrame implements ActionListener
{
JPanel panel1,panel2;
Container c;
JLabel ReturnedBookStudentLabel,ReturnedBookNameLabel;
JTextField ReturnedBookStudentTextField,
ReturnedBookTextField;
JButton YesBtn,CancelBtn;
public ReturnBook()
{
super("书籍归还");
c=getContentPane();
c.setLayout(null);
ReturnedBookStudentLabel=new JLabel("读者编号",JLabel.CENTER);
ReturnedBookNameLabel=new JLabel("图书编号",JLabel.CENTER);
ReturnedBookStudentTextField=new JTextField(15);
ReturnedBookTextField=new JTextField(15);
panel1=new JPanel();
panel1.setLayout(new GridLayout(2,2));
panel1.add(ReturnedBookStudentLabel);
panel1.add(ReturnedBookStudentTextField);
panel1.add(ReturnedBookNameLabel);
panel1.add(ReturnedBookTextField);
panel1.setBounds(0,0,300,80);
c.add(panel1);
YesBtn=new JButton("确定");
CancelBtn=new JButton("取消");
YesBtn.addActionListener(this);
CancelBtn.addActionListener(this);
panel2=new JPanel();
panel2.setBounds(0,90,300,30);
panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
panel2.add(Box.createHorizontalStrut(60));
panel2.add(YesBtn);
panel2.add(Box.createHorizontalStrut(40));
panel2.add(CancelBtn);
panel2.add(Box.createHorizontalStrut(60));
c.add(panel2);
setBounds(300,300,320,160);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Connection con;
PreparedStatement sql;
Statement sql2;
ResultSet rs;
if(e.getSource()==CancelBtn)
{
this.dispose();
}
else if(e.getSource()==YesBtn)
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException f){}
try { con=DriverManager.getConnection("jdbc:odbc:tushuguan");
String bianhao1=ReturnedBookStudentTextField.getText(),
bianhao2=ReturnedBookTextField.getText();
if(bianhao1.equals(""))
JOptionPane.showMessageDialog(this,"读者编号不能为空!","提示对话框",JOptionPane.WARNING_MESSAGE);
else if(bianhao2.equals(""))
JOptionPane.showMessageDialog(this,"图书编号不能为空!","提示对话框",JOptionPane.WARNING_MESSAGE);
else{
sql=con.prepareStatement("select rid,bid,date2 from borrow where rid=? and bid=?");
sql.setString(1,bianhao1);
sql.setString(2,bianhao2);
rs=sql.executeQuery();
if(rs.next()){
con.close();
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql=con.prepareStatement("select datediff(d,date2,getdate()) from borrow where rid=? and bid=?");
sql.setString(1,bianhao1);
sql.setString(2,bianhao2);
rs=sql.executeQuery();
while(rs.next()){
int day=rs.getInt(1);
if(day>0){
JOptionPane.showMessageDialog(this,"图书超期"+day+"天归还!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
}
}
con.close();
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql=con.prepareStatement("delete borrow where rid=? and bid=?");
sql.setString(1,bianhao1);
sql.setString(2,bianhao2);
sql.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="in";
sql2.executeUpdate("update book set station="+"'"+s+"'"+" where bid= "+"'"+bianhao2+"'");
con.close();
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql=con.prepareStatement("insert into returnbook values (?,?,getdate())");
sql.setString(1,bianhao2);
sql.setString(2,bianhao1);
sql.executeUpdate();
con.close();
}
else JOptionPane.showMessageDialog(this,"输入的信息有误!","提示对话框",JOptionPane.WARNING_MESSAGE);
}
}
catch(SQLException e2)
{ System.out.println(e2);
}
}
}
public static void main(String args[]){
ReturnBook rs=new ReturnBook();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -