字典表与列表框的讨论

Thursday May 17, 2007

在最近的一个小系统里要加入一个功能,一个列表框,数据从数据库的字典表中取得,研究了一下,实现了,记录下
只显示一个字段的内容,我采用Vector来实现,代码如下

package com.xx.work.data;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
 
import com.aheadsoft.work.dao.DBConnect;
 
public class Add {
	private int ID;
	private String workType;
	public int getID() {
		return ID;
	}
	public void setID(int id) {
		ID = id;
	}
	public String getWorkType() {
		return workType;
	}
	public void setWorkType(String workType) {
		this.workType = workType;
	}
	public Add(){
		ID = 0;
		workType = "";
	}
	public Add(int ID, String workType){
		this.ID = ID;
		this.workType = workType;
	}
	public void setID(String newID) {
		if (newID != null)
			this.ID = Integer.parseInt(newID);
	}
	public static Vector  excute() throws Exception{
		Vector list = new Vector();
		String Str = "select * from workType";
		DBConnect dbc = null;
		ResultSet rs = null;
		try {
			dbc = new DBConnect();
			try{
				rs = dbc.executeQuery(Str);
				while (rs.next()) {
					Add add = new Add();
					add.setWorkType(rs.getString("workType"));
					list.add(add);
				}
			}catch(SQLException e){
				e.printStackTrace();
			}finally{
				if(rs != null)
					rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(dbc !=  null)
				dbc.close();
		}
		return list;
	}
	public static void main(String [] args){
	}
}


JSP页面实现代码如下

<%@ page import="com.aheadsoft.work.data.Add"%>
//其它略
<select name="workType" style="width: 160; height: 23">
	<option selected value="">
		选择工作类型
	</option>
	<%
		Vector work = Add.excute();
                for(int i=0;i<work.size();i++){
                    Add add = (Add)work.get(i);
	 %>
<option><%=add.getWorkType()%></option>
<%}%>
</select>

2 Comments

  1. Besides the fact that some are only there to fill the politically correct quotas but seem totally incapable of fulfilling their duties. ,

  2. You’re stuck with the experience of what it’s like to be a human being. ,

Leave a reply