📄 searchbook.java
字号:
/**
* @(#)SearchBook.java
*
*
* @author
* @version 1.00 2009/5/16
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class SearchBook extends JFrame implements ActionListener
{
JPanel panel1,panel2,panel3,panel4;
JLabel TipLabel=new JLabel("输入图书编号点确定,将查询图书相关信息");
JLabel BookNameLabel,PressNameLabel,
AuthorLabel,BookIdLabel,
PressDateLabel,PriceLabel,StationLabel;
JTextField BookNameTextField,PressNameTextField,
AuthorTextField,BookIdTextField,
PressDateTextField,PriceTextField,StationTextField;
Container c;
JButton ClearBtn,YesBtn,UpdateBtn,ExitBtn;
public SearchBook()
{
super("查询图书信息");
c=getContentPane();
c.setLayout(null);
panel3=new JPanel();
panel3.add(TipLabel);
panel3.setBounds(0,0,300,30);
c.add(panel3,BorderLayout.NORTH);
BookIdLabel=new JLabel("编号",JLabel.CENTER);
BookNameLabel=new JLabel("书名",JLabel.CENTER);
AuthorLabel=new JLabel("作者",JLabel.CENTER);
PressNameLabel=new JLabel("出版社",JLabel.CENTER);
PressDateLabel=new JLabel("出版日期",JLabel.CENTER);
PriceLabel=new JLabel("价格",JLabel.CENTER);
StationLabel=new JLabel("状态",JLabel.CENTER);
BookNameTextField=new JTextField(20);
PressNameTextField=new JTextField(20);
AuthorTextField=new JTextField(20);
BookIdTextField=new JTextField(20);
PressDateTextField=new JTextField(20);
PriceTextField=new JTextField(20);
StationTextField=new JTextField(15);
panel1=new JPanel();
panel4=new JPanel();
panel1.setLayout(new GridLayout(7,1));
panel4.setLayout(new GridLayout(7,1));
panel1.add(BookIdLabel);
panel4.add(BookIdTextField);
panel1.add(BookNameLabel);
panel4.add(BookNameTextField);
panel1.add(PressNameLabel);
panel4.add(PressNameTextField);
panel1.add(AuthorLabel);
panel4.add(AuthorTextField);
panel1.add(PressNameLabel);
panel4.add(PressNameTextField);
panel1.add(PressDateLabel);
panel4.add(PressDateTextField);
panel1.add(PriceLabel);
panel4.add(PriceTextField);
panel1.add(StationLabel);
panel4.add(StationTextField);
panel1.setBounds(0,30,100,220);
panel4.setBounds(100,30,220,220);
panel2=new JPanel();
panel2.setBounds(10,260,300,30);
panel2.setLayout(new GridLayout(1,4));
ClearBtn=new JButton("清空");
YesBtn=new JButton("确定");
UpdateBtn=new JButton("查询");
ExitBtn=new JButton("退出");
panel2.add(ClearBtn);
panel2.add(YesBtn);
panel2.add(UpdateBtn);
panel2.add(ExitBtn);
ClearBtn.addActionListener(this);
YesBtn.addActionListener(this);
UpdateBtn.addActionListener(this);
ExitBtn.addActionListener(this);
UpdateBtn.setEnabled(false);
c.add(panel3);
c.add(panel1);
c.add(panel4);
c.add(panel2);
setVisible(true);
setBounds(100,50,330,320);
}
public void actionPerformed(ActionEvent e)
{
Connection con;
PreparedStatement sql,sql1;
ResultSet rs;
if(e.getSource()==ExitBtn)
{
this.dispose();
}
else if(e.getSource()==ClearBtn)
{
BookNameTextField.setText("");
PressNameTextField.setText("");
AuthorTextField.setText("");
BookIdTextField.setText("");
PressDateTextField.setText("");
PriceTextField.setText("");
StationTextField.setText("");
}
else if(e.getSource()==YesBtn)
{
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException f){}
try {
String bianhao,isbn,worker,name,printe,date,price,state;
bianhao=BookIdTextField.getText();
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql=con.prepareStatement("SELECT * FROM book where bid=?");
sql.setString(1,bianhao);
rs=sql.executeQuery();
if(!rs.next()){
JOptionPane.showMessageDialog(this,"所要修改的图书不存在!","提示对话框",JOptionPane.WARNING_MESSAGE);
}
else{
UpdateBtn.setEnabled(true);
con.close();
}
}
catch(SQLException e2)
{ System.out.println(e2);
}
}
if(e.getSource()==UpdateBtn){
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException f){}
try {
String bianhao=BookIdTextField.getText();
con=DriverManager.getConnection("jdbc:odbc:tushuguan");
sql=con.prepareStatement("SELECT * FROM book where bid=?");
sql.setString(1,bianhao);
rs=sql.executeQuery();
if(rs.next()){
BookNameTextField.setText(rs.getString(2));
AuthorTextField.setText(rs.getString(3));
PressNameTextField.setText(rs.getString(4));
PressDateTextField.setText(rs.getString(5));
PriceTextField.setText(rs.getString(6));
StationTextField.setText(rs.getString(7));
}
con.close();
}
catch(SQLException e2)
{ System.out.println(e2);
}
}
}
public static void main(String args[]){
new SearchBook();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -