Results 1 to 2 of 2
  1. #1
    sandy1000's Avatar
    sandy1000 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Smile dynamically populate a dropdown box using jsp

    first prepare a package my package and in it create a connection class as defined below
    connection.javapackage mypackage;
    import java.sql.*;

    public class connection {
    public String url = "jdbc:odbc:shanu";
    public Connection con;
    public Statement st;

    public void get_connection()
    {
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con = DriverManager.getConnection(url);
    st=con.createStatement();
    }catch(Exception e){}
    }
    public void close_connection()
    {
    try{
    con.close();
    }catch(Exception e){}
    }
    }

    Now prepare a class named state

    state.java

    package mypackage;
    import java.sql.*;
    public class state extends connection {


    public ResultSet getStates()
    {
    ResultSet rs = null;

    try{
    get_connection();
    rs= st.executeQuery("select * from state");

    }catch (Exception e){}
    return (rs);
    }


    }


    Now finally prepare a jsp page states1.jsp

    <%@page import = "java.io.PrintWriter" %>
    <%@page import="java.sql.*"%>
    <%@page import="mypackage.state.class"%>
    <script language="javascript">
    function showcity(str)
    {
    alert(str);
    }

    </script>
    <%

    //String tables = "<table><tr><td>hiii</td></tr></table>";
    //out.print(names);
    try{
    state s = new state();
    ResultSet rs= s.getStates();
    String ss;

    %>
    <select id="combos" name="combos" onChange="showcity(this.value)">
    <%
    while(rs.next())
    {
    ss= rs.getString(2);
    %>
    <option value= "<%= ss %>" ><%=ss %></option>
    <%
    }
    %>
    </select>

    <%
    }
    catch(Exception e){}
    %>

    Modify the web.xml as defined below
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
    <param-name>debug</param-name>
    <param-value>2</param-value>
    </init-param>
    <init-param>
    <param-name>detail</param-name>
    <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <session-config>
    <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
    <welcome-file>states1.jsp</welcome-file>
    </welcome-file-list>
    </web-app>


    Now run the project
    Last edited by sandy1000; 12-28-2010 at 10:09 AM.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    You should not write business logic in jsp.
    Anyway as a starter what you need to do is put exeception statement in catch and check whether the table has any values.Might be table is empty.
    Ramya:cool:

Similar Threads

  1. To populate dropdown in Struts 1.3.8
    By techV in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-27-2010, 08:49 AM
  2. Jtextbox with dropdown list
    By vijay_s in forum AWT / Swing
    Replies: 1
    Last Post: 05-08-2008, 10:17 PM
  3. getting values from database in dropdown
    By aa74 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-21-2008, 10:00 PM
  4. Servlet with a Form/Dropdown
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:24 AM
  5. Help with dropdown list
    By barney in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •