java - jcombobox 不从数组加载值

标签 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]);
}

}

最佳答案

根据我从您的代码中收集到的信息,您正在尝试将 String[] 设置为 JComboBox 的模型。这行不通。您可以将该数组包装在 DefaultComboBoxModel 中,如下所示:

jComboBox1.setModel(new DefaultComboBoxModel(customers));

关于java - jcombobox 不从数组加载值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1628291/

相关文章:

java - 闪屏图片不出现

java - 优化 Java 图形

mysql - React.js 将数据库(sql)中的 html 动态注入(inject)到不应用 css 样式的组件中?

java - 具有 MySQL 故障转移功能的 Hibernate Web 应用程序

java - 为什么我的 JComboBox 的 ComboBoxModel 不调用 seSelectedItem()?

java - 从 "messy"Java 字符串中提取多种数据类型

java - Android - 无法转换 org.ksoap2.soapfault

php - 如何使用explode()将2个单词分成2个不同的字符串

java - Swing 部件透明度故障

java - 将JComboBox添加到JTable : ComboBoxes in different rows are not independent