?
Solved

View Session Variables in form and then edit or accept to Update DB

Posted on 2005-03-30
13
Medium Priority
?
210 Views
Last Modified: 2010-04-01
Hi there,
have a page "viewdetails.jsp" where I select all data in the DB table "member" and another page want to change/update the details "change.jsp", where all data has to be entered again.  They work but I want to merge the two together.
So when I view details I have form fields that I can change the text and then submit the details to update DB.

0
Comment
Question by:acslater
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13661032
Just output the results from the resultset into a form and then post the form to a servlet that performs an update.
0
 

Author Comment

by:acslater
ID: 13661046
Not using servlets... just JSP and MySQL
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13661100
displaypage.jsp
--------------------
<%
//Connection stuff
//generate resultset rs
%>      <----- here

<form action="updatePage.jsp">
<%while (rs.next ()){%>
<input type="hidden" name="id" value='<%=rs.getString("Id")%>'/>
<input type="text" name="username" value='<%=rs.getString("UserName")%>'/>
<input type="text" name="firstname" value='<%=rs.getString("FirstName")%>'/>
<%}%>
</form>

updatePage.jsp
----------
<%
String id= request.getParameter("id") ;
String username = request.getParameter("username") ;
String firstname = request.getParameter("firstname") ;

String sql = "UPDATE mytable SET firstname = '"+ firstname+"' , username = '"+ username +"'  WHERE id = " + id ;
//do the update
%>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 29

Expert Comment

by:bloodredsun
ID: 13661130
there's a jsp only version. Servlets and JSPs are completely interchangable (JSPs are actually converted to JSPs in the background, you just never see it). I know you know how to do the database stuff as I helped with your previous question in the Java forum ;-)

What's this for? Development project or just some stuff for yourself?
0
 

Author Comment

by:acslater
ID: 13661342
Will try this.
It for a project for college work.
0
 

Author Comment

by:acslater
ID: 13661709
Ye... just figured out that you are the same guy as before... thanks for all your help.

Bit confused now... my table is "member" and the working code (extract) for existing pages are:
https://www.bcdtesting.com/change.jsp 
https://www.bcdtesting.com/viewdetails.jsp

viewdetails.jsp
----------------
<%
Statement      Statement = null;
ResultSet      resultset = null;
ResultSetMetaData rsmd = null;
Connection      connection = null;


Class.forName("org.gjt.mm.mysql.Driver").newInstance();
connection = DriverManager.getConnection("???????"
 
    Statement  = connection.createStatement();
    ResultSet rs = Statement.executeQuery("select * from Member where '" + session.getValue("UserName") + " '  = UserName ");

%>

                    <table width="81%" height="252"  border="0" align="center" cellpadding="0" cellspacing="0">
<%while (rs.next ()){%>
                      <tr>
                        <th width="40%" scope="col"><div align="left">UserName:</div></th>
                        <th width="60%" scope="col"><div align="left"><%=rs.getString("UserName")%></div></th></tr>
                      <tr>
                        <td><div align="left"></div></td>
                        <td><div align="left"></div></td></tr>
                      <tr>
                        <td><div align="left">First Name: </div></td>
                        <td><div align="left"><%=rs.getString("FirstName")%></div></td></tr>
                      <tr>
                        <td><div align="left">Last Name:</div></td>
                        <td><div align="left"><%=rs.getString("LastName")%></div></td></tr>
                      <tr>
                        <td><div align="left">Address :  </div></td>
                        <td><div align="left"><%=rs.getString("Address1")%></div></td></tr>
                      <tr>
                        <td><div align="left">Address :  </div></td>
                        <td><div align="left"><%=rs.getString("Address2")%></div></td></tr>
                      <tr>
                        <td><div align="left">Town :  </div></td>
                        <td><div align="left"><%=rs.getString("Town")%></div></td></tr>
                      <tr>
                        <td><div align="left">County :  </div></td>
                        <td><div align="left"><%=rs.getString("County")%></div></td></tr>
                      <tr>
                        <td><div align="left">Area Code:  </div></td>
                        <td><div align="left"><%=rs.getString("AreaCode")%></div></td></tr>
                      <tr>
                        <td><div align="left">Phone Number :  </div></td>
                        <td><div align="left"><%=rs.getString("Phone")%></div></td></tr>
                      <tr>
                        <td><div align="left">Email Address :  </div></td>
                        <td><div align="left"><%=rs.getString("EmailAddress")%></div></td></tr>
                      <tr>
                        <td><div align="left">Your Gender :  </div></td>
                        <td><div align="left"><%=rs.getString("Gender")%></div></td></tr>
                      <tr>
                        <td><div align="left">Date of birth: </div></td>
                        <td><div align="left"><%=rs.getString("Day")%> / <%=rs.getString("Month")%> / <%=rs.getString("Year")%> </div></td></tr>
                   <%}%>
 </table>

<%
    connection.close();
%>



change.jsp
__________
<--connection here-->

ResultSet rs = Statement.executeQuery("select * from Member where '" + session.getValue("UserName") + " '  = UserName ");
%>
                    <table width="81%" border="0" align="center" cellpadding="0" cellspacing="0">
                      <tr>
                        <td colspan="3" valign=bottom class="subtitle"><div align="center">
                            <p><strong>Please Review Your Details</strong></p>
                            <p>&nbsp;</p>
                        </div></td>
                      </tr>
                      <form name="frm1" id="frm1" method="post" action="changed2.jsp" onSubmit="return validateForm(frm1)">
                        <tr align="left" valign="top">
                          <td valign="center" height="35" colspan="4">
                            <p>
                            <table width="97%" height="345" border="0" align="center" cellpadding="3" cellspacing="0">
                              <tr>
                                <td align=right bgcolor="#ffffff" valign="bottom" width="31%" class="subtitle">
                                  <div align="left"><b> Username:</b></div></td>
                                <td align=left valign=bottom width="45%" >
<% out.print(  session.getValue("UserName")+ "<br>");%>&nbsp;
                                </td>
                                <td width="24%" colspan="4" rowspan="2" align=right valign=top>&nbsp; </td>
                              </tr>
                              <tr>
                                <td height="4" colspan="2" align="right" valign="bottom" bgcolor="#ffffff" class="subtitle"><div align="left"></div>                                  <div align="left"></div></td>
                               
                              </tr>
                              <tr valign="bottom">
                                <td align="right" bgcolor="#ffffff" valign="bottom" class="subtitle">
                                  <div align="left"><b>First Name:</b></div></td>
                                <td align="left" valign="bottom" colspan=2>
                                  <input name="FirstName" size="30" maxlength="30" tabindex="4" value="">
                                </td>
                              </tr>
                              <tr>
                                <td align="right" valign="bottom" bgcolor="#ffffff" class="subtitle"><div align="left"><b>Last Name:</b></div></td>
                                <td colspan=3 align="left" valign="bottom">
                                  <input name="LastName" size="30" maxlength="30" tabindex="5" value="">
                                </td>
                              </tr>
                              <tr>
                                <td align="right" valign="bottom" bgcolor="#ffffff" class="subtitle"><div align="left"><b>Address:</b></div></td>
                                <td colspan=3 align="left" valign="bottom">
                                  <input name="Address1" size="35" maxlength="30" tabindex="8" value="">
                                </td>
                              </tr>
                              <tr>
                                <td align="right" valign="bottom" bgcolor="#ffffff"><div align="left"></div></td>
                                <td colspan=3 align="left" valign="bottom">
                                  <input name="Address2" size="35" maxlength="30" tabindex="9" value="">
                                </td>
                              </tr>
                              <tr>
                                <td align="right" valign="bottom" bgcolor="#ffffff" class="subtitle"><div align="left"><b>Town:</b></div></td>
                                <td colspan=3 align="left" valign="bottom">
                                  <input name="Town" size="35" maxlength="25" tabindex="10" value="">
                                </td>
                              </tr>
                              <tr>
                                <td align="right" valign="bottom" bgcolor="#ffffff" class="subtitle"><div align="left"><b>County:</b></div></td>
                                <td colspan=3 align="left" valign="bottom">
                                  <select name="County">
                                    <OPTION value="County" selected>County</OPTION>
                                    <option value="Carlow">Carlow</option>
                                    <option value="Cavan">Cavan</option>
                                    <option value="Donegal">Donegal</option>
                                    <option value="Dublin">Dublin</option>
                                    <option value="Galway">Galway</option>
                                    <option value="Kerry">Kerry</option>
                                    <option value="Monaghan">Monaghan</option>
                                    <option value="Offaly">Offaly</option>
                                    <option value="Waterford">Waterford</option>
                                    <option value="Westmeath">Westmeath</option>
                                    <option value="Wexford">Wexford</option>
                                    <option value="Wicklow">Wicklow</option>
                                    <option>None of the Above</option>
                                  </select>
                                </td>
                              </tr>
                              <tr>
                                <td height="36" align="right" valign="bottom" nowrap bgcolor="#ffffff" class="subtitle">
                                  <div align="left"><b>Home Phone:</b></div></td>
                                <td colspan=3 align="left" valign="bottom"> (
                                    <input name="AreaCode" id="AreaCode" tabindex="20"onKeyUp="countMeHomePhone1(this.value);" value="" size="4" maxlength="4">
              )
              <input name="Phone" id="Phone" tabindex="21"onKeyUp="countMeHomePhone2(this.value);" value="" size="15" maxlength="8">
                                </td>
                              </tr>
                              <tr>
                                <td height="37" align="right" valign="bottom" bgcolor="#ffffff" class="subtitle">
                                  <div align="left"><b>Email Address:</b></div></td>
                                <td colspan=3 align="left" valign="bottom">
                                  <input name="EmailAddress" size="35" maxlength="40" tabindex="27" value="">
                                </td>
                              </tr>
                              <tr>
                                <td height="25" align="right" valign="bottom" bgcolor="#ffffff" class="subtitle">
                                  <div align="left"><b>Gender:</b></div></td>
                                <td colspan=3 align="left" valign="bottom"><font face="Arial, Helvetica, san-serif" size="-1">
                                  <input type="radio" name="Gender" tabindex="32" value="Male"checked>
              Male
              <input type="radio" name="Gender" tabindex="33" value="Female">
              Female</font></td>
                              </tr>
                              <tr>
                                <TD height="40" align=right vAlign=bottom bgColor=#ffffff class=subtitle>
                                  <DIV align=left>
                                    <p><B>Date of Birth:</B></p>
                                    </DIV></TD>
                                <TD vAlign=bottom align=left colSpan=3>
                                  <TABLE width="78%" height="28" border=0 cellPadding=1 cellSpacing=1>
                                    <TBODY>
                                      <TR>
                                        <TD height="20"><SELECT name=Day>
                                            <OPTION value=Day selected>Day</OPTION>
                                            <OPTION value=01>01</OPTION>
                                            <OPTION value=02>02</OPTION>
                                            <OPTION value=03>03</OPTION>
                                            <OPTION value=04>04</OPTION>
                                            <OPTION value=05>05</OPTION>
                                            <OPTION value=06>06</OPTION>
                                            <OPTION value=07>07</OPTION>
                                            <OPTION value=08>08</OPTION>
                                            <OPTION value=09>09</OPTION>
                                            <OPTION value=10>10</OPTION>
                                            <OPTION value=11>11</OPTION>
                                            <OPTION value=12>12</OPTION>
                                            <OPTION value=13>13</OPTION>
                                            <OPTION value=14>14</OPTION>
                                            <OPTION value=15>15</OPTION>
                                            <OPTION value=16>16</OPTION>
                                            <OPTION value=17>17</OPTION>
                                            <OPTION value=18>18</OPTION>
                                            <OPTION value=19>19</OPTION>
                                            <OPTION value=20>20</OPTION>
                                            <OPTION value=21>21</OPTION>
                                            <OPTION value=22>22</OPTION>
                                            <OPTION value=23>23</OPTION>
                                            <OPTION value=24>24</OPTION>
                                            <OPTION value=25>25</OPTION>
                                            <OPTION value=26>26</OPTION>
                                            <OPTION value=27>27</OPTION>
                                            <OPTION value=28>28</OPTION>
                                            <OPTION value=29>29</OPTION>
                                            <OPTION value=30>30</OPTION>
                                            <OPTION value=31>31</OPTION>
                                          </SELECT>
&nbsp;
                      <SELECT name=Month>
                        <OPTION value=Month selected>Month</OPTION>
                        <OPTION value=01>01</OPTION>
                        <OPTION value=02>02</OPTION>
                        <OPTION value=03>03</OPTION>
                        <OPTION value=04>04</OPTION>
                        <OPTION value=05>05</OPTION>
                        <OPTION value=06>06</OPTION>
                        <OPTION value=07>07</OPTION>
                        <OPTION value=08>08</OPTION>
                        <OPTION value=09>09</OPTION>
                        <OPTION value=10>10</OPTION>
                        <OPTION value=11>11</OPTION>
                        <OPTION value=12>12</OPTION>
                      </SELECT>
                      <SELECT name=Year>
                        <OPTION value=Year selected>Year</OPTION>
                        <OPTION value=1999>1999</OPTION>
                        <OPTION value=1998>1998</OPTION>
                        <OPTION value=1997>1997</OPTION>
                        <OPTION value=1996>1996</OPTION>
                        <OPTION value=1995>1995</OPTION>
                        <OPTION value=1994>1994</OPTION>
                        <OPTION value=1993>1993</OPTION>
                        <OPTION value=1992>1992</OPTION>
                        <OPTION value=1991>1991</OPTION>
                        <OPTION value=1990>1990</OPTION>
                        <OPTION value=1989>1989</OPTION>
                        <OPTION value=1988>1988</OPTION>
                        <OPTION value=1987>1987</OPTION>
                        <OPTION value=1986>1986</OPTION>
                        <OPTION value=1985>1985</OPTION>
                        <OPTION value=1984>1984</OPTION>
                        <OPTION value=1983>1983</OPTION>
                        <OPTION value=1982>1982</OPTION>
                        <OPTION value=1981>1981</OPTION>
                        <OPTION value=1980>1980</OPTION>
                           </SELECT>
                                        </TD>
                                      </tr>
                                            </table></td>
                              </tr>
                            </table>
                            <table width="95%" height="77"  border="0" align="center" cellpadding="0" cellspacing="0">
                              <tr>
                                <th colspan="2" scope="row"><div align="center">
                                    <p>&nbsp;</p>
                                    <p><u>Memorable Data - In case of lost Password (optional)</u></p>
                                    <p>&nbsp; </p>
                                </div></th>
                              </tr>
                              <tr>
                                <th width="42%" scope="row">
                                  <div align="left"><strong>Memorable Data Type: </strong> </div></th>
                                <td width="58%">
                                  <SELECT name=MemDataType>
                                    <OPTION value=Please Select ... selected>Please Select ... </OPTION>
                                    <OPTION
                    value=Mother's Maiden Name>Mother's Maiden Name</OPTION>
                                    <OPTION value=Favourite Colour>Favourite Colour</OPTION>
                                    <OPTION
                    value=Memorable Place>Memorable Place</OPTION>
                                    <OPTION value=Memorable Date>Memorable Date</OPTION>
                                  </SELECT>
&nbsp;</td>
                              </tr>
                              <tr>
                                <th scope="row">
                                  <div align="left"><strong>Memorable Data: </strong> </div></th>
                                <td><input name="MemData" type="text" size="23" maxlength="60"></td>
                              </tr>
                            </table>
                            <br>
                                  <tr>
                                              <td colspan="3" align="right" valign="center"></td>
                                              <td width="1%" align="left" valign="center"> </td>
                                  </tr>
                                            <tr>
                                              <td width="3%" height="24"></td>
                                              <td width="38%"></td>
                                              <td width="58%"><input type="submit" name="Submit" value="Update Details"></td>
                                              <td>&nbsp; </td>
                                            </tr>
                      </form>
                    </table>


Do need to create the pages you mentioned am about to start at it now or would I be better editing one of these pages?

0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13661997
Edit change,jsp so that it pre-populates the form with the users details. The format is pretty much the same as the changes you made to the details page, the only difference is that you are outputting the values into a form field rather that into normal html

 e.g.

      <tr valign="bottom">
      <td align="right" bgcolor="#ffffff" valign="bottom" class="subtitle">
        <div align="left"><b>First Name:</b></div></td>
      <td align="left" valign="bottom" colspan=2>
        <input name="FirstName" size="30" maxlength="30" tabindex="4" value="<%=rs.getString("firstname")%>">
      </td>
      </tr>
0
 

Author Comment

by:acslater
ID: 13662220
that sounds like what I want to do... but
... tabindex="4" value="<%=rs.getString("firstname")%>">
tried and won't work...

lost most of the page but get no data up either... tried changing the rs to rs1, but no joy.



0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13662753
Don't forget to call rs.next() in the same way as you did in the first page...

change.jsp
__________
<--connection here-->

ResultSet rs = Statement.executeQuery("select * from Member where '" + session.getValue("UserName") + " '  = UserName ");
while (rs.next ()){
%>
//your table
     <tr valign="bottom">
     <td align="right" bgcolor="#ffffff" valign="bottom" class="subtitle">
       <div align="left"><b>First Name:</b></div></td>
     <td align="left" valign="bottom" colspan=2>
       <input name="FirstName" size="30" maxlength="30" tabindex="4" value="<%=rs.getString("firstname")%>">
     </td>
     </tr>
//rest of your table


<%}%>
0
 

Author Comment

by:acslater
ID: 13662759
In change.jsp:
Brought it back to exactly as had it and just changed the FirstName to:
      <div align="left"><b>First Name:</b></div></td>
      <td align="left" valign="bottom" colspan=2>
      <input name="FirstName" size="30" maxlength="30" tabindex="4" value="<%=rs.getString ("firstname")%>">

This is the error that I am getting:

Generated servlet error:
    [javac] Compiling 1 source file

/home/combcdte/tomcat/work/change_jsp.java:161: rs is already defined in _jspService(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
ResultSet rs = Statement.executeQuery("select * from Member where '" + session.getValue("UserName") + " '  = UserName ");
          ^
Note: /home/combcdte/tomcat/work/change_jsp.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
1 error

the code is:  ResultSet rs = Statement.executeQuery("select * from Member where '" + session.getValue("UserName") + " '  = UserName ");

I want to use a separte page i.e. "changed.jsp" to insert the data, which is the one that worked for the original version of  "change.jsp" (which I have backed up).
0
 
LVL 29

Accepted Solution

by:
bloodredsun earned 1200 total points
ID: 13663032
You need to use a different name for the resultset as it appears that you already have one earlier in your jsp

>>I want to use a separte page i.e. "changed.jsp" to insert the data, which is the one that worked for the original version of  "change.jsp" (which I have backed up).

Yep, good idea
0
 

Author Comment

by:acslater
ID: 13663152
This is the error im now geting:

An error occurred at line: -1 in the jsp file: null

Generated servlet error:
    [javac] Compiling 1 source file

/home/combcdte/tomcat/work/change_jsp.java:840: 'catch' without 'try'
    } catch (Throwable t) {
      ^
/home/combcdte/tomcat/work/change_jsp.java:31: 'try' without 'catch' or 'finally'
    try {
    ^
/home/combcdte/tomcat/work/change_jsp.java:850: '}' expected
^
3 errors
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13663491
Same probs as before, you are misplacing the curly braces...

Check they are where they are supposed to be
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Any files you do not have backed up in at least two [other] places are files you do not care about."
Ready to get certified? Check out some courses that help you prepare for third-party exams.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question