Problem with database connection

Reply

Join Date: Feb 2007
Posts: 8
Reputation: hegRT is an unknown quantity at this point 
Solved Threads: 0
hegRT hegRT is offline Offline
Newbie Poster

Problem with database connection

 
0
  #1
May 7th, 2007
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
  1. /*
  2.  * MuscleSchedule.java
  3.  *
  4.  * Created on 07 May 2007, 18:03
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9. package fitness;
  10. import java.sql.*;
  11. /**
  12.  *
  13.  * @author Hegstatic
  14.  */
  15. public class MuscleSchedule {
  16. /** Creates a new instance of MuscleSchedule */
  17. public Connection con;
  18. public String error;
  19. public double repmax;
  20. public double benchpress;
  21.  
  22. public Connection getCon()
  23. {
  24. return con;
  25. }
  26.  
  27. public void setCon(Connection con){
  28. this.con = con;
  29. }
  30.  
  31.  
  32.  
  33. public double getRepmax(String userName) throws SQLException, Exception{
  34. if(con != null){
  35. try{
  36.  
  37. ResultSet rs;
  38. Statement stmt = con.createStatement();
  39.  
  40. rs = stmt.executeQuery("SELECT Benchpress FROM repmax WHERE Username= '"+userName+"' ");
  41. while (rs.next()) {
  42. repmax = rs.getDouble("Benchpress");
  43.  
  44. }
  45. return repmax;
  46.  
  47. } catch(SQLException sqle){
  48. error = "SQLException: could not search repmax";
  49. throw new Exception(error, sqle);
  50. }
  51.  
  52. catch(Exception e){
  53. error = "An exception occured while searching repmax";
  54. throw new Exception(error);
  55. }
  56. } else {
  57. error = "Exception: connection to database was lost";
  58. throw new Exception(error);}
  59.  
  60. }
  61. public void setRepmax(double repmax)
  62. {
  63. this.repmax = repmax;
  64. }
  65.  
  66. public double getBenchpress(double repmax)
  67. {
  68. benchpress = Math.round(((repmax)/10)*8);
  69. return benchpress;
  70. }
  71. public void setBenchpress(double benchpress)
  72. {
  73. this.benchpress = benchpress;
  74. }
  75. }
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 :
  1. <%@page contentType="text/html"%>
  2. <%@page import="java.sql.*, fitness.*"%>
  3.  
  4. <jsp:useBean id="conn" class="fitness.DBConnect" />
  5. <jsp:useBean id="update" scope="session" class="fitness.MuscleSchedule" />
  6. <jsp:useBean id="currentuser" scope="session" class="fitness.currentuserBean" />
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  9. "<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>">
  10. <html>
  11. <head>
  12. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  13. <title>Muscle Schedule</title>
  14. </head>
  15. <body>
  16. <h1>JSP Page</h1>
  17.  
  18. <!--Here the user calls either the newEmployer or newStudent methhods from the DBQuery, depending on the user type-->
  19. <head><title>Press up update</title></head>
  20. <body>
  21. <%
  22. String userName = currentuser.getName();
  23. double repmax = update.getRepmax(userName);
  24.  
  25.  
  26.  
  27. conn.connect();
  28. update.setCon(conn.getCon());
  29. update.getBenchpress(repmax);
  30.  
  31. conn.disconnect();
  32.  
  33.  
  34. %>
  35. <jsp:forward page="ScheduleDisplay.jsp" />
  36. </body>
  37. </html>
  38. </body>
  39. </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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 18
Reputation: aruna_k_2006 is an unknown quantity at this point 
Solved Threads: 0
aruna_k_2006's Avatar
aruna_k_2006 aruna_k_2006 is offline Offline
Newbie Poster

Re: Problem with database connection

 
0
  #2
May 8th, 2007
hey just a second what is the connection u r creating ?????????is it Sql ,.........
u need 2 specify .
Nikki
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 8
Reputation: hegRT is an unknown quantity at this point 
Solved Threads: 0
hegRT hegRT is offline Offline
Newbie Poster

Re: Problem with database connection

 
0
  #3
May 8th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 46
Reputation: lucky1981_iway is an unknown quantity at this point 
Solved Threads: 3
lucky1981_iway's Avatar
lucky1981_iway lucky1981_iway is offline Offline
Light Poster

Re: Problem with database connection

 
0
  #4
May 8th, 2007
Post the code of your DBConnect class so we can find out the problem.
Last edited by lucky1981_iway; May 8th, 2007 at 9:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 46
Reputation: lucky1981_iway is an unknown quantity at this point 
Solved Threads: 3
lucky1981_iway's Avatar
lucky1981_iway lucky1981_iway is offline Offline
Light Poster

Re: Problem with database connection

 
0
  #5
May 8th, 2007
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 .
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 8
Reputation: hegRT is an unknown quantity at this point 
Solved Threads: 0
hegRT hegRT is offline Offline
Newbie Poster

Re: Problem with database connection

 
0
  #6
May 9th, 2007
Thanks for the advice ...my DBConnect class is
  1. /*******************************************************
  2. This class is used to open a connection to the mysql
  3. database. The database connection driver is used to
  4. get a connection to the database.
  5. The database connection is established.
  6. The disconnect method closes an open connection to the database.
  7. *******************************************************/
  8. package fitness;
  9. import java.sql.*;
  10. import java.util.*;
  11. public class DBConnect{
  12. String error;
  13. Connection con;
  14. public DBConnect(){}//empty constructor
  15. /*********************************************************
  16. public methods:
  17. void connect() : establishes database connection.
  18. void disconnect() : closes database connection.
  19. Connection getCon() : Returns the connection.
  20. *********************************************************/
  21. public void connect() throws ClassNotFoundException,
  22. SQLException,
  23. Exception {
  24. try{
  25. Class.forName("com.mysql.jdbc.Driver").newInstance();
  26. con = DriverManager.getConnection
  27. ("jdbc:mysql:///register?user=root&password=rootpassword");
  28. // ("jdbc:mysql://localhost/register","root", "rootpassword");
  29. }//end try
  30. catch (ClassNotFoundException cnfe){
  31. error = "ClassNotFoundException: Could not locate DB driver";
  32. throw new ClassNotFoundException(error);
  33. }//end catch
  34. catch (NullPointerException ne){
  35. error = ("NullPointerException: " + ne.toString());
  36. throw new Exception(error);
  37. }
  38. catch (Exception e){
  39. error = "Exception: An unknown error occured while connecting " +
  40. "to database.";
  41. throw new Exception(error);
  42. }
  43. }
  44. public ResultSet execSQL(String sql) throws SQLException{
  45. Statement s = con.createStatement();
  46. ResultSet r = s.executeQuery(sql);
  47. return (r == null) ? null : r;
  48. }
  49.  
  50. public void disconnect() throws SQLException{
  51. try{
  52. if(con!=null){
  53. con.close();
  54. }// end if
  55. }// end try
  56. catch(SQLException sqle){
  57. error = ("SQLException: Unable to close the database connection.");
  58. throw new SQLException(error);
  59. }
  60. }
  61. public Connection getCon(){
  62. return con;
  63. }
  64. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 46
Reputation: lucky1981_iway is an unknown quantity at this point 
Solved Threads: 3
lucky1981_iway's Avatar
lucky1981_iway lucky1981_iway is offline Offline
Light Poster

Re: Problem with database connection

 
0
  #7
May 9th, 2007
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
Last edited by lucky1981_iway; May 9th, 2007 at 10:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 8
Reputation: hegRT is an unknown quantity at this point 
Solved Threads: 0
hegRT hegRT is offline Offline
Newbie Poster

Re: Problem with database connection

 
0
  #8
May 9th, 2007
Hey buddy Il try that now. Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 8
Reputation: hegRT is an unknown quantity at this point 
Solved Threads: 0
hegRT hegRT is offline Offline
Newbie Poster

Re: Problem with database connection

 
0
  #9
May 9th, 2007
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'
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Problem with database connection

 
0
  #10
May 10th, 2007
I hope I never have to maintain your code...
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC