商品类别下拉菜单动态化
- 数据库中建立TypeGoods表(typeId,typeName),字符串
- 创建商品类型Bean,TypeGoods.java
package mybean;
public class TypeGoods {
private String typeId;
private String typeName;
public String getTypeId() {
return typeId;
}
public void setTypeId(String typeId) {
this.typeId = typeId;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
3.编写TypeGoodsDAO的list方法实现 链表显示
package mybean;
import java.sql.*;
import java.util.LinkedList;
public class TypeGoodsDAO {
public LinkedList<TypeGoods> list()
{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from typeGoods";
LinkedList<TypeGoods> ts=new LinkedList<TypeGoods>();
try{
Class.forName(Const.DRIVER);
conn=DriverManager.getConnection(Const.URL, Const.USER,Const.PASSWORD);
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
TypeGoods t=new TypeGoods();
t.setTypeId(rs.getString("typeId"));
t.setTypeName(rs.getString("typeName"));
ts.add(t);
}
}catch(Exception e){
e.printStackTrace();
}
try {
if(rs!=null)
{rs.close();}
if(ps!=null)
{ps.close();}
if(conn!=null)
{conn.close();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ts;
}
}
4.JSP页面good_add.jsp页面中实现动态选择
<tr>
<td>商品类型</td>
<td>
<select name="type" id="type">
<option value="">--商品类型--</option>
<%
TypeGoodsDAO tdao=new TypeGoodsDAO();
LinkedList<TypeGoods> ts=tdao.list();
for(TypeGoods t:ts){
%>
<option value="<%=t.getTypeId()%>"><%=t.getTypeName()%></option>
<%} %>
</select>
</td>
</tr>