📄 book_demand.java
字号:
//图书查询
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class book_demand extends JFrame implements ActionListener{
JLabel la_demand =new JLabel("查询条件");
JLabel la_info =new JLabel("查询结果");
JRadioButton ch_id = new JRadioButton("书目编号");
JRadioButton ch_name = new JRadioButton("书 名");
JRadioButton ch_isbn = new JRadioButton("ISBN号");
ButtonGroup cbg=new ButtonGroup();
JLabel la_zhi =new JLabel("查询值");
JTextField t_demand=new JTextField(25);
JButton b_demand=new JButton("查 询");
JButton b_look=new JButton("显示所有");
JButton b_return=new JButton("返 回");
String[] tab_name={"书目编号","书名","ISBN号","价格","作者","出版社","简介"};
String[][] tab_info=new String[30][7];
JTable tab_demand=new JTable(tab_info,tab_name);
JScrollPane sp = new JScrollPane(tab_demand);
Connection conn = null;
//new监听器
action_demand ac_demand=new action_demand();
action_look ac_look=new action_look();
public book_demand(){
super("图书查询");
this.setLayout(null);
JPanel p=new JPanel(); //菜单面板
JPanel p_info=new JPanel();
JPanel p1=new JPanel();//第一行菜单
JPanel p2=new JPanel();//第二行菜单
JPanel p3=new JPanel();//第三行菜单
p.setBounds(3,10,800,100);
p.setLayout(new GridLayout(3,1));
//p.setBackground(new Color(200,200,200));
cbg.add(ch_id); cbg.add(ch_name); cbg.add(ch_isbn);//添加复选框
p1.add(la_demand); p1.add(ch_id); p1.add(ch_name); p1.add(ch_isbn);
p2.add(la_zhi); p2.add(t_demand);
p3.add(b_demand); p3.add(b_look); p3.add(b_return);
ch_id.setSelected(true);//起初选中ch_id
p.add(p1); p.add(p2);p.add(p3);
this.add(p);
p_info.setBounds(3,110,180,20);//提示信息
p_info.add(la_info);
this.add(p_info);//添加信息提示栏
sp.setBounds(3,140,795,330); // 滚动面板
this.add(sp);
this.setBounds(200,200,800,450);
this.setVisible(true);
this.setResizable(false);
//添加监听器
b_demand.addActionListener(ac_demand);
b_look.addActionListener(ac_look);
b_return.addActionListener(this);
//连接数据库
conn = book_sql.getConn();
//System.out.println(conn);
}
public void actionPerformed(ActionEvent e){
new book_main();//返回主菜单
this.dispose();
}
//action_demand查询按钮
class action_demand implements ActionListener{
public void actionPerformed(ActionEvent e){
String temp=t_demand.getText().trim();
String sql = null;
if(temp.equals("")){
JOptionPane.showMessageDialog(null,"查询关键字不可为空",
"Caution",JOptionPane.WARNING_MESSAGE);
return;
}else{
//bid查询
if(ch_id.isSelected()){
sql="select* from bookinfo where bid='"+temp+"'";
main_sql(sql);
}
//name查询
if(ch_name.isSelected()){
sql="select* from bookinfo where name='"+temp+"'";
main_sql(sql);
}
//isbn查询
if(ch_isbn.isSelected()){
sql="select* from bookinfo where isbn='"+temp+"'";
main_sql(sql);
}
}
}
void main_sql(String sql){
int i,j;
int k=0;
ResultSet rs = null;
Statement st = null;
try{
st=conn.createStatement();
rs=st.executeQuery(sql);
for(i=0;i<tab_info.length;i++){
for (j=0;j<7; j++)
tab_info[i][j] = null;
}
tab_demand.repaint();
rs.beforeFirst();//定位到结果集的首行
while(rs.next()){
tab_info[k][0] = rs.getString(1);
tab_info[k][1] = rs.getString(2);
tab_info[k][2] = rs.getString(3);
tab_info[k][3] = rs.getString(4);
tab_info[k][4] = rs.getString(5);
tab_info[k][5] = rs.getString(6);
tab_info[k][6] = rs.getString(7);
k++;
}
}catch(SQLException sqle){
sqle.printStackTrace();
}
tab_demand.repaint(); //显示数据库里面的信息
}
}
//action_look显示所有按钮
class action_look implements ActionListener{
public void actionPerformed(ActionEvent e){
t_demand.setText("");
int k=0;
ResultSet rs = null;
Statement st = null;
String sql_look="select*from bookinfo";
try{
st=conn.createStatement();
rs=st.executeQuery(sql_look);
while(rs.next()){
tab_info[k][0] = rs.getString(1);
tab_info[k][1] = rs.getString(2);
tab_info[k][2] = rs.getString(3);
tab_info[k][3] = rs.getString(4);
tab_info[k][4] = rs.getString(5);
tab_info[k][5] = rs.getString(6);
tab_info[k][6] = rs.getString(7);
k++;
}
}catch(SQLException elook){
elook.printStackTrace();
}
tab_demand.repaint(); //显示数据库里面的信息
}
}
//
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -