How to retrieve data from MYSQL database to JComboBox or to an array
the field type is text
i really have no idea hope you guys can help me
Re: How to retrieve data from MYSQL database to JComboBox or to an array
Which bit do you have no idea about - constructing a JComboBox, transferring data from resultSet to array/Vector, or retrieving data from MySQL? You should: SELECT a ResultSet from MySQL (java.sql.Connection, java.sql.Statement, java.sql.ResultSet), transfer the data you want to display to the user into an array or java.util.Vector, invoke the JComboBox constructor with the data you've collected.
Re: How to retrieve data from MYSQL database to JComboBox or to an array
transferring mysql data to array is my problem
here is the code that I'm playing with
Code :
public int i;
public String [] array;
PreparedStatement pstmt = con.prepareStatement("Select * from products");
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
array[i]=rs.getString(2);
i++;
}
JComboBox combo = new JComboBox(array);
I'm getting no error but eclipse is suspends the program
highlighting (exception nullpointer exception)
array[i]=rs.getString(2);
Re: How to retrieve data from MYSQL database to JComboBox or to an array
Where do you give a value to the array variable?
You give a value to a variable by using the assignment statement:
array = ....
Re: How to retrieve data from MYSQL database to JComboBox or to an array
I change my while statement to
Code :
while(rs.next())
{
array=rs.getString(2);
}
but I got an error Type mismatch cannot convert from String to String[]
Re: How to retrieve data from MYSQL database to JComboBox or to an array
The array variable is a String[] array.
The getString() method returns a String.
You need to give the array variable a value. It is defined as String[]
You need to use the new statement to give it a String[] value.
array = new String[1]; // give the variable array a value
Re: How to retrieve data from MYSQL database to JComboBox or to an array
thanks it's already solved. I forgot the give the array a value