Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jcombobox未从数组加载值_Java_Mysql_Jcombobox - Fatal编程技术网

Java jcombobox未从数组加载值

Java jcombobox未从数组加载值,java,mysql,jcombobox,Java,Mysql,Jcombobox,}需要检查的事项: 查询是否实际返回了任何内容 行jcombox1.setModel(客户)它在代码中的实际位置?如何创建客户?也许你需要发布这部分代码 传递给setmodel方法的customers对象是什么类型 根据我从您的代码中收集到的信息,您正在尝试将字符串[]设置为JComboBox的模型。这行不通。您可以将该数组包装在DefaultComboxModel中,如下所示: jComboBox1.setModel(customers); public class CustomerDat

}需要检查的事项:

  • 查询是否实际返回了任何内容
  • jcombox1.setModel(客户)它在代码中的实际位置?如何创建客户?也许你需要发布这部分代码
    
  • 传递给setmodel方法的customers对象是什么类型

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

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

    这个问题的重点是什么?您已经为此主题创建了另一个帖子。您没有遵循该帖子中给出的建议。阅读这个问题的人不知道你想做什么,为什么你要浪费每个人的时间?这不是获得帮助的方法。嗯,修复了其中一些问题,现在它告诉我它找不到可变客户
    jComboBox1.setModel(new DefaultComboBoxModel(customers));