jcombobox没有加载数组中的值

时间:2009-10-27 01:29:04

标签: java mysql jcombobox

jComboBox1.setModel(customers);


public class CustomerData {

private static final String JDBC_CONNECTION_URL = "jdbc:mysql://localhost/test";
private static final String DATABASE_USER       = "root";
private static final String DATABASE_PASSWORD   = "root";

// query to select customer data
private static final String SQL_FETCH_CUSTOMERS = "SELECT custName FROM customers";
private Connection connection = null;


public CustomerData(){
    initConnection();
}


private void initConnection() {
    try {
        //load the mysql driver
        Class.forName("com.mysql.jdbc.Driver");
        //create the database connection
        connection = DriverManager.getConnection(JDBC_CONNECTION_URL, DATABASE_USER, DATABASE_PASSWORD);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

public void closeConnection(){
    if (connection != null) {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            connection = null;
        }
    }
}


public ArrayList fetchCustomerData(){
    if (connection != null){
        Statement statement = null;
        try {
            statement = connection.createStatement();
            //get results from database
            ResultSet resultSet = statement.executeQuery(SQL_FETCH_CUSTOMERS);
            //get customers from resultset and return them to the app
            return convertResultSetToCustomersArray(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //close the statement we just used
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }else{
        System.out.println("NO VALID DATABASE CONNECTION, CAN'T FETCH CUSTOMER DATA!");
    }
    return new ArrayList();
}

private ArrayList convertResultSetToCustomersArray(ResultSet results) throws SQLException{
    ArrayList customers = new ArrayList();

    //loop the results and add customers to an ArrayList
    while (results.next()){
        customers.add(results.getString("custName"));
    }
    return customers;
}
    private String[] customers = null;
    private void initData(){
    CustomerData customerData = new CustomerData();

    ArrayList custArrayList = customerData.fetchCustomerData();

    //get the array from the ArrayList and cast it to a String[]
    customers = (String[]) custArrayList.toArray(new String[0]);
}

}

2 个答案:

答案 0 :(得分:3)

从我可以从您的代码中收集到的内容,您尝试将String[]设置为JComboBox的模型。这不行。你可以将这个数组包装在DefaultComboBoxModel中,如下所示:

jComboBox1.setModel(new DefaultComboBoxModel(customers));

答案 1 :(得分:0)

要检查的事项:

  1. 查询实际上是否返回了什么内容?
  2. jComboBox1.setModel(customers);在您的代码中实际存在于哪里?客户是如何创建的?也许你需要发布那部分代码。
  3. 您传递给setmodel方法的客户对象是什么类型的?