I continue to get the same error. This is getting frustrating. I compiled and ran a few scripts that interact with a MySQL database per Beginning JSP Web Development by Wrox Press. I have no problem interacting with the database but when I try to go to the localhost site (
http://localhost:8080/begjsp-ch16/booklist.jsp), I get the errors I initially posted. The booklist.jsp file looks like this:
<%@ page language="java"
import="java.sql.*, java.io.*, java.util.*, com.wrox.databases.*"
errorPage="error.jsp" %>
<jsp:useBean id="book" class="com.wrox.databases.Books" />
<html>
<head>
<title> Wrox Press Ltd. </title>
</head>
<body>
<h1> Wrox Press Ltd.</h1>
<h2> List of Books </h2>
<a href="newbook.jsp"><b>Add More Books</b></a>
<form action="delete.jsp" method="post">
<table border="1">
<tr>
<td><b>ID:</b></td>
<td><B>Title:</b></td>
<td><B>Price:</b></td>
</tr>
<%
book.connect();
ResultSet rs = book.viewBooks();
while (rs.next()) {
%>
<tr>
<td>
<input type="checkbox" name="pkey"
value="<%= rs.getString("Title_ID") %>" />
</td>
<td><%= rs.getString("Title") %></td>
<td><%= rs.getString("Price") %></td>
</tr>
<%
}
%>
</table><br />
Check books for deletion.<BR>
<input type="submit" value="Delete All Checked Books">
</form>
<% book.disconnect(); %>
</body>
</html>
===================================
and is in this file: c:\jakarta-tomcat-4.1.24\webapps\begjsp-ch16
The Books.java application is in c:\jakarta-tomcat-4.1.24\webapps\begjsp-ch16\web-inf\classes\com\wrox\databases per very specific instructions from the text.
Books.java is as follows:
package com.wrox.databases;
import java.sql.*;
import java.util.*;
public class Books {
String error;
Connection con;
public Books() { }
public void connect() throws ClassNotFoundException,
SQLException,
Exception {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(
"jdbc:mysql://localhost/Wrox ?user=root&password=");
} catch (ClassNotFoundException cnfe) {
error = "ClassNotFoundException: Could not locate DB driver.";
throw new ClassNotFoundException(error);
} catch (SQLException cnfe) {
error = "SQLException: Could not connect to database.";
throw new SQLException(error);
} catch (Exception e) {
error = "Exception: An unknown error occurred while connecting " +
"to database.";
throw new Exception(error);
}
}
public void disconnect() throws SQLException {
try {
if ( con != null ) {
con.close();
}
} catch (SQLException sqle) {
error = ("SQLException: Unable to close the database connection.");
throw new SQLException(error);
}
}
public ResultSet viewBooks() throws SQLException, Exception {
ResultSet rs = null;
try {
String queryString = ("SELECT * FROM Book;");
Statement stmt = con.createStatement();
rs = stmt.executeQuery(queryString);
} catch (SQLException sqle) {
error = "SQLException: Could not execute the query.";
throw new SQLException(error);
} catch (Exception e) {
error = "An exception occured while retrieving books.";
throw new Exception(error);
}
return rs;
}
public void addBooks(int id, String title, float price, int cid)
throws SQLException, Exception {
if (con != null) {
try {
PreparedStatement updatebooks;
updatebooks = con.prepareStatement(
"insert into Book values(?, ?, ?, ?);");
updatebooks.setInt(1, id);
updatebooks.setString(2, title);
updatebooks.setInt(3, cid);
updatebooks.setFloat(4, price);
updatebooks.execute();
} catch (SQLException sqle) {
error = "SQLException: update failed, possible duplicate entry";
throw new SQLException(error);
}
} else {
error = "Exception: Connection to database was lost.";
throw new Exception(error);
}
}
public void removeBooks(String [] pkeys) throws SQLException, Exception {
if (con != null) {
try {
PreparedStatement delete;
delete = con.prepareStatement("DELETE FROM Book WHERE Title_ID=?;");
for (int i = 0; i < pkeys.length; i++) {
delete.setInt(1, Integer.parseInt(pkeys[i]));
delete.execute();
}
} catch (SQLException sqle) {
error = "SQLException: update failed, possible duplicate entry";
throw new SQLException(error);
} catch (Exception e) {
error = "An exception occured while deleting books.";
throw new Exception(error);
}
} else {
error = "Exception: Connection to database was lost.";
throw new Exception(error);
}
}
}
==============================
Thanks again,
ddave