Hi there Im having a problem with my database connection...other instances of nearly the same code works but I cant understand why this isn't working
My class which retrieves info from the data base is
/*
* MuscleSchedule.java
*
* Created on 07 May 2007, 18:03
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package fitness;
import java.sql.*;
/**
*
* @author Hegstatic
*/
public class MuscleSchedule {
/** Creates a new instance of MuscleSchedule */
public Connection con;
public String error;
public double repmax;
public double benchpress;
public Connection getCon()
{
return con;
}
public void setCon(Connection con){
this.con = con;
}
public double getRepmax(String userName) throws SQLException, Exception{
if(con != null){
try{
ResultSet rs;
Statement stmt = con.createStatement();
rs = stmt.executeQuery("SELECT Benchpress FROM repmax WHERE Username= '"+userName+"' ");
while (rs.next()) {
repmax = rs.getDouble("Benchpress");
}
return repmax;
} catch(SQLException sqle){
error = "SQLException: could not search repmax";
throw new Exception(error, sqle);
}
catch(Exception e){
error = "An exception occured while searching repmax";
throw new Exception(error);
}
} else {
error = "Exception: connection to database was lost";
throw new Exception(error);}
}
public void setRepmax(double repmax)
{
this.repmax = repmax;
}
public double getBenchpress(double repmax)
{
benchpress = Math.round(((repmax)/10)*8);
return benchpress;
}
public void setBenchpress(double benchpress)
{
this.benchpress = benchpress;
}
}
I want this class to basically take a variable in the first method
then do a calculation on that variable in the next method
Then send that calculation to a JSP....
My JSP page with a servlet that calls the methods is :
<%@page contentType="text/html"%>
<%@page import="java.sql.*, fitness.*"%>
<jsp:useBean id="conn" class="fitness.DBConnect" />
<jsp:useBean id="update" scope="session" class="fitness.MuscleSchedule" />
<jsp:useBean id="currentuser" scope="session" class="fitness.currentuserBean" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL]http://www.w3.org/TR/html4/loose.dtd[/URL]">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Muscle Schedule</title>
</head>
<body>
<h1>JSP Page</h1>
<!--Here the user calls either the newEmployer or newStudent methhods from the DBQuery, depending on the user type-->
<head><title>Press up update</title></head>
<body>
<%
String userName = currentuser.getName();
double repmax = update.getRepmax(userName);
conn.connect();
update.setCon(conn.getCon());
update.getBenchpress(repmax);
conn.disconnect();
%>
<jsp:forward page="ScheduleDisplay.jsp" />
</body>
</html>
</body>
</html>
The error I am getting is
org.apache.jasper.JasperException: Exception in JSP: /Muscleschedule.jsp:28
25: <body>
26: <%
27: String userName = currentuser.getName();
28: double repmax = update.getRepmax(userName);
29:
30:
31:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
root cause
javax.servlet.ServletException: Exception: connection to database was lost
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.Muscleschedule_jsp._jspService(Muscleschedule_jsp.java:125)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
root cause
java.lang.Exception: Exception: connection to database was lost
fitness.MuscleSchedule.getRepmax(MuscleSchedule.java:61)
org.apache.jsp.Muscleschedule_jsp._jspService(Muscleschedule_jsp.java:96)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.
Im going mad here with this !! If anybody has an idea as to why Im getting this error I'd really appreciate it....thanks
Heg :)