1

How can I populate a dropdownlist in a .jsp page, retrieving fields names of a specific table from a mysql database? Thanks in advance

2
  • 1
    Did you event try to use google ? Commented Jan 16, 2012 at 11:01
  • I tried but google google omniscience didn't help me Thanks for your very useful suggestion Commented Jan 16, 2012 at 11:14

1 Answer 1

1

Let us consider you have a class called DBConnection where you can connect to your DB. Create a class called ListObject.java as-

package mypackage;
public class ListObject {
    public List<MyObject> getList() {
        Statement st = new DBConnection().getConnection().createStatement();
        ResultSet rs = st.executeQuery("select * from myTable");
        List<MyObject> list = new ArrayList<MyObject>();
        while(rs.next()) {
            list.add(new MyObject(rs.getString(1), rs.getString(2)));
        }
        st.close();
        rs.close();
        return list;
    }
}

Now on the jsp, use following-

<%@page import="mypackage.ListObject"%>
<select>
<%
    Iterator it = new ListObject().getList();
    while(it.hasNext()) {
        out.write("<option value=\""+ object.getFieldA();+ "\">"+ object.getFieldB() +"\">");
    }
%>
</select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.