📄 b_rborrow.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
class borrow extends Frame implements ActionListener
{
private static final long serialVersionUID = 1L;
Button button1,button2,button3,button4;
Label label1,label2,label3,label4,label5;
TextField text1,text2,text3,text4;
TextArea area;
borrow(String s)
{
super(s);
setVisible(true);
setBounds(300,150,700,450);
setBackground(Color.cyan);
setResizable(false);
Font f=new Font("",Font.LAYOUT_LEFT_TO_RIGHT,20);
Font g=new Font("",Font.BOLD,20);
Font k=new Font("",Font.PLAIN,15);
button1=new Button("借阅");
button1.setFont(k);
button1.addActionListener(this);
button2=new Button("归还");
button2.setFont(k);
button2.addActionListener(this);
button3=new Button("查询");
button3.setFont(k);
button3.addActionListener(this);
label1=new Label("ISBN");
label1.setFont(f);
label1.setBounds(80,80,100,30);
label2=new Label("读者编号");
label2.setFont(f);
label2.setBounds(80,120,100,30);
label3=new Label("读者姓名");
label3.setFont(f);
label3.setBounds(80,160,100,30);
label4=new Label("图书名称");
label4.setFont(f);
label4.setBounds(80,200,100,30);
text1=new TextField(20);
text1.setFont(f);
text1.setBounds(190,80,150,30);
text1.setBackground(Color.lightGray);
text2=new TextField(20);
text2.setFont(f);
text2.setBounds(190,120,150,30);
text2.setBackground(Color.lightGray);
text3=new TextField(20);
text3.setFont(f);
text3.setBounds(190,160,150,30);
text3.setBackground(Color.lightGray);
text4=new TextField(20);
text4.setFont(f);
text4.setBounds(190,200,150,30);
text4.setBackground(Color.lightGray);
area=new TextArea(20,10);
area.setFont(k);
area.setEditable(false);
button1.setBounds(170,300,100,30);
button1.setForeground(Color.blue);
button2.setBounds(300,300,100,30);
button2.setForeground(Color.blue);
button3.setBounds(430,300,100,30);
button3.setForeground(Color.blue);
area.setBounds(360,80,250,150);
add(area);
add(label1);
add(label2);
add(label3);
add(label4);
add(text1);
add(text2);
add(text3);
add(text4);
add(button1);
add(button2);
add(button3);
label5=new Label("说明");
label5.setBounds(0,0,50,30);
label5.setBackground(Color.RED);
label5.setForeground(Color.red);
add(label5);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
public void actionPerformed(ActionEvent e)
{
Date date=new Date();
SimpleDateFormat n=new SimpleDateFormat("MM-dd");
area.setText(n.format(date));
Connection con;
Statement sql;
ResultSet rs;
String insertStr,delStr,searchStr;
if(e.getSource()==button1)//添加信息
{
if((!text1.getText().equals(""))&&(!text2.getText().equals(""))&&(!text3.getText().equals(""))&&(!text4.getText().equals("")))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
JOptionPane.showMessageDialog(this, "没有建立连接","警告",JOptionPane.WARNING_MESSAGE);
area.setText("没有建立连接\n");
}
try
{
con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
sql=con.createStatement();
insertStr="select num from book where ISBN='"+text1.getText()+"';";
rs=sql.executeQuery(insertStr);
int number=rs.getInt("num");
if(number==0)
{
JOptionPane.showConfirmDialog(this, "借阅失败,该书已借出!","确认对话框",JOptionPane.PLAIN_MESSAGE);
}
else
{
insertStr="insert into borrow values ('"+text1.getText()+"','"+text2.getText()+"','"+text3.getText()+"','"+text4.getText()+"','"+n.format(date)+",'"+n.format(date)+"+"+30+");";
sql.executeUpdate(insertStr);
sql.executeUpdate("update book set num=num-1;");
}
con.close();
}
catch (SQLException e2)
{
area.setText("借阅失败!\n");
JOptionPane.showConfirmDialog(this, "借阅失败!","确认对话框",JOptionPane.PLAIN_MESSAGE);
}
}
else if((text1.getText().equals(""))||(!text2.getText().equals(""))||(!text3.getText().equals(""))||(!text4.getText().equals("")))
{
JOptionPane.showMessageDialog(this,"文本框中不能为空值!","警告",JOptionPane.WARNING_MESSAGE);
}
}
else if(e.getSource()==button2)//归还图书
{
if((!text1.getText().equals(""))&&(!text2.getText().equals(""))&&(!text3.getText().equals(""))&&(!text4.getText().equals("")))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
JOptionPane.showMessageDialog(this, "没有建立连接","警告",JOptionPane.WARNING_MESSAGE);
area.setText("没有建立连接\n");
}
try
{
con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
sql=con.createStatement();
delStr="delete borrow where ISBN='"+text1.getText()+"'and no='"+text2.getText()+"'and rName='"+text3.getText()+"'and bName='"+text4.getText()+"';";
sql.executeUpdate(delStr);
sql.executeUpdate("update book set num=num+1;");
area.append("图书归还成功");
con.close();
}
catch (SQLException e2)
{
area.setText("归还失败!\n");
JOptionPane.showConfirmDialog(this, "归还失败!","确认对话框",JOptionPane.PLAIN_MESSAGE);
}
}
else if((text1.getText().equals(""))||(!text2.getText().equals(""))||(!text3.getText().equals(""))||(!text4.getText().equals("")))
{
JOptionPane.showMessageDialog(this,"文本框中不能为空值!","警告",JOptionPane.WARNING_MESSAGE);
}
}
else if(e.getSource()==button3)//查找借阅信息
{
if((!text1.getText().equals(""))&&(!text2.getText().equals("")))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
JOptionPane.showMessageDialog(this, "没有建立连接","警告",JOptionPane.WARNING_MESSAGE);
area.setText("没有建立连接\n");
}
try
{
con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
sql=con.createStatement();
searchStr="select *from borrow where ISBN='"+text1.getText()+"'and no='"+text2.getText()+"';";
rs=sql.executeQuery(searchStr);
con.close();
}
catch (SQLException e2)
{
area.setText("查询失败!\n");
JOptionPane.showConfirmDialog(this, "查询失败!","确认对话框",JOptionPane.PLAIN_MESSAGE);
}
}
else if((text1.getText().equals(""))||(!text2.getText().equals("")))
{
JOptionPane.showMessageDialog(this,"文本框中不能为空值!","警告",JOptionPane.WARNING_MESSAGE);
}
}
text1.setText(null);
text2.setText(null);
text3.setText(null);
text4.setText(null);
area.setText(null);
}
}
/*public class b_rBorrow
{
public static void main(String[] args)
{
new borrow("借阅信息管理");
}
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -