2 Replies Latest reply: Jan 7, 2012 3:45 PM by 908365 RSS

    Inserting data into MySQL database

    908365
      Hi Everyone!
      Over here, I'm creating a non-members registration page. my homework consists of a table with the following fields, mid,name, pword (namely members ID, name and password respectively) in a MySQL database.

      Right now, I'm stuck with the controller jsp especially the part where the output confirmation goes.
      Please take note that the output confirmation part is from another homework so there might be some confusion.
      it actually compares data from two different tables but i'm working on a solo one currently.

      Can you guys help me with the problem?
      Please feel free to provide me with your suggestions and comments.
      Thanks in advance and sorry I'm clear in my explanation.

      Here's my requestJoin.jsp
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
          <head>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              <title>Fantasy Football</title>
          </head>
          <body>
              <h1>Joining the league</h1>
              
      <form action="requestQuery.jsp" method="post">
          
      Enter your name: <input type="text" name="name" value="${param.name}" />
      <c:if test="${empty param.name && param.submitted}">
          <span style="color:red"> * please fill in field</span>
      </c:if>
      <br />
      
      Enter your password:<br />
      <textarea name="password" rows="6" ><c:if test="${!empty param.password}">${param.password}</c:if></textarea>
      <c:if test="${empty param.password && param.submitted}">
          <span style="color:red"> * please fill in field</span>
      </c:if>
      <br />
      
      <input type="hidden" name="submitted" value="true" />
      <p />
      <input type="submit" value="SUBMIT" />
      </form>
      
          </body>
      </html>
      Then the requestQuery.jsp (controller)
      <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
      
      <c:choose>
      
          <c:when test="${empty param.name  || empty param.password}">
              <%-- empty so go back to form --%>
              <jsp:forward page="requestJoin.jsp" />
          </c:when>
      
          <c:otherwise>
      
              <%-- insert new data into table --%>
              <sql:update>
                  insert into member (mid,name,pword) values (null,?,?)
                  <sql:param value="${dbd}" />
                  <sql:param value="${fn:toLowerCase(param.name)}" />
              </sql:update>
      
              <%-- output confirmation --%>
              <sql:query var="dbResult" scope="request">
                  select * from drequest,degree where drequest.did=degree.did and name = '${fn:toLowerCase(param.name)}'
              </sql:query>
              <jsp:forward page="requestResult.jsp" />
      
          </c:otherwise>
      </c:choose>
      Finally, my requestResult.jsp
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
         "http://www.w3.org/TR/html4/loose.dtd">
      
      <html>
          <head>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              <title>JSP Confirm Page</title>
          </head>
          <body>
              <h1>Confirmation!</h1>
              <%-- display data held in query ran in requestQuery --%>
              Thank you ${dbResult.rows[0].name} for your registration <br />
      
          </body>
      </html>