【问题标题】:jcombobox not loading values from an arrayjcombobox没有从数组中加载值
【发布时间】:2009-10-27 01:29:03
【问题描述】:
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]);
}

}

【问题讨论】:

  • 这个问题的意义何在?您已经在此主题上创建了另一个帖子。您没有遵循该帖子中给出的建议。阅读此问题的人不知道您要做什么,为什么要浪费大家的时间?这不是寻求帮助的方式。

标签: java mysql jcombobox


【解决方案1】:

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

jComboBox1.setModel(new DefaultComboBoxModel(customers));

【讨论】:

  • 嗯修复了一些,现在它只是告诉我它找不到变量客户
【解决方案2】:

检查事项:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2014-02-23
    • 1970-01-01
    • 2015-04-27
    • 2015-06-17
    • 2013-12-23
    • 1970-01-01
    相关资源
    最近更新 更多