Results 1 to 10 of 10
  1. #1
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default JSP update what's wrong with the following codes?

    Hi expert,

    I've used the codes below somewhere and it worked. But, it isn't anymore so I'd like to seek help and advise where I'd gone wrong. Thanks.

    My database is ID autoincrement, string Name, String NRIC.

    Java Code:
    <%    Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                Statement stmt = null;
    
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    conn = DriverManager.getConnection("jdbc:mysql://localhost/ADFJA) 
                    stmt = conn.createStatement();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                if (request.getParameter("action") != null) {
                    int selectionId = Integer.parseInt(request.getParameter("ID"));
                    String NRIC = request.getParameter("txtNRIC");
                    String Name = request.getParameter("txtFullName");
    String sql = "UPDATE members SET strNRICNO='" + NRIC
                            + " ', strName=' " + Name + "'where ID= " + selectionId;
                    //out.println(sql);
                    stmt.executeUpdate(sql);
    
    %>
    <html>
        Member Id = <%= request.getParameter("ID")%> Successfully Updated ! <br><br>
    
    </html>
    <%} else {%>
    <%
                    int selectionId = Integer.parseInt(request.getParameter("ID")); // this is the error line.
                    String MemberNo = null;
    
    String strNRIC = null;
                    String strName = null;
    rs = stmt.executeQuery("select * from members where ID = "   + selectionId + "");
                    if (rs.next()) {
                        strNRIC = rs.getString("strNRICNO");
                        strName = rs.getString("strName");
      } else {
                        strNRIC = "Unkown.";
                        strName = "Unkown.";
      </head>
        <body><center>
                <form action="Update.jsp" method ="post" name="update">
            </center>
            <input type="hidden" value="list" name="action">
            <input type="hidden" value=<%= request.getParameter("ID")%> name="ID"">
            <div id="header">Welcome to Admin</div>
                          <input type = "hidden" value="list" name="action">
                <table>
                    <tr><td>Member's ID</td>
                        <td><%= selectionId%></td>
                    <tr>
                        <td>Full Name</td>
                        <td><input type="text" id="txtFullName" name="txtFullName" value="<%=strName%>"></td>
                    </tr>
                    <tr>
         <input type="submit" id="btnSubmit" name="Update Member" value="Update Member<%= request.getParameter("ID")%>">
                </td></tr>
        </table>
        <a href ="logout.jsp">Log Out</a>
    </div>
    </form>
    </body>
    </html>
    <%}%>
    Thanks.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    What does "it doesn't work anymore" mean?

    We're not mind readers...

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    I will say that that code should not be in a JSP page.
    That's poor practice.

  4. #4
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default

    The code doesn't work anymore because when I try to run the program, this time round it gave me an error code at

    int selectionId = Integer.parseInt(request.getParameter("ID"));

    Which puzzled me. So, I hope someone can give me some clue why is it so.

    Thanks.

  5. #5
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default

    The code doesn't work anymore because when I try to run the program, this time round it gave me an error code at

    int selectionId = Integer.parseInt(request.getParameter("ID"));

    Which puzzled me. So, I hope someone can give me some clue why is it so.

    Thanks.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    And?
    You don't think it might be an idea to actually tell us the full error text that you are getting, or do we have to guess?

  7. #7
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    And?
    You don't think it might be an idea to actually tell us the full error text that you are getting, or do we have to guess?
    The full error message:-

    org.apache.jasper.JasperException: An exception occurred processing JSP page /update.jsp at line 58

    55: <%} else {%>
    56: <%
    57:
    58: int selectionId = Integer.parseInt(request.getParameter("ID"));
    59: String MemberNo = null;
    60: String strNRIC = null;
    61: String strFullName = null;

    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:505)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:416)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:390)

    root cause

    java.lang.NumberFormatException: null
    java.lang.Integer.parseInt(Integer.java:417)
    java.lang.Integer.parseInt(Integer.java:499)
    org.apache.jsp.update_jsp._jspService(update_jsp.j ava:116)
    org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:390)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    root cause

    java.lang.NumberFormatException: null

    Which is saying that getParemeter("ID") is returning null.

  9. #9
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    root cause

    java.lang.NumberFormatException: null

    Which is saying that getParemeter("ID") is returning null.
    OK. But, I have parsed the parameter into integer and ID is my hidden value in the form, so what went wrong?

    Kindly enlighten me because I'm basically very week in programming. Many thanks.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    I have no idea, apart from the fact that there is no "ID" parameter being sent to the server by whatever HTML form was submitted.
    At least not one with anything in it.

Similar Threads

  1. I need some codes
    By johnmergene in forum New To Java
    Replies: 11
    Last Post: 01-25-2011, 03:42 AM
  2. What do the following codes do?
    By javaguy2 in forum New To Java
    Replies: 2
    Last Post: 01-23-2011, 10:23 PM
  3. two short codes
    By Libertyman in forum New To Java
    Replies: 7
    Last Post: 06-21-2010, 03:22 PM
  4. What's wrong with my codes?
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 09-01-2008, 03:57 AM
  5. Posting codes and help
    By Java_Man in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •