/*
* Created by JFormDesigner on Sat Dec 31 12:54:54 CST 2022
*/
package AdministratorView;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import Dao.BookDao;
import Dao.BookTypeDao;
import Moudle.Book;
import Moudle.BookType;
import Util.DataBaseUtil;
import Util.IsValueUser;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
/**
* @author MaYiA
*/
public class BookMangerFrame extends JFrame {
public BookMangerFrame() {
initComponents();
}
//查询书籍事件
private void searchbook(ActionEvent e) {
bookSearchActionPerformed(e);
}
/**
* 查询书籍事件处理
* @param e
*/
private void bookSearchActionPerformed(ActionEvent e) {
//获取查询的条件
String bookName=s_bookNameTxt.getText(); //图书名称
String author=s_authorTxt.getText(); //图书作者
String bookTypeName=s_bookTypeJcb.getSelectedItem().toString(); //图书类别
//对图书类别"请选择..."换成""
if("请选择图书类别".equals(bookTypeName)){
bookTypeName="";
}
//生成带有条件的图书对象
Book book=new Book();
book.setBookName(bookName); //设置图书名称条件
book.setAuthor(author); //设置图书作者条件
book.setBookTypeName(bookTypeName); //设置图书类别条件
//调用table填充函数,根据查询结果重新填充表格
this.fillTable(book);
}
//表格点击事件
private void bookTableMousePressed(MouseEvent e) {
bookTableActionMousePressed(e);
}
/**
* 表格行点击事件处理
* @param e
*/
private void bookTableActionMousePressed(MouseEvent e) {
int row = this.bookTable.getSelectedRow();
this.idTxt.setText((String)bookTable.getValueAt(row,0));
this.bookNameTxt.setText((String)bookTable.getValueAt(row,1));
this.authorTxt.setText((String)bookTable.getValueAt(row,2));
String sex = (String) bookTable.getValueAt(row,3);
if ("男".equals(sex)) {
this.manJrb.setSelected(true);
} else if ("女".equals(sex)) {
this.femaleJrb.setSelected(true);
}
this.priceTxt.setText((Float)bookTable.getValueAt(row,4) + "");
this.bookDescTxt.setText((String)bookTable.getValueAt(row,5));
String bookTypeName = (String) this.bookTable.getValueAt(row,6);
int num = this.bookTypeJcb.getItemCount();
for (int i = 0;i < num;i++) {
BookType item=(BookType)this.bookTypeJcb.getItemAt(i);
if (item.getBookTypeName().equals(bookTypeName)) {
this.bookTypeJcb.setSelectedIndex(i);
}
}
}
//修改书籍事件
private void bookUpDate(ActionEvent e) {
bookUpDateAvtionPerformed(e);
}
/**
* 图书修改事件处理
* @param e
*/
private void bookUpDateAvtionPerformed(ActionEvent e) {
String id = this.idTxt.getText();
if (IsValueUser.isEmpty(id)) {
JOptionPane.showMessageDialog(null,"请选择要修改的记录!");
return;
}
String bookName = this.bookNameTxt.getText();
String author = this.authorTxt.getText();
String sex= null;
if (manJrb.isSelected()) {
sex = "男";
} else if (femaleJrb.isSelected()) {
sex = "女";
}
String price = this.priceTxt.getText();
String bookDesc = this.bookDescTxt.getText();
if (IsValueUser.isEmpty(bookName)) {
JOptionPane.showMessageDialog(null,"图书名称不能为空!");
return;
}
if (IsValueUser.isEmpty(author)) {
JOptionPane.showMessageDialog(null,"图书作者不能为空!");
return;
}
if (IsValueUser.isEmpty(price)) {
JOptionPane.showMessageDialog(null,"图书价格不能为空!");
return;
}
BookType bookType = (BookType) bookTypeJcb.getSelectedItem();
int bookTypeId = bookType.getId();
Book book = new Book(Integer.parseInt(id),bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc);
Connection connection =null;
try {
connection = dataBaseUtil.getCon();
int addNum = bookDao.upDate(connection,book);
if (addNum == 1) {
JOptionPane.showMessageDialog(null,"图书修改成功!");
resetValue();
this.fillTable(new Book());
} else {
JOptionPane.showMessageDialog(null,"图书修改失败!");
}
} catch (Exception evt) {
evt.printStackTrace();
JOptionPane.showMessageDialog(null,"图书修改失败!");
} finally {
try {
dataBaseUtil.closeCon(connection);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
//图书删除事件
private void deleteBook(ActionEvent e) {
bookDeleteActionPerformed(e);
}
/**
* 图书删除事件处理
* @param e
*/
private void bookDeleteActionPerformed(ActionEvent e) {
String id =idTxt.getText();
if (IsValueUser.isEmpty(id)) {
JOptionPane.showMessageDialog(null,"请选择要删除的记录:");
return;
}
int n = JOptionPane.showConfirmDialog(null,"确定要删除该记录吗?");
if (n == 0) {
Connection connection = null;
try {
connection = dataBaseUtil.getCon();
int deleteNum = bookDao.delete(connection,id);
if (deleteNum == 1) {
JOptionPane.showMessageDialog(null,"删除成功!");
this.resetValue();
this.fillTable(new Book());
} else {
JOptionPane.showMessageDialog(null,"删除失败!");
}
} catch (Exception evt) {
evt.printStackTrace();
JOptionPane.showMessageDialog(null,"修改 失败!");
}finally {
try {
dataBaseUtil.closeCon(connection);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
scrollPane1 = new JScrollPane();
bookTable = new JTable();
label1 = new JLabel();
s_bookNameTxt = new JTextField();
label2 = new JLabel();
s_authorTxt = new JTextField();
label3 = new JLabel();
s_bookTypeJcb = new JComboBox();
button1 = new JButton();
panel1 = new JPanel();
label4 = new JLabel();
idTxt = new JTextField();
label5 = new JLabel();
label6 = new JLabel();
bookNameTxt = new JTextField();
manJrb = new JRadioButton();
femaleJrb = new JRadioButton();
label7 = new JLabel();
authorTxt = new JTextField();
label8 = new JLabel();
priceTxt = new JTextField();
label9 = new JLabel();
label10 = new JLabel();
bookTypeJcb = new JComboBox();
scrollPane2 = new JScrollPane();
bookDescTxt = new JTextArea();
button2 = new JButton();
button3 = new JButton();
//======== this ========
setTitle("\u56fe\u