| | |
Problem with database connection
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Feb 2007
Posts: 8
Reputation:
Solved Threads: 0
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
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 :
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
My class which retrieves info from the data base is
JSP Syntax (Toggle Plain Text)
/* * 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; } }
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 :
JSP Syntax (Toggle Plain Text)
<%@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" "<a rel="nofollow" class="t" href="http://www.w3.org/TR/html4/loose.dtd" target="_blank">http://www.w3.org/TR/html4/loose.dtd</a>"> <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>
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
•
•
Join Date: Feb 2007
Posts: 8
Reputation:
Solved Threads: 0
Thanks for the advice ...my DBConnect class is
JSP Syntax (Toggle Plain Text)
/******************************************************* 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
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 Last edited by lucky1981_iway; May 9th, 2007 at 10:08 pm.
![]() |
Similar Threads
- database connection(select ,insert query) within javascript function (JSP)
- Visual C++, Database connection, please help (C++)
- ASP file with database connection problem (ASP)
- script problem adding to database (PHP)
- php error HELP!!! (PHP)
Other Threads in the JSP Forum
- Previous Thread: Combining Static & Dynamic content
- Next Thread: JSP shopping cart
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






