/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. <%@page import="java.sql.*,javax.sql.*,javax.naming.*" %> 

2.  

3. <html

4. <head><title>JSP DB update</title></head

5. <body

6. JSP DB update Version 0.5 

7. <% 

8. if ( xcep != null ) throw new Exception( xcep.toString() ) ; 

9.  

10.     String action = request.getParameter ( "act" ) ; 

11.     String fname =   request.getParameter ( "fnm" ) ; 

12.     String lname =   request.getParameter ( "lnm" ) ; 

13.     String phone =   request.getParameter ( "ph" ) ; 

14.  

15.     if ( fname.length()==0 || lname.length()== 0

16.     { 

17.       response.sendRedirect("testupd.html"); 

18.       return

19.      } 

20.  

21.     Connection con = null

22.     if ( action.equalsIgnoreCase("selc") ) 

23.     { 

24.     ResultSet rs ; 

25.     PreparedStatement stm = null

26.     try

27.     con = ds.getConnection(); 

28.     stm = con.prepareStatement 

29.     ("SELECT * FROM FMTEST WHERE FNAME=? AND LNAME=?"); 

30.     stm.setString( 1, fname ); 

31.     stm.setString( 2, lname ); 

32.     rs = stm.executeQuery(); 

33.     rs.next(); 

34.     if ( rs.getRow() != 1

35.     { 

36.     %> 

37.       <br>Unknown being<br

38.       <A HREF=javascript:history.back()>Try again</A

39.       </body></html

40.     <% 

41.       return

42.     } 

43.     %> 

44.     <FORM ACTION=DBupd.jsp METHOD=POST

45.     Change for

46.     <%=fname%> <%=lname%> 

47.     <INPUT TYPE=TEXT NAME=ph VALUE=<%=rs.getString("phone")%> 

48.      MAXLENGTH=10

49.     <INPUT TYPE=SUBMIT VALUE="Change"

50.     <INPUT TYPE=HIDDEN NAME=fnm VALUE= <%=fname%>  > 

51.     <INPUT TYPE=HIDDEN NAME=lnm VALUE= <%=lname%>  > 

52.     <INPUT TYPE=HIDDEN NAME=act VALUE=upd> 

53.     </FORM

54.     <% 

55.     rs.close(); 

56.     } 

57.     catch ( SQLException sex ) 

58.     { 

59.       out.println( sex ); 

60.     } 

61.     finally 

62.     { 

63.       try 

64.       { if (stm != null)   stm.close(); } 

65.         catch (Exception e) {} 

66.       try 

67.       { if (con != null)  con.close(); } 

68.         catch (Exception e) {} 

69.     } 

70. %> 

71.     </body></html

72. <% 

73.     return;    //end action 'selc' 

74.      } 

75.  

76.     if ( !action.equalsIgnoreCase("upd") ) return

77.  

78.     PreparedStatement stm = null

79.     try

80.     con = ds.getConnection(); 

81.     stm = con.prepareStatement 

82.     ("UPDATE FMTEST SET PHONE=? WHERE FNAME=? AND LNAME=?"); 

83.     stm.setString( 1, phone ); 

84.     stm.setString( 2, fname ); 

85.     stm.setString( 3, lname ); 

86.     int rsuk = stm.executeUpdate(); 

87.  

88.     if ( rsuk == 1

89.     { 

90. %> 

91.       <br>Changed

92.       <%=fname%> <%=lname%> 

93.       <%=phone%><BR

94.       <A HREF=testupd.html >Change another one</A

95. <% 

96.     } 

97.     } 

98.     catch ( SQLException sex ) 

99.     { 

100.       out.println( sex ); 

101.     } 

102.     finally 

103.     { 

104.       try 

105.       { if (stm != null)   stm.close(); } 

106.         catch (Exception e) {} 

107.       try 

108.       { if (con != null)  con.close(); } 

109.         catch (Exception e){} 

110.     } 

111. %> 

112.    </body></html

113.  

114. <%! 

115. DataSource ds; 

116. Exception xcep = null

117.  

118.   public void jspInit( ) 

119.   { 

120.    try

121.     InitialContext jndictx = new InitialContext(); 

122.     ds = (DataSource) jndictx.lookup 

123.       ( "java:/DefaultDS" ); 

124.     } 

125.     catch(Exception ex) 

126.     { xcep = ex ; } 

127.  } 

128. %> 

129. ====  testupd.html in dbjsp.war  ====== 

130. <HTML

131. <HEAD><TITLE

132. JSP DB FORM Update 

133. </TITLE></HEAD

134. <BODY

135.  

136. <FORM ACTION="DBupd.jsp" METHOD="POST"

137.  

138. Search for

139. <INPUT TYPE="TEXT" NAME="fnm" MAXLENGTH="20"

140. <INPUT TYPE="TEXT" NAME="lnm" MAXLENGTH="30"

141. <INPUT TYPE="HIDDEN" NAME="act" VALUE="selc"

142. <INPUT TYPE="SUBMIT" VALUE="Send"

143.  

144. </FORM

145.  

146. </BODY></HTML