Java图书馆管理系统执行查询语句时抛出异常、求助
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=查询,when=1353773128072,modifiers=] on button0请问这是什么原因? 该怎么解决?、
源代码:
package MainPro;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import PublicMoudle.*;
public class BookQuery extends Frame {
JTable table;
JScrollPane scrollPane;
Label lbbookname = new Label("图书名称");
Label lbauthor = new Label("作者");
Label lbpublisher = new Label("出版社");
TextField tf_bookname = new TextField("");
TextField tf_author = new TextField("");
TextField tf_publisher = new TextField("");
Button queryBtn = new Button("查询");
Button closeBtn = new Button("关闭");
String[] heads = {"图书编号","图书名称","图书类别","作者","译者","出版社","出版时间","定价","库存数量"};
public BookQuery() {
setTitle("图书查询");
setSize(800,500);
setLayout(null);
lbbookname.setBounds(170,40,50,20);
tf_bookname.setBounds(230,40,160,20);
lbauthor.setBounds(410,40,50,20);
tf_author.setBounds(470,40,160,20);
lbpublisher.setBounds(170,80,50,20);
tf_publisher.setBounds(230,80,160,20);
queryBtn.setBounds(300,120,80,25);
queryBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn_queryActionPerformed(e);
}
});
closeBtn.setBounds(420,120,80,25);
closeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
add(lbbookname);
add(tf_bookname);
add(lbauthor);
add(tf_author);
add(lbpublisher);
add(tf_publisher);
add(queryBtn);
add(closeBtn);
setLocationRelativeTo(null);
setVisible(true);
}
private void btn_queryActionPerformed(ActionEvent e) {
try {
String bookname,author,publisher;
String sql,sql1,sql2,sql3,sql4;
bookname = tf_bookname.getText();
author = tf_author.getText();
publisher = tf_publisher.getText();
sql = "select * from book";
if (bookname.equals("")) {
sql1 = "";
} else {
sql1 = "bookname like '" + bookname +"'";
}
if (author.equals("")) {
sql2 = "";
} else {
sql2 = "author like '" + author +"'";
if (!bookname.equals("")) {
sql2 = " and " + sql2;
}
}
if (publisher.equals("")) {
sql3 = "";
} else {
sql3 = "publisher like '" + publisher + "'";
if (!(author.equals("") && bookname.equals(""))) {
sql3 = " and " + sql3;
}
}
sql4 = sql1 + sql2 +sql3;
if (!sql4.equals("")) {
sql = sql + " where " + sql4;
}
ResultSet rs = DbOp.executeQuery(sql);
Object[][] bookq = new Object[30][heads.length];
int i = 0;
while (rs.next()) {
bookq[i][0] = rs.getString("id");
bookq[i][1] = rs.getString("bookname");
bookq[i][2] = rs.getString("booktype");
bookq[i][3] = rs.getString("author");
bookq[i][4] = rs.getString("translator");
bookq[i][5] = rs.getString("publisher");
bookq[i][6] = rs.getString("pubish_time");
bookq[i][7] = rs.getFloat("price");
bookq[i][8] = rs.getInt("stock");
i++;
}
table = new JTable(bookq,heads);
scrollPane = new JScrollPane(table);
scrollPane.setBounds(20,160,760,300);
add(scrollPane);
} catch (SQLException e1) {
System.out.println(e);
}
}
}
--------------------编程问答-------------------- 代码不完整,可能是你的db工具出问题了。
ResultSet rs = DbOp.executeQuery(sql);
把dbop代码粘出来 --------------------编程问答--------------------
package PublicMoudle;
import java.sql.*;
public class DbOp {
static String Driver = "com.mysql.Jdbc.Driver";
static String url = "jdbc:mysql://localhost:3306/library_manage_system";
static String username = "root";
static String password = "548326";
static Connection con = null;
public static ResultSet executeQuery(String sql) {
try {
Class.forName(Driver);
} catch (ClassNotFoundException e) {
System.out.println(e);
}
try {
con = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
System.out.println(e);
}
try {
return con.createStatement().executeQuery(sql);
} catch (SQLException e) {
System.out.println(e);
return null;
}
}
public static int executeUpdate(String sql) {
try {
Class.forName(Driver);
} catch (ClassNotFoundException e) {
System.out.println(e);
}
try {
con = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
System.out.println(e);
}
try {
return con.createStatement().executeUpdate(sql);
} catch (SQLException e) {
System.out.println(e);
return -1;
}
}
public static void Close() {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
System.out.println(e);
}
}
} --------------------编程问答--------------------
此工具有问题的。
你先用这个吧,http://my.csdn.net/my/code/detail/28195
把驱动换成mysql的,
补充:Java , Eclipse