对于JcomboBox,需要通过联数据库得到他的下拉框选项值的问题;
第一步,我们必须明白那个框框的值是压得bean对象,因为这个框框中对象还联系到很多别的值,并且需要在别的地方要用到,比如说,jtable中要显示与框框的值的相关数据,所以说……
我们首先的通过相关表建立bean类,并且要重写toString()方法,让toString()返回相关Jcombobox需要的值;
然后设计dao方法,让其返回一个Vector或list对象,其里面添加的是bean数据;
最后建立一个方法来为JcomboBox添加数据,然后调用该方法,就可以完成,
代码:
/**
*Bean:
*/
public class GoodsBeans {
int c_id ;
public int getC_id() {
return c_id;
}
public void setC_id(int c_id) {
this.c_id = c_id;
}
public String getC_name() {
return c_name;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public String getC_contPe() {
return c_contPe;
}
public void setC_contPe(String c_contPe) {
this.c_contPe = c_contPe;
}
public String getC_phone() {
return c_phone;
}
public void setC_phone(String c_phone) {
this.c_phone = c_phone;
}
public String getC_type() {
return c_type;
}
public void setC_type(String c_type) {
this.c_type = c_type;
}
public String getC_addre() {
return c_addre;
}
public void setC_addre(String c_addre) {
this.c_addre = c_addre;
}
public float getC_moret() {
return c_moret;
}
public void setC_moret(float c_moret) {
this.c_moret = c_moret;
}
public String getC_rema() {
return c_rema;
}
public void setC_rema(String c_rema) {
this.c_rema = c_rema;
}
String c_name ;
String c_contPe;
String c_phone ;
String c_type ;
String c_addre ;
float c_moret ;
String c_rema ;
public String toString() {
return c_name;
}
}
/**
*dao方法
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import com.beans.GoodsBeans;
import com.unit.DBUnit;
public class CustomerDao {
public Vector getCustomer() {
DBUnit dao = new DBUnit();
Vector date = new Vector();
Connection conn = dao.getConn();
PreparedStatement st = null;
ResultSet rs = null;
try {
String sql = "select * from customer";
st = conn.prepareStatement(sql);
rs = st.executeQuery(sql);
while (rs.next()) {
GoodsBeans ab = new GoodsBeans();
ab.setC_id(rs.getInt("c_id"));
ab.setC_name(rs.getString("c_name"));
ab.setC_contPe(rs.getString("c_contPe"));
ab.setC_phone(rs.getString("c_phone"));
ab.setC_type(rs.getString("c_type"));
ab.setC_addre(rs.getString("c_addre"));
ab.setC_moret(rs.getFloat("c_moret"));
ab.setC_rema(rs.getString("c_rema"));
date.add(ab);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
dao.connfree(rs, st, conn);
}
return date;
}
}
/**
*添加Item方法
*
public void comboxkefu_action(){
Vector name = new Vector();
CustomerDao dao = new CustomerDao();
name = dao.getCustomer();
for (Object o : name) {
comboxkefu.addItem(o);
}
}
/**
*diao yong
*