📄 bookadd.java
字号:
package library.book;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import library.hibernate.Books;
import library.main.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
* 书籍添加类,将新增加的图书信息保存到数据库中去
*
* @author lianhw
*
*/
public class BookAdd extends JFrame implements ActionListener {
JPanel panel1, panel2;
JLabel bookNameLabel, pressNameLabel, authorLabel, addressLabel,
pressDateLabel, priceLabel, bookCountLabel, commentLabel;
JTextField bookNameText, pressNameText, authorText, addressText,
pressDateText, priceText, bookCountText, commentText;
Container container;
JButton clearButton, addButton, exitButton;
public BookAdd() {
super("添加书籍信息");
container = getContentPane();
container.setLayout(new BorderLayout());
//“名称”标签
bookNameLabel = new JLabel("名称", JLabel.CENTER);
//“出版社”标签
pressNameLabel = new JLabel("出版社", JLabel.CENTER);
//“作者”标签
authorLabel = new JLabel("作者", JLabel.CENTER);
//“地址”标签
addressLabel = new JLabel("地址", JLabel.CENTER);
//“出版日期”标签
pressDateLabel = new JLabel("出版日期", JLabel.CENTER);
//“价格”标签
priceLabel = new JLabel("价格", JLabel.CENTER);
//“新书数目”标签
bookCountLabel = new JLabel("新书数目", JLabel.CENTER);
//“备注”标签
commentLabel = new JLabel("备注", JLabel.CENTER);
//输入书名文本框
bookNameText = new JTextField(15);
//输入出版社名字文本框
pressNameText = new JTextField(15);
//输入作者姓名文本框
authorText = new JTextField(15);
//输入地址文本框
addressText = new JTextField(15);
//输入出版日期文本框
pressDateText = new JTextField(15);
//输入价格文本框
priceText = new JTextField(15);
//输入图书数量文本框
bookCountText = new JTextField(15);
//输入图书备注文本框
commentText = new JTextField(15);
panel1 = new JPanel();
panel1.setLayout(new GridLayout(8, 2));
panel1.add(bookNameLabel);
panel1.add(bookNameText);
panel1.add(pressNameLabel);
panel1.add(pressNameText);
panel1.add(authorLabel);
panel1.add(authorText);
panel1.add(addressLabel);
panel1.add(addressText);
panel1.add(pressDateLabel);
panel1.add(pressDateText);
panel1.add(priceLabel);
panel1.add(priceText);
panel1.add(bookCountLabel);
panel1.add(bookCountText);
panel1.add(commentLabel);
panel1.add(commentText);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1, 3));
//“清空”按钮
clearButton = new JButton("清空");
//为“清空”按钮添加事件监听者
clearButton.addActionListener(this);
//“添加”按钮
addButton = new JButton("添加");
//为“添加”按钮添加事件监听者
addButton.addActionListener(this);
//退出按钮
exitButton = new JButton("退出");
//为“退出”按钮添加事件监听者
exitButton.addActionListener(this);
panel2.add(clearButton);
panel2.add(addButton);
panel2.add(exitButton);
container.add(panel1, BorderLayout.CENTER);
container.add(panel2, BorderLayout.SOUTH);
}
/**
* 动作响应方法,将新增的图书信息提交到数据库中
*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent action) {
if (action.getSource() == exitButton) {
// 单击“退出”按钮不作任何事情
this.dispose();
} else if (action.getSource() == clearButton) {
// 单击“清空”按钮将所有输入框清空
bookNameText.setText("");
pressNameText.setText("");
authorText.setText("");
addressText.setText("");
pressDateText.setText("");
priceText.setText("");
bookCountText.setText("");
commentText.setText("");
} else if (action.getSource() == addButton) {
// 判断书名是否为空
if (bookNameText.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "书名不能为空!");
} else if (pressNameText.getText().trim().equals("")) {
// 判断出版社是否为空
JOptionPane.showMessageDialog(null, "出版社不能为空!");
} else if (authorText.getText().trim().equals("")) {
// 判断作者是否为空
JOptionPane.showMessageDialog(null, "作者不能为空!");
} else if (bookCountText.getText().trim().equals("")) {
// 判断新书数目是否为空
JOptionPane.showMessageDialog(null, "新书数目不能为空!");
} else {
// 取得SessionFactory
SessionFactory sessionFactory = HibernateUtil
.getSessionFactory();
// 打开session
Session session = sessionFactory.openSession();
// 创建一个事务
Transaction tx = session.beginTransaction();
// 创建UserTable对象
Books book = new Books();
book.setBookName(bookNameText.getText().trim());
book.setPress(pressNameText.getText().trim());
book.setAuthor(authorText.getText().trim());
book.setAddress(addressText.getText().trim());
book.setPressDate(new GregorianCalendar().getTime());
book.setPrice(new Double(priceText.getText().trim()));
book.setBooksCount(new Integer(bookCountText.getText().trim()));
book.setCom(commentText.getText().trim());
session.saveOrUpdate(book);
// 事务提交
tx.commit();
// 关闭session
session.close();
JOptionPane.showMessageDialog(null, "添加书籍成功!");
this.dispose();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -