日期:2014-05-17  浏览次数:20711 次

JSP连接SQL做select下拉菜单时,value与option的值相同时,总是显示option外的值有误
JSP连接SQL做select下拉菜单时,value与option的值相同时,总是显示option外的值有误,请帮我改下吧!谢谢~~

Java code
<select name="name">
          <%    
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:tscgdb","sa","sa");
            Statement stat = con.createStatement();
            ResultSet rs = stat.executeQuery("select * from books");
            
            while(rs.next()){
            %> 
                     <option value="<%=rs.getString("bookname")%>">
                <%=rs.getString("bookname")%></option>
            <%         
            }
                rs.close();
            %>
             </select>


系统现实“<%=rs.getString("bookname")%></option>”有误


------解决方案--------------------
你可以把rs.getString("bookname")得到的值放到一个临时变量中,再分别把它赋给value值和显示值。
HTML code

<%    
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:tscgdb","sa","sa");
            Statement stat = con.createStatement();
            ResultSet rs = stat.executeQuery("select * from books");
           
            while(rs.next()){ 
                String name = rs.getString("bookname");
            %> 
           
                     <option value="<%=name%>">
                <%=name%></option>
            <%         
            }
                rs.close();
            %>