I have combo box and two type of employee in my db I want to display full time emplyee in the combo box however when I tested the code I this line is executed test = conn.prepareStatement("select * FROM Employee WHERE EmployeeType = " + "'" + "Part Time" + "'"); and not the first line. I have no I idea what is wrong with the code I'm sure that it is correct

  public void EmployeeCombo() {
        String EmployeeType = "Full Time";

        try {
            PreparedStatement test = null;
            ResultSet rs;

            Connection conn = ConnectionManager.getConnection();
            if (EmployeeType.equals("Full Time")) {
                test = conn.prepareStatement("select * FROM Employee WHERE EmployeeType = " + "'" + "Full Time" + "'");

            } else {
                test = conn.prepareStatement("select * FROM Employee WHERE EmployeeType = " + "'" + "Part Time" + "'");

            }
            rs = query.executeQuery();

            while (rs.next() != false) {
                String f = rs.getString("first");
                String l = rs.getString("last");
                Employee emp = new Employee(f, l);
                 ComboEmployee.addItem(emp.full());
            }

        } catch (SQLException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
         }

    }
  • you can to display only one value in JComboBox from Employee emp = new Employee(f, l);

  • whats ComboEmployee.addItem(emp.full());

  • there are two ways add

    1. a new Items to JComboBox directly (have to decide if String f or String l)

    2. use XxxListCellRenderer by using 2D array Employee emp = new Employee(f, l);

  • everything is on Oracles tutorials, there could be an issue with XxxListCellRenderer for newbee, but nothing special nor complicated

Edited 2 Years Ago by mKorbel

How many times have we said this in the Java forum...
Never NEVER NEVER do this when writing new code:

    } catch (Exception e) {
    }

If/when there is an error you just told Java that you didn't want to know anything about it, and please discard the detailed error message that Java just created for you.
ALWAYS put an e.printStackTrace(); in your catch blocks until/unless you have a good reason to do something else.

Anyway, you prepare an SQL statement called "test", but then you execute one called "query" whose definition is not in the method you posted.

This article has been dead for over six months. Start a new discussion instead.