在 JSP 中使用数据库中的值创建一个动态组合框 [英] create a dynamic combo box using values from database in JSP
本文介绍了在 JSP 中使用数据库中的值创建一个动态组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 jsp 中创建一个组合框,其中包含从我的数据库中获取的值.这是我编写的代码,但它返回一个空白组合框,即使数据库中有值.
I want to create a combo box in jsp that contains values fetched from my database. Here's the code that I've written,but it returns a blank combo box,even though there are values in the database.
<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>
这篇关于在 JSP 中使用数据库中的值创建一个动态组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文