views:

294

answers:

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

}

A: 

Things to check:

  1. Is the query actually returning anything?
  2. The line jComboBox1.setModel(customers); where is it actually in your code? And how is customers being created? Maybe you need to post that part of the code.
  3. What type is the customers object that you are passing to the setmodel method?
Vincent Ramdhanie
+3  A: 

From what I can gather from your code, you are trying to set a String[] as the model for your JComboBox. This wont work. you can wrap that array in a DefaultComboBoxModel like so:

jComboBox1.setModel(new DefaultComboBoxModel(customers));
akf
mmm that fixed some of it, now it just tells me it can't find the variable customers
Jason
nm, fixed that, thanks
Jason