Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

List MySQL column in a JComboBox

Options
  • 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 Posts: 1,184 ✭✭✭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 Posts: 7,498 ✭✭✭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