在JSP中使用数据库中的值创建动态组合框
我想在jsp中创建一个组合框,其中包含从数据库中获取的值。 下面是我编写的代码,但它返回一个空白组合框,即使数据库中有值在JSP中使用数据库中的值创建动态组合框,jsp,jdbc,combobox,Jsp,Jdbc,Combobox,我想在jsp中创建一个组合框,其中包含从数据库中获取的值。 下面是我编写的代码,但它返回一个空白组合框,即使数据库中有值 <select> <% Connection con=null; ResultSet rs=null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:db","root","root");
<select>
<%
Connection con=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:db","root","root");
HttpSession ss=request.getSession();
String uid=(String)ss.getAttribute("id");
PreparedStatement pst=con.prepareStatement("select name from emp where uid=?");
pst.setString(1,uid);
rs=pst.executeQuery();
while(rs.next())
{
out.print(rs.getString("name"));
%>
</select>
<%
}
}catch(Exception e)
{ out.print(e);
}
%>
那么:
<select>
<%
Connection con=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:db","root","root");
HttpSession ss=request.getSession();
String uid=(String)ss.getAttribute("id");
PreparedStatement pst=con.prepareStatement("select name from emp where uid=?");
pst.setString(1,uid);
rs=pst.executeQuery();
while(rs.next())
{
String name = rs.getString("name");
%>
<option value="<%=name%>"><%=name%></option>
<%
}
}catch(Exception e)
{ out.print(e);
}
%>
</select>
它抛出一个异常,并在组合框中显示:java.sql.SQLException:找不到数据,只做了一些更改,效果很好:)我刚刚在互联网上读到,对同一列的getString的第二次调用是问题所在。有些司机允许,有些不允许。因此,我只是将value参数更改为value=x,并保留另一半以在组合框中显示它。谢谢:)更新!事实上,有些驱动程序不允许获取多个数据。