📄 bookdatadialog.java
字号:
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.JTextPane;
import java.awt.Color;
public class BookDataDialog extends JDialog {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JLabel jLabel = null;
private JLabel jLabel2 = null;
private JScrollPane jScrollPane1 = null;
private JLabel jLabel1 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JLabel jLabel3 = null;
private Connection connection = null;
private String userId = null;
private String bookId = null;
private String status = null; // @jve:decl-index=0:
private JScrollPane jScrollPane2 = null;
private JTextPane jTextPane = null;
private Statement statement = null; // @jve:decl-index=0:
/**
* @param owner
*/
public BookDataDialog(Frame owner,Connection connection,String userId,String bookId) {
super(owner);
this.connection = connection;
this.userId = userId;
this.bookId = bookId;
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(566, 378);
this.setTitle("图书资料");
this.setContentPane(getJContentPane());
this.setLocation(300, 200);
try{
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
CallableStatement cs = connection.prepareCall("{call TESTSP}",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query = "select * from Book where id = "+"'"+bookId+"'";
ResultSet bookResultSet = statement.executeQuery(query);
bookResultSet.next();
String name = bookResultSet.getString("name");
String sort = bookResultSet.getString("sort");
String author = bookResultSet.getString("author");
String publish = bookResultSet.getString("publish");
String introduction = bookResultSet.getString("introduction");
String price = new Float(bookResultSet.getFloat("price")).toString();
String text ="<html>"
+"编号:" + "<font color = blue>" + bookId +"</font>"
+"<br><br>"
+"书名:" + "<font color = blue>" + name + "</font>"
+"<br><br>"
+"作者:" + "<font color = blue>" +author + "</font>"
+"<br><br>"
+"类别:" + "<font color = blue>"+ sort + "</font>"
+"<br><br>"
+"出版社:" + "<font color = blue>" +publish + "</font>"
+"<br><br>"
+"价格: " + "<font color = blue>" + price + "</font>"
+"<br><br>"
+"</html>";
jLabel1.setText(text);
jTextPane.setText("图书简介:\n "+introduction.trim());
jLabel.setIcon(new ImageIcon(bookResultSet.getBytes("cover")));
bookResultSet.close();
String query2 = "select * from Borrow where bookId = "+"'"+bookId+"'";
ResultSet borrowResultSet = statement.executeQuery(query2);
if(borrowResultSet.next()){
status = "已借出";
String text3 = "<html>"
+ "<font size = 5>"+"图书状态 :" + "</font>"
+ "<font size = 5 color = red >"+status+"</font>"
+ "</html>";
jLabel3.setText(text3);
}else{
status = "在馆";
String text3 = "<html>"
+ "<font size = 5>"+"图书状态 :"+ "</font>"
+ "<font size = 5 color = green >"+status+"</font>"
+ "</html>";
jLabel3.setText(text3);
}
borrowResultSet.close();
//statement.close();
}catch(Exception sqlE){
sqlE.printStackTrace();
}
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
show(false);
//System.exit(1);
//System.out.println("windowClosing()"); // TODO Auto-generated Event stub windowClosing()
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(9, 212, 169, 45));
jLabel3.setText("");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(1, 293, 54, 54));
jLabel2.setText("");
jLabel2.setIcon(new ImageIcon("Icon\\fox.gif"));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(getJScrollPane1(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(jLabel3, null);
jContentPane.add(getJScrollPane2(), null);
}
return jContentPane;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jLabel = new JLabel();
jLabel.setText("");
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(6, 6, 173, 203));
jScrollPane.setViewportView(jLabel);
}
return jScrollPane;
}
/**
* This method initializes jScrollPane1
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane1() {
if (jScrollPane1 == null) {
jLabel1 = new JLabel();
jLabel1.setText("");
jLabel1.setBackground(Color.lightGray);
jScrollPane1 = new JScrollPane();
jScrollPane1.setBounds(new Rectangle(185, 8, 149, 253));
jScrollPane1.setViewportView(jLabel1);
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
}
return jScrollPane1;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(226, 290, 72, 33));
jButton.setText("借阅");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
//connection.setAutoCommit(false);
//boolean status;
String query = "select * from Borrow where bookId = "+"'"+bookId+"'";
ResultSet borrowResultSet = statement.executeQuery(query);
if(!borrowResultSet.next()){
//status = false;
String insert = "insert into Borrow "
+ "values"
+ "("
+ "'" + userId + "',"
+ "'" + bookId + "',"
+ "'" + GetTime.currentTime() + "'"
+ ")";
statement.executeUpdate(insert);
status = "已借出";
String text3 = "<html>"
+ "<font size = 5>"+"图书状态 :" + "</font>"
+ "<font size = 5 color = red >"+status+"</font>"
+ "</html>";
jLabel3.setText(text3);
JOptionPane.showMessageDialog(null, "你已经成功借阅这本书","借阅成功",JOptionPane.INFORMATION_MESSAGE);
//System.out.println(insert);
}else{
//status = true;
JOptionPane.showMessageDialog(null, "书籍已经被借出", "借阅不成功", JOptionPane.ERROR_MESSAGE);
}
}catch(Exception sqlE){
sqlE.printStackTrace();
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(389, 291, 72, 33));
jButton1.setText("归还");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
String query = "select * from Borrow "
+ " where readerId = "
+ "'"+userId +"'"
+ " and bookId = "
+ "'"+bookId+"'";
//System.out.println(query);
ResultSet borrowResultSet = statement.executeQuery(query);
if(borrowResultSet.next()){
String delete = "delete from Borrow "
+ " where readerId = "
+ "'"+userId +"'"
+ " and bookId = "
+ "'"+bookId+"'";
statement.executeUpdate(delete);
//System.out.println(delete);
status = "在馆";
String text3 = "<html>"
+ "<font size = 5>"+"图书状态 :" + "</font>"
+ "<font size = 5 color = green >"+status+"</font>"
+ "</html>";
jLabel3.setText(text3);
JOptionPane.showMessageDialog(null, "你已经成功归还这本书","归还成功",JOptionPane.INFORMATION_MESSAGE);
//System.out.println(insert);
}else{
//status = true;
JOptionPane.showMessageDialog(null, "您没有借阅这本书", "归还不成功", JOptionPane.ERROR_MESSAGE);
}
}catch(Exception sqlE){
sqlE.printStackTrace();
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton1;
}
/**
* This method initializes jScrollPane2
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane2() {
if (jScrollPane2 == null) {
jScrollPane2 = new JScrollPane();
jScrollPane2.setBounds(new Rectangle(344, 10, 208, 250));
jScrollPane2.setBackground(Color.white);
jScrollPane2.setViewportView(getJTextPane());
jScrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
}
return jScrollPane2;
}
/**
* This method initializes jTextPane
*
* @return javax.swing.JTextPane
*/
private JTextPane getJTextPane() {
if (jTextPane == null) {
jTextPane = new JTextPane();
jTextPane.setEditable(false);
}
return jTextPane;
}
} // @jve:decl-index=0:visual-constraint="170,13"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -