/* 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. import java.sql.*; 

2. import javax.sql.*; 

3. import javax.naming.*; 

4. import java.io.*; 

5. import javax.servlet.*; 

6. import javax.servlet.http.*; 

7.  

8. public class DBSupd extends HttpServlet 

9.

10.   DataSource ds; 

11.  

12.   public void doPost( 

13.   HttpServletRequest req, HttpServletResponse response ) 

14.       throws ServletException, IOException 

15.   { 

16.     response.setContentType("text/html"); 

17.     response.setHeader("Pragma", "no-cache"); 

18.     response.setHeader("Cache-Control", "no-cache"); 

19.     response.setDateHeader("Expires", 0); 

20.     PrintWriter out = response.getWriter(); 

21.     out.println("<html>"); 

22.     out.println("<head><title>DB servlet result page</title></head>"); 

23.     out.println("<body>"); 

24.     out.println("DBSupd Version 1.2"); 

25.      

26.     String action = req.getParameter ( "act" ) ; 

27.     String fname =   req.getParameter ( "fnm" ) ; 

28.     String lname =   req.getParameter ( "lnm" ) ; 

29.     String phone =   req.getParameter ( "ph" ) ; 

30.      

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

32.     { 

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

34.       out.println("</body></html>"); 

35.       out.flush(); 

36.       out.close(); 

37.       return

38.     } 

39.     Connection con = null

40.     try

41.     con = ds.getConnection(); 

42.     } catch(Exception ex) 

43.     {throw new ServletException(ex); } 

44.  

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

46.     { 

47.     ResultSet rs ; 

48.     PreparedStatement stm = null

49.     try

50.     stm = con.prepareStatement 

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

52.     stm.setString( 1, fname ); 

53.     stm.setString( 2, lname ); 

54.     rs = stm.executeQuery(); 

55.     rs.next(); 

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

57.     { 

58.       out.println("<br>Unknown being<br>"); 

59.       out.println("<A HREF=javascript:history.back()>Try again</A>"); 

60. //      out.println("<A HREF=testupd.html>Try again</A>"); 

61.       out.println("</body></html>"); 

62.       out.flush(); 

63.       out.close(); 

64.       return

65.     } 

66.     out.println("<FORM ACTION=dbsupd METHOD=POST>" ); 

67.     out.println("Change for :"); 

68.     out.println( fname + " " +  lname ); 

69.     out.println("<INPUT TYPE=TEXT NAME=ph "

70.     "VALUE=" + rs.getString("phone") +  " MAXLENGTH=10>" ); 

71.     out.println("<INPUT TYPE=SUBMIT VALUE=\"Change\">" ); 

72.     out.println("<INPUT TYPE=HIDDEN NAME=fnm VALUE=" 

73.     + fname + ">" ); 

74.     out.println("<INPUT TYPE=HIDDEN NAME=lnm VALUE=" 

75.     + lname + ">" ); 

76.     out.println("<INPUT TYPE=HIDDEN NAME=act VALUE=upd" ); 

77.      out.println("</FORM>"); 

78.     rs.close(); 

79.     } 

80.     catch ( SQLException sex ) 

81.     { 

82.       out.println( sex ); 

83.     } 

84.     finally 

85.     { 

86.       try 

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

88.         catch (Exception e) {} 

89.       try 

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

91.         catch (Exception e){} 

92.     } 

93.     out.println("</body></html>"); 

94.     out.flush(); 

95.     out.close(); 

96.     return;    //end action 'selc' 

97.      } 

98.  

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

100.  

101.     PreparedStatement stm = null

102.     try

103.     stm = con.prepareStatement 

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

105.     stm.setString( 1, phone ); 

106.     stm.setString( 2, fname ); 

107.     stm.setString( 3, lname ); 

108.     int rsuk = stm.executeUpdate(); 

109.  

110.     if ( rsuk == 1

111.     { 

112.       out.println("<br>Changed :"); 

113.       out.println( fname + " " +  lname ); 

114.       out.println( " " +  phone + "<BR>" ); 

115.       out.println("<A HREF=testupd.html >Change another one</A>"); 

116.     } 

117.     } 

118.     catch ( SQLException sex ) 

119.     { 

120.       out.println( sex ); 

121.     } 

122.     finally 

123.     { 

124.       try 

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

126.         catch (Exception e) {} 

127.       try 

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

129.         catch (Exception e){} 

130.     } 

131.     out.println("</body></html>"); 

132.     out.flush(); 

133.     out.close(); 

134.    } 

135.  

136.   public void init(ServletConfig cfg) 

137.    throws ServletException 

138.   { 

139.     super.init(cfg); 

140.     try

141.       InitialContext jndictx = new InitialContext(); 

142.       ds = (DataSource) jndictx.lookup 

143.           ( "java:/DefaultDS" ); 

144.     } 

145.     catch( Exception ex ) 

146.     { throw new ServletException(ex);  } 

147.   } 

148.

149. /* 

150. ====  testupd.html in sdb.war  ==== 

151. <HTML

152. <HEAD><TITLE

153. Servlets DB FORM Update DB 

154. </TITLE></HEAD

155. <BODY

156.  

157. <FORM ACTION="dbsupd" METHOD="POST"

158. Search for

159.  

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

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

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

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

164.  

165. </FORM

166. </BODY></HTML

167. ===  in web.xml  ==== 

168.     <url-pattern>/dbsupd</url-pattern> 

169. ================= 

170.  jdbc/someds 

171. */