package bookmanager;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.*;
import java.sql.*; //引入包java.sql.*;
import javax.swing.table.*; //引入包sqljavax.swing.table.*;
import java.util.*; //引入包sqljava.util*;
import java.awt.event.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2003
* Company:
* @author not attributable
* @version 1.0
*/
public class bookList
extends JFrame {
ResultSet rs; //定义结果集
private DBManager db = new DBManager(); //定义数据库操作对象
Vector tempvector = new Vector(1, 1);
DefaultTableModel model = new DefaultTableModel(); //定义DefaultTableModel模型
JTable jTableResult = new JTable(model); //定义model型JTable
XYLayout xYLayout1 = new XYLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JButton jButtonSearch = new JButton();
JLabel jLabel1 = new JLabel();
JTextField jTextFieldname = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField jTextFieldpress = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField jTextFieldauthor = new JTextField();
public bookList() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
bookList bookList = new bookList();
}
private void jbInit() throws Exception {
xYLayout1.setWidth(564);
xYLayout1.setHeight(374);
this.getContentPane().setLayout(xYLayout1);
jButtonSearch.setText("查询");
jButtonSearch.addMouseListener(new bookList_jButtonSearch_mouseAdapter(this));
jButtonSearch.setFont(new java.awt.Font("Dialog", 0, 16));
// model中加入列名
model.addColumn("书名");
model.addColumn("出版社");
model.addColumn("作者");
model.addColumn("地址");
model.addColumn("出版日期");
model.addColumn("定价");
model.addColumn("评论");
jLabel1.setFont(new java.awt.Font("Dialog", 0, 16));
jLabel1.setText("名称");
jTextFieldname.setFont(new java.awt.Font("Dialog", 0, 16));
jTextFieldname.setText("");
jLabel2.setFont(new java.awt.Font("Dialog", 0, 16));
jLabel2.setText("出版社");
jTextFieldpress.setFont(new java.awt.Font("Dialog", 0, 16));
jTextFieldpress.setText("");
jLabel3.setText("作者");
jLabel3.setFont(new java.awt.Font("Dialog", 0, 16));
jTextFieldauthor.setFont(new java.awt.Font("Dialog", 0, 16));
jTextFieldauthor.setText("");
this.getContentPane().add(jScrollPane1, new XYConstraints(18, 102, 530, 253));
this.getContentPane().add(jLabel1, new XYConstraints(27, 21, 92, 27));
this.getContentPane().add(jTextFieldname,
new XYConstraints(138, 22, 116, -1));
this.getContentPane().add(jTextFieldauthor,
new XYConstraints(384, 20, 139, 27));
this.getContentPane().add(jLabel3, new XYConstraints(305, 23, 62, -1));
this.getContentPane().add(jLabel2, new XYConstraints(24, 67, 91, -1));
this.getContentPane().add(jTextFieldpress,
new XYConstraints(137, 59, 117, 28));
this.getContentPane().add(jButtonSearch, new XYConstraints(385, 59, 136, 28));
jScrollPane1.add(jTableResult, null);
}
//加载数据
void jButtonSearch_mouseClicked(MouseEvent e) {
String strSQL;
//
boolean isFirst = true;
//检验书目是否存在
strSQL = "select * from Books ";
if (!jTextFieldname.getText().trim().equals("")) {
strSQL = strSQL + " where bookname='" + jTextFieldname.getText().trim() +
"'";
isFirst = false;
}
if (!jTextFieldpress.getText().trim().equals("")) {
if (isFirst) {
strSQL = strSQL + " where press='" + jTextFieldpress.getText().trim() +
"'";
isFirst = false;
}
else {
strSQL = strSQL + " and press='" + jTextFieldpress.getText().trim() +
"'";
}
}
if (!jTextFieldauthor.getText().trim().equals("")) {
if (isFirst) {
strSQL = strSQL + " where author='" + jTextFieldauthor.getText().trim() +
"'";
isFirst = false;
}
else {
strSQL = strSQL + " and author='" + jTextFieldauthor.getText().trim() +
"'";
}
}
rs = db.getResult(strSQL);
boolean isexist = false;
try {
isexist = rs.first();
}
catch (SQLException ex1) {
JOptionPane.showMessageDialog(null, "ex1.toString() " + ex1.toString());
}
//若书目不存在,提示警告信息提醒用户
if (!isexist) {
JOptionPane.showMessageDialog(null, "书库中没有书目,请确认!");
}
//若存在,执行更新操作
else {
//首先清空集合中的对象
tempvector.clear();
//清空model中的对象
for (int n = model.getRowCount() - 1; n >= 0; n--)
model.removeRow(n);
try {
rs.beforeFirst();
while (rs.next()) {
Vector tempvector=new Vector(1,1);
tempvector.add(rs.getString("bookname"));
tempvector.add(rs.getString("press"));
tempvector.add(rs.getString("author"));
tempvector.add(rs.getString("address"));
tempvector.add(rs.getString("pressdate"));
tempvector.add(rs.getString("price"));
tempvector.add(rs.getString("comment"));
// System.out.println(rs.getString("bookname"));
model.addRow(tempvector);
// tempvector.clear();
for (int n = 0; n < tempvector.size(); n++) {
System.out.println( (String) tempvector.get(n));
}
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "ex.toString() :" + ex.toString());
}
}
}
}
class bookList_jButtonSearch_mouseAdapter
extends java.awt.event.MouseAdapter {
bookList adaptee;
bookList_jButtonSearch_mouseAdapter(bookList adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jButtonSearch_mouseClicked(e);
}
}