gpt4 book ai didi

database - 使用数据库中存在的值填充 jsp 下拉菜单

转载 作者:搜寻专家 更新时间:2023-10-30 20:26:45 24 4
gpt4 key购买 nike

如何使用数据库中存在的值填充 JSP 中的下拉菜单(遵循 MVC 架构)?试过这样做里面EmployeeDAO.java

private List<EmployeeVO> empIDList;

public List<EmployeeVO> getStateList(){
empIDList=new ArrayList<EmployeeVO>();
Connection con=DBUtil.getConnection();



try {
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select Emp_id from Employee order by Emp_id ASC");
while(rs.next()){
String emID=rs.getString("Emp_id");
int empID=Integer.parseInt(emID);
EmployeeVO s=new EmployeeVO();
s.setEmp_id(empID);
empIDList.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
return empIDList;
}

里面EmployeeVO.java

private int Emp_id;
private String Emp_pwd;
private String Emp_fname;
private String Emp_mname;
private String Emp_lname;
private String Emp_dob;
private String Emp_doj;
private String Emp_status;
public int getEmp_id() {
return Emp_id;
}
public void setEmp_id(int emp_id) {
Emp_id = emp_id;
}

里面员工.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="emplID" scope="session" class="com.model.EmployeeVO" />

<th>Employee ID</th>
<td><input type="text" name="EID" id="eid" />
<select
id="testlist">
<c:forEach var="item" items="${emplID.items}">
<option>${item}</option>
</c:forEach>
</select>
</td>

jsp 的源代码取自 this

最佳答案

也许你可以试试这个Vectorarraylist 方法..

jsp部分(矢量)

<%
Vector vFRUIT_CODE = new Vector();
Vector vFRUIT_DESCP = new Vector();

String SQL = "SELECT * FROM TB_TEST";
DB_TEST.makeConnection();
DB_TEST.executeQuery(SQL);
while(DB_TEST.getNextQuery())
{
String sCODE = DB_TEST.getColumnString("FRUIT_CODE");
String sDESCP = DB_TEST.getColumnString("FRUIT_DESCP");

vFRUIT_CODE.addElement(sCODE);
vFRUIT_DESCP.addElement(sDESCP);
}
DB_TEST.takeDown();
%>

html部分

<select name="FRUIT">
<option value="">--- Please Select ---</option>
<%
for (int i=0;i<vFRUIT_CODE.size();i++) {
String sCODE = (String) vFRUIT_CODE.elementAt(i);
String sDESCP = (String) vFRUIT_DESCP.elementAt(i);
%>
<option value="<%=sCODE %>" ><%= sDESCP %></option>
<%}%>
</select>

关于database - 使用数据库中存在的值填充 jsp 下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30390025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com