Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to retrieve data from MYSQL database to JComboBox or to an array

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by A4Andy; September 5th, 2011 at 05:33 AM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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 = ....

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to retrieve data from MYSQL database to JComboBox or to an array

    I change my while statement to
    while(rs.next())
    {
      array=rs.getString(2);
     }

    but I got an error Type mismatch cannot convert from String to String[]
    Last edited by A4Andy; September 5th, 2011 at 09:44 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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

  7. The Following User Says Thank You to Norm For This Useful Post:

    A4Andy (September 5th, 2011)

  8. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Retrieve data from other table
    By kichkich in forum JDBC & Databases
    Replies: 2
    Last Post: June 23rd, 2011, 07:51 AM
  2. [SOLVED] Connection to MySql Database
    By dego89 in forum JDBC & Databases
    Replies: 8
    Last Post: May 26th, 2011, 08:56 AM
  3. [SOLVED] Passing data to a MySQL database
    By JDyson in forum JDBC & Databases
    Replies: 1
    Last Post: December 1st, 2010, 01:31 PM
  4. MySQL Database
    By pjeremy90 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 12th, 2010, 10:34 AM
  5. [SOLVED] Get Data From Database MySQL using ComboBox
    By Simple in forum Java Theory & Questions
    Replies: 2
    Last Post: June 4th, 2010, 02:45 AM