2014-10-30 66 views
0

我試圖填補從MySQL database.First選擇框中的值(拆分值由逗號),我會讓我的數據庫設計之下,如何通過spilit逗號(,)從DB存儲選擇框值?

標準學科特徵

我學植物,動物,人類,自然

我數學整數,司,模塊

II科學BB,CC,DD,FF,GG

我的代碼,我想到目前爲止,

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> 

有選擇feautures應當由一個在chapterdropdown選擇框顯示一個。

請有人解決我的問題。

+0

http://stackoverflow.com/questions/10631715/splitting-a-comma-分隔線 – cy3er 2014-10-30 08:25:02

回答

1

下面的代碼片段可以幫助你

<% 

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> 
+0

其工作。非常感謝你! – MMMMS 2014-10-30 10:06:59

1

您可以分割逗號分隔值如下

String temp = "a,b,c"; 
String values[] = temp.split(","); 
for(String str : values){ 
    out.print(str); 
}