如何从数据库中读取字段名出来,然后写到列表框select的option 中

sunxiangyao 2006-03-15 09:45:09
在表格中有个列表下拉框,我在填写表格时要根据需要选择它的值,可是它的值要从数据库中读出来,就是把数据库中的一项字段的全部内容显示在列表框中。


请高手帮帮忙哦,我是刚学的师弟!!!
...全文
237 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zf121x 2006-03-15
  • 打赏
  • 举报
回复
// //此方法的作用在于取得已存在的记录并将其展现在下拉框中
public static String DrawListBox(DBManager dbm, String default_item,
String select_name, String sql, String selected_item, String js)
throws CSSException {
try {
String result = "";
String code = "";
String value = "";
ResultSet rs = null;

rs = dbm.excuteQuery(sql);
result = result + "<select name='" + select_name + "'";
if (!(js.equals(""))) {
result = result + js;// javascript
}
result = result + ">";
if (!(default_item.equals(""))) {
result = result + "<option value='"
+ Constants.Box_Default_Value + "'>" + default_item
+ "</option>"; // 有无默认选项
}
while (rs.next()) {
code = rs.getString(1).trim();
value = rs.getString(2).trim();
result = result + "<option value='" + code + "'";
if ((!(selected_item.equals("")))
&& (selected_item.equals(code)))
result = result + " selected";// 是否有请求提交的选项.
result = result + ">" + value + " </option>";
}

result = result + "</select>";

rs.close();
return result;
}// end of try
catch (SQLException sqlEx) {
// Debug.printStackTrace(sqlEx);
// SysLog.log(sqlEx.getMessage());
throw new CSSException("DataDictError");
} catch (CSSException e) {
throw e;
}
}
imA 2006-03-15
  • 打赏
  • 举报
回复
读取数据库的数据你会吧。
读出数据后将你要显示的数据放入一个数组列表(java.util.ArrayList),然后将这个数组列表存入request或者是session中,然后在jsp页面上用一个循环一次读出数据,显示在页面上。
当然如果你用了struts框架就更好了,可以采用struts的标签,就不用在页面上显式的用循环取数据了。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧