Example : How to hang data into combo/drop down box of html from database [JSP Combo                       Box Hang Code].
<%-- 
    Document   : Combo box Hang Code
    Created on : 16 May, 2019, 5:09:41 PM
    Author     : Codershelpline
--%>

//   JSP & Oracle 10g Connectivity Code
<%@page contentType="text/html; charset=utf-8" language="java" import="java.sql.*"import=
       "java.util.*" errorPage=""%>
<%
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs;
    Statement st=null;
	
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","oracleusername",
         "oraclepassword");
    out.println("Database connected");
%>

<!DOCTYPE html>
<html>    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Combo Hang Code</title>
    </head>

    <body>    
        <form><center>
            <fieldset style="width: 60%; background-color: aliceblue; border-radius: 8px; border- 
                     width: 4px; border-color: cadetblue">                
                <table>
                   <h1>User Registration Page</h1>
                   <hr style="width: 90%">
                
                    <tr>                       
                        <td>
                           <select name="UrCmbEmailSearch1">
						   
                               // Code to Hang Oracle 10g Database Data into Combo Box
                               <option>Select One</option>
                                 <%
                                   pst=conn.prepareStatement("select UrTxtEmail5 from TABLENAME order 
                                         by UrTxtEmail5");
                                    rs=pst.executeQuery();
                                    while(rs.next())
                                    {
                                      String email6=rs.getString("UrTxtEmail5");
                                  %>
                                      <option value="<%=email6%>"><%=email6%></option>
                                  <% 
                                     }
                                   %>    
                             </select> 
							 
                             <input type="submit" name="sub" id="subSearch2" value="Search"> 
                          </td>
                      </tr>                    
                    <tr>
                        <td></td>
                        <td></td>
                        <td>
                            <input type="submit" name="sub" id="subSubmit2" value="Submit">
                            <input type="reset" name="sub" id="subReset2" value="Reset">                                        
                        </td>
                    </tr>
                </table>
             </fieldset>
          </center>
      </form>
    </body>
</html>
Example : How to select combo box (static) data automatically by comparing it dynamically                       from database [Combo Box Auto Select].
<%-- 
    Document   : Combo Data Auto Select Code
    Created on : 14 May, 2019, 2:15:53 PM
    Author     : Codershelpline
--%>

//   JSP & Oracle 10g Connectivity Code
<%@page contentType="text/html; charset=utf-8" language="java" import="java.sql.*"import=
       "java.util.*" errorPage=""%>
<%
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs;
    Statement st=null;
	
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","putusernamehere",
         "putpasswordhere");
    out.println("Database connected");

//   Code for Transfer Html data into JSP variables

    String UrTxtEmail3=request.getParameter("UrTxtEmail1").trim();
    String UrCmbEmailSearch3=request.getParameter("UrCmbEmailSearch1");
        
    String sub2=request.getParameter("sub");
	
//  JSP Variable declaration Code   
    String UrTxtEmail4="";    
    String UrCmbState4="";   

    try
    {
        if(sub2!=null)
        {           

//   Code to Search Unique Record from Oracle 10g Database and Fill into HTML box			
            if (sub2.equals("Search"))
	    {
		pst=conn.prepareStatement("select * from TABLENAME where 
                     UrTxtEmail5='"+UrCmbEmailSearch3+"'");
		rs=pst.executeQuery();
				
		while(rs.next())
		{		
		UrTxtEmail4=rs.getString("UrTxtEmail5");
		UrCmbState4=rs.getString("UrCmbState5");					
		}
	    }            
        }
    }
    catch(Exception e)
    {
        out.println(e);        
    }
%>
<!DOCTYPE html>
<html>    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>All Integrated Code</title>        
        <script>
		
//    Code for Auto Combo Selection on Matching Oracle 10g Database Data
            function comboautoselect()
            {
                var x="<%=UrCmbState4%>"    //   Code to Transfer JSP Data into Java Script           
                document.getElementById("UrCmbState2").value=x;  //Code to Assign Java Script Value 
                                                                   into HTML Box
            }
        </script>
    </head>

    <body onload="comboautoselect()">    // Calling of Java Script Function on Page Load
        <form><center>
            <fieldset style="width: 60%; background-color: aliceblue; border-radius: 8px; border- 
                     width: 4px; border-color: cadetblue">                
                <table>
                   <h1>User Registration Page</h1>
                   <hr style="width: 90%">
                
                    <tr>                        
                        <td>
                           <select name="UrCmbEmailSearch1">
						   
                               // Code to Hang Oracle 10g Database Data into Combo Box
                               <option>Select One</option>
                                 <%
                                    st=conn.prepareStatement("select UrTxtEmail5 from TABLENAME order 
                                         by UrTxtEmail5");
                                    rs=st.executeQuery();
                                    while(rs.next())
                                    {
                                      String email6=rs.getString("UrTxtEmail5");
                                  %>
                                      <option value="<%=email6%>"><%=email6%></option>
                                  <% 
                                     }
                                   %>    
                             </select> 
							 
                             <input type="submit" name="sub" id="subSearch2" value="Search"> 
                          </td>
                      </tr>                      

                    <tr>
                        <td>E-mail</td>
                        <td>:</td>
                        <td>
                            <input type="Email" name="UrTxtEmail1" id="UrTxtEmail2" 
                                 placeholder="[email protected]" value="<%=UrTxtEmail4%>">
                        </td>
                    </tr> 

                    <tr>
                        <td>State</td>
                        <td>:</td>
                        <td>
                            <select name="UrCmbState1" id="UrCmbState2">
                                <option value="Select One">Select One</option>
                                <option value="Bihar">Bihar</option>
                                <option value="Uttar Pradesh">Uttar Pradesh</option>
                            </select>
                        </td>
                    </tr>
                    
                    <tr>
                        <td></td>
                        <td></td>
                        <td>
                            <input type="submit" name="sub" id="subSubmit2" value="Submit">
                            <input type="reset" name="sub" id="subReset2" value="Reset">                                         
                        </td>
                    </tr>
                </table>
             </fieldset>
          </center>
      </form>
    </body>
</html>

//--------------  OR  -------------

  while(rs.next())
                    {                       
                        UrCmbMcode4=rs.getString("UrCmbMcode5");
                        if(UrCmbMcode4.equals("Bihar"))
                        {
                            Cmb1="selected";
                        }
                        if(UrCmbMcode4.equals("Uttar Pradesh"))
                        {
                            Cmb2="selected";
                        }                        
                    }

                    <tr>
                        <td>State</td>
                        <td>:</td>
                        <td>
                            <select name="UrCmbState1" id="UrCmbState2" >
                                <option value="Bihar"<%=Cmb1%>>Bihar</option>
                                <option value="Uttar Pradesh"<%=Cmb2%>>Uttar Pradesh</option>                                
                            </select>                            
                        </td>
                    </tr>

                  

Loading

Categories: JSP

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.