package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import dao.BookDao;
import dao.JieyueDao;
import model.Book;
import model.Jieyue;
import util.Connect;
import util.DeterminNull;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class FindBook extends JFrame {
private JPanel contentPane;
private JTextField book_nameTxt;
private JTextField book_zuozheTxt;
private JTextField book_chubanTxt;
private Connect conutil= new Connect();
private BookDao bookDao= new BookDao();
private JieyueDao jieyueDao = new JieyueDao();
public static String duzheName;
public static String guanliyName;
/**
* Launch the application.
*/
JFrame frame = new JFrame();
private JTable bookTable;
private JPanel panel;
private JLabel lblNewLabel;
private JButton jieshuButton;
private JLabel label;
private JLabel label_5;
private JLabel label_6;
private JTextField book_RBnameTxt;
private JTextField book_RBzuozheTxt;
private JTextField book_RBchubanTxt;
private JTextField book_RBstateTxt;
private JTextField book_RBclassTxt;
private JButton guihuanButton;
private JTextField book_RBidTxt;
private JLabel label_4;
private JTextField book_classTxt;
private JLabel label_8;
/**
* Create the frame.
*/
public FindBook() {
setTitle("图书的查询、借阅、归还");
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100, 100, 920, 686);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton findButton = new JButton("查询");
findButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
findBookTable(e);
}
});
JLabel label_1 = new JLabel("图书名称:");
JLabel label_2 = new JLabel("图书作者:");
JLabel label_3 = new JLabel("出版社:");
book_nameTxt = new JTextField();
book_nameTxt.setColumns(10);
book_zuozheTxt = new JTextField();
book_zuozheTxt.setColumns(10);
book_chubanTxt = new JTextField();
book_chubanTxt.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
panel = new JPanel();
label_4 = new JLabel("类型:");
book_classTxt = new JTextField();
book_classTxt.setColumns(10);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(42)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label_1)
.addComponent(label_2)
.addComponent(label_3)
.addComponent(label_4))
.addGap(28)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(findButton, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)
.addComponent(book_nameTxt, GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
.addComponent(book_zuozheTxt))
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
.addComponent(book_classTxt, Alignment.LEADING)
.addComponent(book_chubanTxt, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))))
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 435, GroupLayout.PREFERRED_SIZE)))
.addGap(34)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 388, GroupLayout.PREFERRED_SIZE)
.addGap(29))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(42)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(84)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label_1)
.addComponent(book_nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(book_zuozheTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(28)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_3)
.addComponent(book_chubanTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(39)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_4)
.addComponent(book_classTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(35)
.addComponent(findButton, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE)
.addGap(11)))
.addGap(21))
);
lblNewLabel = new JLabel("图书名字:");
jieshuButton = new JButton("借阅");
jieshuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jieshuBook();
}
});
label = new JLabel("作者:");
label_5 = new JLabel("出版社:");
label_6 = new JLabel("状态:");
book_RBnameTxt = new JTextField();
book_RBnameTxt.setColumns(10);
book_RBzuozheTxt = new JTextField();
book_RBzuozheTxt.setColumns(10);
book_RBchubanTxt = new JTextField();
book_RBchubanTxt.setColumns(10);
book_RBstateTxt = new JTextField();
book_RBstateTxt.setColumns(10);
guihuanButton = new JButton("归还");
guihuanButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
returnBook();
}
});
JLabel label_7 = new JLabel("编号:");
book_RBidTxt = new JTextField();
book_RBidTxt.setColumns(10);
label_8 = new JLabel("类型:");
book_RBclassTxt = new JTextField();
book_RBclassTxt.setColumns(10);
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(28)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(lblNewLabel)
.addComponent(label)
.addComponent(label_5)
.addComponent(label_6)
.addComponent(label_7)
.addComponent(label_8, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE))
.addGap(32)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(book_RBidTxt, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
.addComponent(jieshuButton)
.addPreferredGap(ComponentPlacement.RELATED, 191, Short.MAX_VALUE)
.addComponent(guihuanButton))
.addComponent(book_RBzuozheTxt, 286, 305, Short.MAX_VALUE)
.addComponent(book_RBnameTxt, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
.addComponent(book_RBchubanTxt, 286, 305, Short.MAX_VALUE)
.addComponent(book_RBstateTxt, 286, 305, Short.MAX_VALUE)
.addComponent(book_RBclassTxt, 286, 305, Short.MAX_VALUE))
.addContainerGap())
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(22)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(book_RBnameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(24)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(book_RBzuozheTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_5)
.addComponent(book_RBchubanTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_6)
.addComponent(book_RBstateTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
.addComponent(book_RBidTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_7))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_8)
.addComponent(book_RBclassTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(guihuanButton)
.addComponent(jieshuButton))
.addContainerGap())
);
panel.setLayout(gl_panel);
bookTable = new JTable();
bookTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
bookTableMousePressed(e);
}
});
bookTable.setFillsViewportHeight(true);
bookTable.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"编号", "图书名称", "作者", "出版社", "状态","类型"
}
) {
boolean[] columnEditables = new boolean[] {
false, false, false, false, false,false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
scrollPane.setViewportView(bookTable);
contentPane.setLayout(gl_contentPane);
this.fillTable(new Book());
}
/**
* �黹ͼ�麯��
*/
protected void returnBook() {
String bookId = this.book_RBidTxt.getText();
String bookName = this.book_RBnameTxt.getText();
String bookChuban =this.book_RBchubanTxt.getText();
String bookZuozhe = this.book_RBzuozheTxt.getText();
String bookState = this.book_RBstateTxt.getText();
String bookClass = this.book_RBclassTxt.getText();
if(DeterminNull.isEmpty(bookId)) {
JOptionPane.showMessageDialog(null, "图书编号不能为空!");
return ;
}
Connection con = null;
try {
Jieyue bi = new Jieyue(Integer.parseInt(bookId));
Book book = new Book(Integer.parseInt(bookId));
book.setBook_state("0");//��ͼ��״̬�ı�Ϊ0
con = conutil.loding();
ResultSet rs = jieyueDao.returnn(con,bi);//�õ�����bookId����һ�С�
//���߶Աȣ�ֻ�н����Ķ��߲��ܻ�����
if((rs.next() && rs.getString(2).equals(duzheName))||((DeterminNull.isNotEmpty(guanliyName))&&rs.getString(5).equals("1"))) {
jieyueDao.delete(con, Integer.parseInt(bookId));//ɾ������������һ������
bookDao.update(con, book);//����ͼ����е�ͼ��״̬
JOptionPane.showMessageDialog(null, "归还成功");
return ;
}else{
JOptionPane.showMessageDialog(null, "归还失败");
return ;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
conutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* �����û�ID�������������
* @param id
*/
public static void setDuzheId(String name) {
duzheName = name;
}
public static void setGuanliyId(String name) {
guanliyName = name;
}
/**
* ����
*/
private void jieshuBook() {
String bookId = this.book_RBidTxt.getText();
String bookName = this.book_RBnameTxt.getText();
String bookChuban = this.book_RBchubanTxt.getText();
String bookZuozhe = this.book_RBzuozheTxt.getText();
String bookState = this.book_RBstateTxt.getText();
String bookClass = this.book_RBclassTxt.getText();
if(DeterminNull.isEmpty(bookId)) {
JOptionPane.showMessageDialog(null, "图书编号不能为空");
return;
}
if(bookState.equals("1")) {
JOptionPane.showMessageDialog(null, "该书已被借出");
return;
}
Connection con = null;
try {
con = conutil.loding();
Jieyue bi;
if(DeterminNull.isNotEmpty(duzheName)) {
bi = new Jieyue(Integer.parseInt(bookId), duzheName, null, null, "1");//����
}else {
bi = new Jieyue(Integer.parseInt(bookId), guanliyName, null, null, "1");//����Ա
}
Book book =new Book(Integer.parseInt(bookId), bookName, bookZuozhe, bookChuban,bookClass, "1");
int find = jieyueDao.add(con, bi);
int flag = bookDao.update(con, book);
if(1 != find ||1 != flag) {
System.out.print("a");
JOptionPane.showMessageDialog(null, "没有此书!");
return;
}else {
JOptionPane.showMessageDialog(null, "借阅成功!");
return;
}
} catch (Exception e) {
e.printStackTrace();
System.out.print("b");
JOptionPane.showMessageDialog(null, "借阅失败!");
return ;
}finally {
try {
conutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* ��ӵ������
* @param e
*/
private void bookTableMousePressed(MouseEvent e) {
int row = bookTable.getSelectedRow();
book_RBnameTxt.setText((String)bookTable.getValueAt(row, 1));
book_RBzuozheTxt.setText((String)bookTable.getValueAt(row, 2));
book_RBchubanTxt.setText((String)bookTable.getValueAt(row, 3));
book_RBstateTxt.setText((String)bookTable.getValueAt(row, 4));
book_RBidTxt.setText((String)bookTable.getValueAt(row, 0));
book_RBclassTxt.setText((String)bookTable.getValueAt(row, 5));
}
/**
* ͼ����Ϣ��ѯ
*/
private void findBookTable(ActionEvent e) {
//������ѯ�����ߣ�������
String book_name = this.book_nameTxt.getText();
String book_zuozhe = this.book_zuozheTxt.getText();
String book_chuban = this.book_chubanTxt.getText();
String book_class = this.book_classTxt.getText();
Book book =new Book();
book.setBook_name(book_name);
book.setBook_zuozhe(book_zuozhe);
book.setBook_chuban(book_chuban);
book.setBook_class(book_class);
this.fillTable(book);
}
/**
* ��ʼ��ͼ����Ϣ
* @param book
*/
private void fillTable(Book book) {
DefaultTableModel dtm = (DefaultTableModel) bookTable.getModel();
dtm.setRowCount(0);
Connection con = null;
try {
con = conutil.loding();
ResultSet rs = bookDao.find(con, book);
while(rs.next()) {
Vector v = new Vector();
v.add(rs.getString("book_id"));
v.add(rs.getString("book_name"));
v.add(rs.getString("book_zuozhe"));
v.add(rs.getString("book_chuban"));
v.add(rs.getString("book_class"));
if(rs.getString("book_state").equals("1")) {
v.add("已借出");
}else {
v.add("未借出");
}
dtm.addRow(v);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
conutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这里面没有显示数据,我感觉我的代码是没问题的,求大佬帮助!!!