0

I am trying to fill the select box values(split values by comma) from mysql database.First, i show my DB design below,

Standard Subject Features

I Science Plants,Animals,Humans,Nature

I Maths Integer,Division,Modules

II Science BB,CC,DD,FF,GG

my code i tried so far,

ListFeature.jsp

<select name="chapterdropdown" id="chapterdropdown"
                    onchange="selectchange()">

<%
Class.forName("com.mysql.jdbc.Driver");
Connection connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/raptor1_5","root","");
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("select * from contentselection where Standard='"+class1+"' and Subject='"+subject+"'");

                while(rs.next())
                        {
                           String featvar = rs.getString("Features");

                           // here how to split the features by comma???

                          %>

                              <option><%=featvar%></option>

                        <% } %>                     
            %>
</select>

there selected feautures should display one by one in chapterdropdown select box.

Please someone fix my problem.

MMMMS
  • 2,179
  • 9
  • 43
  • 83

2 Answers2

1

You can split comma separated values as follow

String temp = "a,b,c";
String values[] = temp.split(",");
for(String str : values){
    out.print(str);
}
Vicky Thakor
  • 3,847
  • 7
  • 42
  • 67
1

The following code snippet may help you

 <%

 String data=" Science Plants,Animals,Humans,Nature";//your data from db

 String [] values=data.split(",");

%>

<select>
 <%
   for(int i=0;i<values.length;i++)
   {
 %>

  <option><%=values[i]%></option>

 <%
 }
 %>

</select>
Anptk
  • 1,125
  • 2
  • 17
  • 28