Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

List MySQL column in a JComboBox

  • 31-03-2005 12:10AM
    #1
    Closed Accounts Posts: 2


    Hi i am trying to place a list of names from a column in a tables in mysql database.

    I cant seem to find out how to do this. Can anyone help. I can access the database and execute commands no problem, im just not sure how i can do the above.

    Any help would be appreciated. thanks.


Comments

  • Registered Users, Registered Users 2 Posts: 1,171 ✭✭✭causal


    Check out the java.sql.ResultSet interface, it's the jdbc equivalent of the RecordSet in odbc.
    Set it (i.e. your class that implements it) to hold the data retrieved by your query, and then use it's methods to navigate and retrieve your results.

    hth,
    causal


  • Registered Users, Registered Users 2 Posts: 7,544 ✭✭✭BrokenArrows


    I am bonus just incase you are wondering im just logged in under a different name. No Point in logging in and out again.

    This is my attempt to get this to work but its having problems. It enters the first row of the specified column into the JComboBox but then causes a java.lang.NullPointerException error.

    Here is the code :

    public void PopulateCombo()
    {
    int cnt=0;
    String ans = "Yes";

    label0:
    {
    Connection c = getConnection();
    Statement stmt = null;

    cnt = 0;
    String name = (String)jRoomType.getSelectedItem();

    try
    {
    stmt = c.createStatement();
    ResultSet rs = stmt.executeQuery("select Vacant, RoomNum FROM rooms");
    String vacant;
    String roomNum;


    do
    {
    if(!rs.next())
    break label0;
    vacant = rs.getString("Vacant");
    list[cnt] = rs.getString("RoomNum");

    System.out.println("after list[cnt]");
    cnt=cnt+1;



    } while(!name.equalsIgnoreCase(ans));
    }
    catch(Exception et)
    {
    System.out.println("outside while");
    System.out.println(et);
    }


    }





    }


    The list array is specified as a global variables and i am adding the list to the jcombobox using JComboBox jRooms = new JComboBox(list);

    If you can see what the problem is please help.


Advertisement