hi,

i have two combo boxes one for country and another for state
both populated from data base dynamically.

<%@page import="java.sql.*"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       <select name="ctry">
  <% 
String city="";
Statement pstm;
ResultSet rs;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connect =DriverManager.getConnection("jdbc:oracle:thin:@172.17.0.14:1521:develop","devuser","devuser");
pstm = connect.createStatement();
rs = pstm.executeQuery("select * from sam1");

while (rs.next()) { 
    city = rs.getString(1);
    %>

<option>
<%out.println(city); %> </option>
<%
}
rs.close();
pstm.close();
connect.close();
}
catch(Exception ex){
System.out.println(ex);
}
%>
          
       </select>

       <select>
<%         
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connect =DriverManager.getConnection("jdbc:oracle:thin:@172.17.0.14:1521:develop","devuser","devuser");
pstm = connect.createStatement();
rs = pstm.executeQuery("select * from sam1 where cntry = '"+ city +"'");
while (rs.next()) { 
    city = rs.getString(2);
    %>

<option>
<%out.println(city); %> </option>
<%
}
rs.close();
pstm.close();
connect.close();
}
catch(Exception ex){
System.out.println(ex);
}
%>
       </select>
    </body>
</html>

the variable city is always point to the last value of the country
how to make this???

help me please

i search in google i saw onchange event, but i am not clear with so give some example code for that.

Recommended Answers

All 2 Replies

hi friends i got the solution
please any one know the better solution please suggest me

<%@ page import="java.util.*" %>
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<HTML>
    <FORM ACTION="index.jsp" METHOD="get">
        <%
        Connection conn = null;
        String userSelectedCategory = request.getParameter("userSelectedCategory");
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@172.17.0.14:1521:develop", "devuser", "devuser");
        } catch (Exception e1) {
            System.out.print(e1);
        }
        if (conn != null) {
        %>
        <SELECT NAME="userSelectedCategory" onChange="this.form.submit();">
            <%
     PreparedStatement mystatement = conn.prepareStatement("SELECT cntry from sam1");
     ResultSet result = mystatement.executeQuery();
     out.println("<option value=\"-\">-</option>");
     while (result.next()) {
         String optionCategoryValue = result.getString(1);
         String optionTag = "<OPTION VALUE=\"" + optionCategoryValue + "\"";
         if (optionCategoryValue.equals(userSelectedCategory)) {
             optionTag += " selected=\"selected\"";
         }
         optionTag += ">" + optionCategoryValue + "</OPTION>";
         out.println(optionTag);
     }
     result.close();
     mystatement.close();
            %>
        </SELECT>
        <%
     if (userSelectedCategory != null) {
        %>

        <SELECT NAME="secondSelectBox" ><!--onChange="this.form.submit();"> -->

            <%
            PreparedStatement mySecondStatement = conn.prepareStatement("SELECT * FROM sam1 WHERE cntry = '" + userSelectedCategory + "'");
            ResultSet mySecondResult = mySecondStatement.executeQuery();

            while (mySecondResult.next()) {
                String somethingString = mySecondResult.getString(2);
                out.println("<OPTION VALUE=\"" + somethingString + "\">" + somethingString + "</OPTION>");
            }
            mySecondResult.close();
            mySecondStatement.close();
            %>
        </SELECT>
        <%
            }
            conn.close();
        }
        %>
    </FORM>
</HTML>

i have one, dont know if it is any better although as i am learning.


html dropdown box...

<p for="workArea">Work Area: 
			<select name="workArea" id="workArea"  property="workArea" style="overflow:hidden;maxheight:10px;">
			<% 	
			DataAccess dataAccess = new DataAccess();
			ArrayList workAreas = dataAccess.loadWorkAreas();;
			Iterator it = workAreas.iterator();
			String xx = null;
			while(it.hasNext()) { 
			 xx = (String) it.next(); %>
			<option  name="workArea" id="workArea" value="<%=xx%>"><%=xx%></option>
			<% } %>

and in the java method...

public ArrayList loadWorkAreas() {    	   

		boolean bln_result = true;

		conn_fandp = new conn();

		bln_result = conn_fandp.getConnection(jdbc_connection);		       

		if (bln_result == false)  {
			System.out.println("Connection to DCS FANDP schema failed.");
		}
		else {
			System.out.println("Connection to DCS FANDP succeded.");   
		}   

		ArrayList workAreas = new ArrayList();

		try {
			String sql ="select prod_workarea from fandp.prod_workarea";
			ResultSet rs = null;
			Connection curr_conn  = conn_fandp.current_connection;			 	    	

			rs = curr_conn.createStatement().executeQuery(sql);

			while ( rs.next()) {	

				workAreas.add(rs.getString("prod_workarea"));
			}  
			rs.close();
			curr_conn.close();
		}
		catch(Exception e) {

			System.out.println(e.getMessage());
		}
//		get an Iterator object for ArrayList using iterator() method.
		Iterator itr = workAreas.iterator();

//		use hasNext() and next() methods of Iterator to iterate through the elements.
		System.out.println("Iterating through ArrayList elements...");
		while(itr.hasNext()){
			System.out.println("Iterations:" + itr.next());
		}
		//System.out.println("bryn " + itr.hasNext());

		conn_fandp.current_connection = null;			     	
		conn_fandp = null;
		//for testing purposes...
		workAreas.add("Bryn21");
		workAreas.add("bjdfd");
		return workAreas;
	}

now...can somebody please help me deploy in tomcat, i can get my jsp and bean working in eclipse under local tomcat but when i try and run it in a browser through local tomcat it does not work properly, no errors but it doesnt submit to do the db. i can add code if somebody can help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.