jsp object instantiation can't find driver

B

ben

I have the following object on my server:

User.java
-------------------------------
public class User {

private Connection con;
private Statement stmt;

public User() {

try{

Class.forName("com.mysql.jdbc.Driver").newInstance();

con =
DriverManager.getConnection("jdbc:mysql://localhost.localdomain/blah?user=blah");
stmt = con.createStatement();
} catch (Exception e){
e.printStackTrace();
}
}
/*auxiliary method for getMembers(String), so that you don't
have to pass anything if you want all the members*/
public ResultSet getMembers() {
return getMembers(null);
}

public ResultSet getMembers(String param) {
String sql = null;
if(param == null){
sql = "SELECT * FROM Member";
}

/* check to make sure that a value has been assigned to sql
if not, then it is a good form of error checking and
we'll possibly throw an exception later */
try{
return stmt.executeQuery(sql);
} catch(Exception e){
e.printStackTrace();
return null;
}

}

protected void finalize() throws Throwable{
super.finalize();
stmt.close();
con.close();
}
}

and I have this driver class:

driver.java
------------------
public class driver {
public static void main(String[] args){
User u = new User();
ResultSet r = u.getMembers();
try {
r.next();
System.out.println(r.getString("user_name"));
} catch (Exception e) {
System.out.println("prob in driver");
}
}
}

I can run driver fine if I use the following command:

java -cp .:/usr/local/jakarta-tomcat-4.1.29/common/lib/mysql-connector-java-3.0.11-stable-bin.jar
driver

so I know that the object works. the problem occurs when I try to
instantiate this object within a jsp page. it gives me an error,
telling me that it can't find the driver com.mysql.jdbc.Driver

I think I somehow need to set the classpath under which my jsp pages
operate, since I get the same error if I try to run:

java driver

without giving the classpath.

I've placed the driver within $CATALINA_HOME/common/lib/

so I'm not really sure why my jsp pages are not able to find the
driver class.

Does anybody have any ideas?

Thanks you,

Ben
 
J

Jason Bell

Your driver jar file should be within the App servers collection of used
files.
For Tomcat it's
$TOMCAT_HOME/common/lib

Place the MySQL driver in there, restart the server and it "should" work.

Regards
Jase Bell
 
B

ben

on my system, $TOMCAT_HOME = $CATALINA_HOME. So i'm placing my driver
jar in: $CATALINA_HOME/common/lib/ which translates to:
/usr/local/jakarta-tomcat-4.1.29/common/lib/

So its in the right directory. Here is a copy of my jsp file:

<%@ page contentType="text/html; charset=iso-8859-1" language="java"
import="com.snipjib.database.User,java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Using com.snipjib.database.User</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body>

<table>
<tr>
<td>Name</td>
</tr>

<%
try{
User u = new User();
ResultSet rset = u.getMembers();

while(rset.next()){
%>
<tr>
<td><%=rset.getString("user_name") %></td>
</tr>
<% }

} catch(Exception e) { %>
Problem!
<% }
%>
</table>
</body>
</html>

and here is a copy of the top of the error message from tomcat
(User.java is in com.snipjib.database)

org.apache.jasper.JasperException:
com.snipjib.database.User.getMembers()Ljava/sql/ResultSet;
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

Any clue as to what going on?

Thanks for the help!

Ben

Jason Bell said:
Your driver jar file should be within the App servers collection of used
files.
For Tomcat it's
$TOMCAT_HOME/common/lib

Place the MySQL driver in there, restart the server and it "should" work.

Regards
Jase Bell


ben said:
I have the following object on my server:

User.java
-------------------------------
public class User {

private Connection con;
private Statement stmt;

public User() {

try{

Class.forName("com.mysql.jdbc.Driver").newInstance();

con =
DriverManager.getConnection("jdbc:mysql://localhost.localdomain/blah?user=bl
ah");
stmt = con.createStatement();
} catch (Exception e){
e.printStackTrace();
}
}
/*auxiliary method for getMembers(String), so that you don't
have to pass anything if you want all the members*/
public ResultSet getMembers() {
return getMembers(null);
}

public ResultSet getMembers(String param) {
String sql = null;
if(param == null){
sql = "SELECT * FROM Member";
}

/* check to make sure that a value has been assigned to sql
if not, then it is a good form of error checking and
we'll possibly throw an exception later */
try{
return stmt.executeQuery(sql);
} catch(Exception e){
e.printStackTrace();
return null;
}

}

protected void finalize() throws Throwable{
super.finalize();
stmt.close();
con.close();
}
}

and I have this driver class:

driver.java
------------------
public class driver {
public static void main(String[] args){
User u = new User();
ResultSet r = u.getMembers();
try {
r.next();
System.out.println(r.getString("user_name"));
} catch (Exception e) {
System.out.println("prob in driver");
}
}
}

I can run driver fine if I use the following command:

java -cp .:/usr/local/jakarta-tomcat-4.1.29/common/lib/mysql-connector-java-3.0.11-st
able-bin.jar
driver

so I know that the object works. the problem occurs when I try to
instantiate this object within a jsp page. it gives me an error,
telling me that it can't find the driver com.mysql.jdbc.Driver

I think I somehow need to set the classpath under which my jsp pages
operate, since I get the same error if I try to run:

java driver

without giving the classpath.

I've placed the driver within $CATALINA_HOME/common/lib/

so I'm not really sure why my jsp pages are not able to find the
driver class.

Does anybody have any ideas?

Thanks you,

Ben
 
R

Ryan Stewart

ben said:
on my system, $TOMCAT_HOME = $CATALINA_HOME. So i'm placing my driver
jar in: $CATALINA_HOME/common/lib/ which translates to:
/usr/local/jakarta-tomcat-4.1.29/common/lib/

So its in the right directory. Here is a copy of my jsp file:
*snip JSP*

and here is a copy of the top of the error message from tomcat
(User.java is in com.snipjib.database)

org.apache.jasper.JasperException:
com.snipjib.database.User.getMembers()Ljava/sql/ResultSet;
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:2
54)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

Any clue as to what going on?

Thanks for the help!

Ben
First, please don't top post. Second, the middle of the error message is
often much more useful than the top. Third, from what you said in your
original post, you have a class named "driver" and you're looking for one
named "Driver". That won't work.
 
J

Jason Bell

I don't know what com.snipjib.database is like or how it operates with a
database connection.
I would try and get a connection working without all that first.

try {
Class.forName("com.mysql.jdbc.Driver");
catch(Exception e) {

}

try {
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost/mydb","user","passwd");
PreparedStatement pstmt = connection.prepareStatement("SELECT * FROM
table");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){

}
// close everything you opened
} catch(Exception e) {

}

Jase Bell


<%@ page contentType="text/html; charset=iso-8859-1" language="java"
 
B

ben

Thanks for all the help everybody - I'd already been able to access
the database directly from a jsp page, so I knew that tomcat was
finding the driver correctly. But in desperation I restarted Tomcat,
and suddenly everything started working. I can't figure out why, but
hey - I'm not complaining.

Ben
 
R

Ryan Stewart

ben said:
Thanks for all the help everybody - I'd already been able to access
the database directly from a jsp page, so I knew that tomcat was
finding the driver correctly. But in desperation I restarted Tomcat,
and suddenly everything started working. I can't figure out why, but
hey - I'm not complaining.

Ben

That should always be your first course of action when a webapp suddenly
stops working and gives strange error messages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top