Posted By:
dolfandave_uyemura
Posted On:
Friday, November 28, 2003 09:25 AM
Wrox Press Ltd. Hi all, I am learning JSP by first working on applications that run on my standalone computer at home. That is as far as I have gotten so far. The error: "Unable to compile class for JSP" is causing me great frustration. I have successfully installed Jakarta-Tomcat-4.1.24, Java (j2sdk1.4.1_01), the JDBC driver "com.mysql.jdbc.Driver". I have created and interacted with a MySQL(4.0.16) database successfully by running by running other Java applications that did things like add and delete recors so that part isn't a problem. Now I venture into the area of Beans. This is my first attempt and it has got me stuck. Any help is going to keep me from losing more sleep:). I saved a file
More>>
Wrox Press Ltd.
Hi all,
I am learning JSP by first working on applications that run on my standalone computer at home. That is as far as I have gotten so far. The error: "Unable to compile class for JSP" is causing me great frustration. I have successfully installed Jakarta-Tomcat-4.1.24, Java (j2sdk1.4.1_01), the JDBC driver "com.mysql.jdbc.Driver". I have created and interacted with a MySQL(4.0.16) database successfully by running by running other Java applications that did things like add and delete recors so that part isn't a problem. Now I venture into the area of Beans. This is my first attempt and it has got me stuck. Any help is going to keep me from losing more sleep:).
I saved a file called Books.java in C:jakarta-tomcat-4.1.24webapps egjsp-ch16web-infclassescomwroxdatabases. I also saved five files called add.jsp, booklist.jsp, delete.jsp, error.jsp, and newbook.jsp to C:jakarta-tomcat-4.1.24webapps egjsp-ch16. I then went to my DOS prompt and compiled Books.java which created the Book.class file. I copied Book.class to C:jakarta-tomcat-4.1.24webapps egjsp-ch16web-inflib which I got from someone else. I am not sure if this part is correct.
I then went to my Internet Explorer and tried to access this page: http://localhost:8080/begjsp-ch16/booklist.jsp. This is where I get the error below. I did go to the tomcat site and try their examples to make sure JSP was working.
=========
HTTP Status 500 - type Exception report
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: -1 in the jsp file: null
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] C:jakarta-tomcat-4.1.24workStandalonelocalhost egjsp-ch16 ooklist_jsp.java:10: package com.wrox.databases does not exist
[javac] import com.wrox.databases.*;
[javac] ^
[javac] C:jakarta-tomcat-4.1.24workStandalonelocalhost egjsp-ch16 ooklist_jsp.java:46: package com.wrox.databases does not exist
[javac] com.wrox.databases.Books book = null;
[javac] ^
[javac] C:jakarta-tomcat-4.1.24workStandalonelocalhost egjsp-ch16 ooklist_jsp.java:48: package com.wrox.databases does not exist
[javac] book = (com.wrox.databases.Books) pageContext.getAttribute("book", PageContext.PAGE_SCOPE);
[javac] ^
[javac] C:jakarta-tomcat-4.1.24workStandalonelocalhost egjsp-ch16 ooklist_jsp.java:51: package com.wrox.databases does not exist
[javac] book = (com.wrox.databases.Books) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "com.wrox.databases.Books");
[javac] ^
[javac] 4 errors
=========
Books.java looks like this:
=========
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));
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);
}
}
}
=========
Booklist.jsp:
=========
<%@ page language="java"
import="java.sql.*, java.io.*, java.util.*, com.wrox.databases.*"
errorPage="error.jsp" %>
Wrox Press Ltd.
List of Books
Add More Books
<% book.disconnect(); %>
=========
Thanks again,
ddave