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 :)

hey just a second what is the connection u r creating ?????????is it Sql ,.........
u need 2 specify .

My connection is to a MySQL database.

I use a jdbc:mysql connection with Netbeans. I can access the data base with other classes.

I use a standard way of adding/retrieving throughout but retrieving data causes problems for me.

Post the code of your DBConnect class so we can find out the problem.

In your exception stacktrace this line gets printed "Exception: connection to database was lost" It means something is wrong with passing the connection. While checking whether con is null or not "it found to be null" and exception is thrown.

Check your DBConeect class .

Thanks for the advice ...my DBConnect class is

/*******************************************************
This class is used to open a connection to the mysql
database. The database connection driver is used to
get a connection to the database.
The database connection is established.
The disconnect method closes an open connection to the database.
*******************************************************/
package fitness;
import java.sql.*;
import java.util.*;
public class DBConnect{
String error;
Connection con;
public DBConnect(){}//empty constructor
/*********************************************************
public methods:
void connect() : establishes database connection.
void disconnect() : closes database connection.
Connection getCon() : Returns the connection.
*********************************************************/
public void connect() throws ClassNotFoundException,
                             SQLException,
                             Exception {
 try{
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  con = DriverManager.getConnection
                    ("jdbc:mysql:///register?user=root&password=rootpassword");
                    // ("jdbc:mysql://localhost/register","root", "rootpassword");
  }//end try
  catch (ClassNotFoundException cnfe){
   error = "ClassNotFoundException: Could not locate DB driver";
   throw new ClassNotFoundException(error);
   }//end catch
   catch (NullPointerException ne){
   error = ("NullPointerException: " + ne.toString());
   throw new Exception(error);
   }
   catch (Exception e){
   error = "Exception: An unknown error occured while connecting " +
           "to database.";
   throw new Exception(error);
   }
  }
public ResultSet execSQL(String sql) throws SQLException{ 
                    Statement s = con.createStatement(); 
                    ResultSet r = s.executeQuery(sql); 
                    return (r == null) ? null : r; 
                    } 
 
public void disconnect() throws SQLException{
 try{
  if(con!=null){
  con.close();
  }// end if
  }// end try
  catch(SQLException sqle){
  error = ("SQLException: Unable to close the database connection.");
  throw new SQLException(error);
  }
  }
public Connection getCon(){
return con;
}
}

Ah what i was thinking yeasterday !!

Buddy you are doing this
"double repmax = update.getRepmax(userName);" Till this point of time you didn't passed the connection to "MuscleSchedule"

Shift this line " double repmax = update.getRepmax(userName);" in your program below

conn.connect();
        update.setCon(conn.getCon());
// put it here

Hey buddy Il try that now. Thanks:)

That solved the problem with it not being able to read from the database anyways ....but when I try to call <jsp:getProperty name ="update" property="benchpress" />Im recieving an error: Cannot find a method to read property 'benchpress' in a bean of type 'fitness.MuscleSchedule' :(

I hope I never have to maintain your code...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.