176 lines
6.8 KiB
Java
176 lines
6.8 KiB
Java
package com.msksbr.MainFrm.MenuItemDiaog;
|
||
|
||
import com.msksbr.LoginFrm.ScreenSize;
|
||
import com.msksbr.SQL.Connector;
|
||
|
||
import javax.swing.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
import java.awt.event.KeyAdapter;
|
||
import java.awt.event.KeyEvent;
|
||
import java.sql.ResultSet;
|
||
import java.sql.SQLException;
|
||
|
||
/**
|
||
* SearchDIalog 类用于显示查询对话框,并根据用户输入的信息查询数据库中的记录。
|
||
*/
|
||
public class SearchDIalog extends JDialog {
|
||
// 用于显示查询结果的标签
|
||
public JLabel messageLabel;
|
||
// 用于存储用户输入的ID
|
||
protected int idInt;
|
||
// 内容面板
|
||
private JPanel contentPane;
|
||
// 确认按钮
|
||
private JButton buttonOK;
|
||
// 用于输入ID的文本框
|
||
private JTextField idFIeld;
|
||
// 要查询的表名
|
||
private String table;
|
||
|
||
/**
|
||
* SearchDIalog 构造函数,初始化对话框并设置相关属性。
|
||
* @param table 要查询的表名
|
||
*/
|
||
public SearchDIalog(String table) {
|
||
// 设置对话框的内容面板
|
||
setContentPane(contentPane);
|
||
// 设置对话框为模态对话框
|
||
setModal(true);
|
||
// 设置默认按钮为确认按钮
|
||
getRootPane().setDefaultButton(buttonOK);
|
||
// 根据表名设置对话框标题和提示信息
|
||
if (table.equals("books")) {
|
||
this.table = "books";
|
||
this.setTitle("查询图书信息");
|
||
messageLabel.setText("请输入书号:");
|
||
} else if (table.equals("students")) {
|
||
this.table = "students";
|
||
this.setTitle("查询学生信息");
|
||
messageLabel.setText("请输入学号:");
|
||
} else if (table.equals("rents")) {
|
||
this.table = "rents";
|
||
this.setTitle("查询借阅信息");
|
||
messageLabel.setText("请输入书号:");
|
||
}
|
||
// 设置事件监听器
|
||
setListener();
|
||
// 调整对话框大小以适应内容
|
||
pack();
|
||
// 设置对话框位置
|
||
setLocation();
|
||
}
|
||
|
||
/**
|
||
* 设置事件监听器,包括确认按钮的点击事件和文本框的回车键事件。
|
||
*/
|
||
private void setListener() {
|
||
// 为确认按钮添加事件监听器
|
||
buttonOK.addActionListener(new ActionListener() {
|
||
public void actionPerformed(ActionEvent e) {
|
||
// 点击确认按钮时执行的操作
|
||
onOK();
|
||
}
|
||
});
|
||
// 为文本框添加键盘事件监听器
|
||
idFIeld.addKeyListener(new KeyAdapter() {
|
||
public void keyPressed(KeyEvent e) {
|
||
// 当用户按下回车键时执行的操作
|
||
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||
onOK();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 设置对话框位置,使其居中显示在屏幕上。
|
||
*/
|
||
private void setLocation() {
|
||
// 设置对话框位置,使其居中显示在屏幕上
|
||
setLocation(new ScreenSize().width / 2 - getWidth() / 2, new ScreenSize().height / 2 - getHeight() / 2);
|
||
}
|
||
|
||
/**
|
||
* 点击确认按钮或按下回车键时执行的操作,查询数据库并显示结果。
|
||
*/
|
||
private void onOK() {
|
||
// 执行SQL查询并获取结果集
|
||
ResultSet rs = SQLExe();
|
||
// 用于存储查询结果的字符串
|
||
String message = "";
|
||
try {
|
||
// 如果结果集不为空且有下一条记录
|
||
if (rs != null && rs.next()) {
|
||
// 用于存储查询结果的标题
|
||
String title = "";
|
||
// 根据表名生成查询结果的字符串
|
||
if (table.equals("books")) {
|
||
message = "书名:\t" + rs.getString("book_name") + "\n类别:\t" + rs.getString("book_type") + "\n书号:\t" + rs.getInt("book_id") + "\nISBN:\t" + rs.getString("ISBN");
|
||
title = "图书信息";
|
||
} else if (table.equals("students")) {
|
||
message = "学生姓名:\t" + rs.getString("student_name") + "\n学号:\t" + rs.getInt("student_id") + "\n班级:\t" + rs.getString("student_class") + "\n性别:\t" + rs.getString("student_gender");
|
||
title = "学生信息";
|
||
} else if (table.equals("rents")) {
|
||
message = "学生姓名:\t" + rs.getString("student_name") + "\n学号:\t" + rs.getInt("student_id") + "\n班级:\t" + rs.getString("student_class") + "\n性别:\t" + rs.getString("student_gender") + "\n书名:\t" + rs.getString("book_name") + "\n书号:\t" + rs.getInt("book_id") + "\n类别:\t" + rs.getString("book_type") + "\nISBN:\t" + rs.getString("ISBN");
|
||
title = "借阅信息";
|
||
}
|
||
// 显示查询结果
|
||
new showMessae(message, title);
|
||
} else {
|
||
// 如果未找到记录,则显示错误信息
|
||
JOptionPane.showMessageDialog(this, "未找到", "未找到", JOptionPane.WARNING_MESSAGE);
|
||
}
|
||
} catch (SQLException e) {
|
||
// 抛出运行时异常
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 执行SQL查询并返回结果集。
|
||
* @return 查询结果集
|
||
*/
|
||
private ResultSet SQLExe() {
|
||
// 用于存储查询结果集
|
||
ResultSet rs = null;
|
||
try {
|
||
// 将用户输入的ID转换为整数
|
||
idInt = Integer.valueOf(idFIeld.getText());
|
||
// 创建数据库连接器
|
||
Connector connector = new Connector();
|
||
// 执行SQL查询并获取结果集
|
||
rs = connector.executeQuery(SQLExeMaker(table));
|
||
} catch (NumberFormatException e) {
|
||
// 如果用户输入的ID不是整数,则显示错误信息
|
||
JOptionPane.showMessageDialog(this, "请输入一个整数", "错误", JOptionPane.ERROR_MESSAGE);
|
||
} catch (SQLException e) {
|
||
// 抛出运行时异常
|
||
throw new RuntimeException(e);
|
||
} catch (ClassNotFoundException e) {
|
||
// 抛出运行时异常
|
||
throw new RuntimeException(e);
|
||
}
|
||
// 返回查询结果集
|
||
return rs;
|
||
}
|
||
|
||
/**
|
||
* 根据表名生成SQL查询语句。
|
||
* @param table 要查询的表名
|
||
* @return SQL查询语句
|
||
*/
|
||
private String SQLExeMaker(String table) {
|
||
// 根据表名生成SQL查询语句
|
||
if (table.equals("books")) {
|
||
return "SELECT * FROM `books` WHERE `book_id` = " + idInt + ";";
|
||
} else if (table.equals("students")) {
|
||
return "SELECT * FROM `students` WHERE `student_id` = " + idInt + ";";
|
||
} else if (table.equals("rents")) {
|
||
return "SELECT * FROM `rents` WHERE `book_id` = " + idInt + ";";
|
||
} else {
|
||
return null;
|
||
}
|
||
}
|
||
}
|