/* 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. <HTML

3. <HEAD><TITLE

4. JSP DB Query 

5. </TITLE></HEAD

6.  

7. <BODY

8. <h2

9. DBQ JSP Version 0.4 

10. </h2

11.  

12. <% 

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

14.  

15.     String fname =   request.getParameter( "firstname" ) ; 

16.  

17.     Connection con = null

18.     ResultSet rs = null

19.     PreparedStatement stm=null

20.     try

21.     con = ds.getConnection(); 

22.     stm = con.prepareStatement 

23.     ("SELECT * FROM FMTEST WHERE FNAME=?" ); 

24.     stm.setString( 1, fname ); 

25.     rs = stm.executeQuery() ; 

26. %> 

27.     <UL

28. <% 

29.     while( rs.next() ) 

30.     { 

31. %> 

32.       <LI> <%= rs.getString("FNAME")%> 

33.          

34.       <%=rs.getString("LNAME")%> 

35.          

36.       <%=rs.getString("PHONE")%> 

37.  

38. <% 

39.     } 

40.     } 

41.     catch ( SQLException sex ) 

42.     { 

43.       out.println( sex ); 

44.     } 

45.     finally 

46.     { 

47.       try 

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

49.         catch (Exception e) {} 

50.       try 

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

52.         catch (Exception e){} 

53.     } 

54. %> 

55.  

56. <%! 

57. DataSource ds; 

58. Exception xcep = null

59.  

60.   public void jspInit( ) 

61.   { 

62.   try

63.   InitialContext jndictx = new InitialContext(); 

64.   ds = (DataSource) jndictx.lookup 

65.       ( "java:/DefaultDS" ); 

66.     } 

67.    catch( Exception ex ) 

68.     { xcep = ex ; } 

69.  } 

70. %> 

71.  

72. </BODY

73. </HTML

74. ==== in test.html  in  dbjsp.war  ===== 

75. <FORM ACTION="DBQ.jsp" METHOD="POST"

76.  

77. Find

78. <INPUT TYPE="TEXT" NAME="firstname" MAXLENGTH="20"

79. <INPUT TYPE="SUBMIT" VALUE="Find"

80.  

81. </FORM

82.