解决如何通过循环来使用数据库的值设置jsp的select标签的option值

 

Select 处的代码:

[html] view plain copy
 
  1. <select name="position">  
  2. <span style="white-space:pre">    </span><%  
  3.     <span style="white-space:pre">    </span>while(rs.next()){  
  4.     %>  
  5.     <option value="<%=rs.getString("position")%>"><%=rs.getString("position") %</option>  
  6.     <!-- 循环得到数据库查找rs中的值并赋值到select的option中 -->  
  7. <span style="white-space:pre">    </span><%  
  8.     <span style="white-space:pre">    </span>}  
  9.         rs.close();  
  10.         st.close();  
  11.         conn.close();  
  12.     %>  
  13. </select>  
[html] view plain copy
 
  1. 这是与mysql数据库连接得到select的option共有那些选项的代码:  
[html] view plain copy
 
  1. <pre name="code" class="html"><%  
  2.     //与数据库建立连接  
  3.     Class.forName("com.mysql.jdbc.Driver");  
  4.     String url = "jdbc:mysql://localhost:3306/test";  
  5.     String sqlName = "root";  
  6.     String sqlPwd = "root";  
  7.     Connection conn = DriverManager.getConnection(url,sqlName,sqlPwd);  
  8.     Statement st = conn.createStatement();  
  9.     String sqlStr = "select * from tb_position";  
  10.     ResultSet rs = st.executeQuery(sqlStr);             //得到的rs结果集  
  11. %>  

from: http://blog.csdn.net/lei196299/article/details/45059325

 

posted @ 2017-12-03 22:14  ec04  阅读(858)  评论(0编辑  收藏  举报