日期:2014-05-18  浏览次数:20651 次

求一个从数据库中提取的下拉列表
form   对象如何得到数据库的记录,绑定到   下拉列表中...

------解决方案--------------------
我有asp版本的,java的没有,楼主要的话可以给我留言
------解决方案--------------------
把form封到 arraylist 里面
------解决方案--------------------

ArrayList alValue = new ArrayList();
读取结果集放到
while (rs.next()) {
alValue.add(value);
}

定义变量生成下拉
StringBuffer buffer = new StringBuffer( " ");
for (int i = 0; i < alValue.size(); i++) {
buffer.append( " <option value= ' " + (String) alValue.get(i) + " ' selected> " + (String) alValue.get(i) + " </option> \n ");
}

大体思路就是这么写,自己实现一下把
------解决方案--------------------
如果是用STRUTS需要在CONFIG中配置一下即可
如果是用JSP直接取,跟 "lsj19830812(李逵) "说得差不多,或者直接在页面中写
------解决方案--------------------
***********jsp页面*************
showuser.jsp

<html:select property= "userid ">
<html:options collection= "userarray " property= "userid " labelProperty= "username " />
</html:select>

************Action**************
public ActionForward showUser(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
UserForm form = (UserForm) arg1;
UserModel model = new UserModel();
arg2.setAttribute( "userarray ", model.getAllUser(this
.getDataSource(arg2, "myds ")));

return arg0.findForward( "showuser ");
}
*************Model*************
public ArrayList getAllUser(DataSource ds) {
String sql = "select * from user ";

DbManager manager = null;
try {
manager = new DbManager(ds.getConnection());
ResultSet rs = manager.execQuery(sql);

ArrayList array = new ArrayList();
while (rs.next()) {

UserForm form= new UserForm();
form.setUserid(rs.getInt( "userid "));
form.setUsername(rs.getString( "username "));
array.add(form);
}
rs.close();

return array;
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally {
manager.close();
}
return null;
}
------解决方案--------------------
<select name= "courseName ">
<%
List list = (new CourseDB(CLASSFORNAME,SERVANDDB,USER,PWD)).getCourseName();
Iterator it = list.iterator();
while(it.hasNext())
{
CourseID courseID = (CourseID)it.next();
%>
<option value= <%=courseID.getCourseID()%> > <%=courseID.getCourseName()%> </option>
<%}%>

%>
</select>
// 获取数据库中,所以课程的名称
public List getCourseName() throws Exception
{
List list = new ArrayList();
try
{
statement = con.createStatement();
rs = statement.executeQuery( "select courseID,courseName from courseIDToName ");
int course_id_count = 0;
if(rs.next())
{
course_id_count = rs.getInt(1);
rs.close();
}

if(course_id_count > 0)
{
rs = statement.executeQuery( "select courseID,courseName from courseIDToName ");
while ( rs.next() )