March 6th, 2003, 08:52 PM
-
Problem with JSP/Beans/mySQL
Hey all, I'm trying to run a simple JavaBean from a JSP page that displays a table from a mySQL database. I keep getting an error that reads:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
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: 6 in the jsp file: /Select.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:\jakarta-tomcat-4.1.18-LE-jdk14\work\Standalone\localhost\_\Select_jsp.java:48: cannot resolve symbol
symbol : class DataBaseSelect
location: class org.apache.jsp.Select_jsp
DataBaseSelect select = null;
^
An error occurred at line: 6 in the jsp file: /Select.jsp
Generated servlet error:
C:\jakarta-tomcat-4.1.18-LE-jdk14\work\Standalone\localhost\_\Select_jsp.java:50: cannot resolve symbol
symbol : class DataBaseSelect
location: class org.apache.jsp.Select_jsp
select = (DataBaseSelect) pageContext.getAttribute("select", PageContext.REQUEST_SCOPE);
^
An error occurred at line: 6 in the jsp file: /Select.jsp
Generated servlet error:
C:\jakarta-tomcat-4.1.18-LE-jdk14\work\Standalone\localhost\_\Select_jsp.java:53: cannot resolve symbol
symbol : class DataBaseSelect
location: class org.apache.jsp.Select_jsp
select = (DataBaseSelect) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "DataBaseSelect");
^
3 errors
I'm not sure exactly what to do - I have placed DataBaseSelect.class in nearly every subdirectory, but nothing works. The code for the JSP and Bean is below:
DataBaseSelect.java:
import java.sql.*;
import java.util.Vector;
public class DataBaseSelect {
private Vector result;
public DataBaseSelect() {
result = new Vector();
} // constructor DataBaseSelect
public String connect() {
try {
Class.forName("socketFactory=com.mysql.jdbc.NamedPipeSocketFactory").newInstance();
return "Driver Loaded!";
} catch (Exception E) {
return "Unable to load driver.";
}
}
public String select() {
try {
Connection C = DriverManager.getConnection("jdbc:mysql://localhost/test?user=xxxx&password=123");
Statement Stmt = C.createStatement();
ResultSet myResult = Stmt.executeQuery("SELECT * from users");
while (myResult.next()) {
result.addElement(myResult.getString(1));
}
// Clean up
myResult.close();
Stmt.close();
C.close();
return "Connection Success!";
} catch (SQLException E) {
return "SQLException: " + E.getMessage();
}
}
/**
* Accessor for result
**/
public Vector getResult() {
return result;
}
/**
* Mutator for result
**/
public void setResult(Vector avector) {
result = avector;
}
} // class DataBaseSelect
Select.jsp:
<html>
<head>
<title>Select everything from a database</title>
</head>
<body>
<jsp:useBean id="select" class="DataBaseSelect" scope="request">
</jsp:useBean>
<% out.print(select.connect()); %>
<br>
<% out.print(select.select()); %>
<p>Format results
<br>
<%@ page import="java.util.Vector" %>
<% Vector aResult = select.getResult(); %>
<table>
<% for (int i=0; i < aResult.size(); i++) { %>
<tr>
<td>
<% out.print(aResult.elementAt(i)); %>
</td>
</tr>
<% } %>
</table>
</body>
</html>
Thanks for the help!
March 9th, 2003, 05:25 PM
-
The DataBaseSelect class needs to be in the WEB-INF/classes directory
March 9th, 2003, 06:45 PM
-
Hey,
The DataBaseSelect class is in the WEB-INF/classes directory, which is why this makes no sense to me. Anyone have an idea what could be causing this problem?
Thanks again for the help.
March 10th, 2003, 09:04 AM
-
FOr some reason putting class files in the root classes directory throws the app server for a loop (it is technically not good form to not use a package anyway). To get yours working you can import the class at the top of the jsp:
Code:
<%@ page import="DataBaseSelect" %>
Alternatively, if you put the DataBaseSelect in a package and qualify your useBean with the package name you can avoid this problem.
For instance, if you put the DataBaseSelect in a package called "db", you could use this in your jsp without any changes
Code:
<jsp:useBean id="select" class="db.DataBaseSelect" scope="request">
Hope that helps.
March 11th, 2003, 01:09 AM
-
Hey,
I tried both of those suggestions but Tomcat is still generating an error message - either the same one if I do the page import line or an error indicating that the package db does not exist if I do the second suggestion.
Thanks for your help.