STEPHEN YAO
STEPHEN YAO

Reputation: 105

JAVA setting The String based on the value of ComboBox

I am trying to search the database by using column names from the String based from ComboBox that the user picked and I assign a String based from what the user picked and when I put in the SQL statement the String is NULL. Please help thank you! Here is my code :

String[] searchOptions={"ID","First Name","Middle Name","Last Name","Gender","Date of Birth","Nationality","Contact number"};
JComboBox comboBoxOption = new JComboBox(searchOptions);

textFieldSearch = new JTextField();
textFieldSearch.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent arg0) {

    try 
        String selected = (String)comboBoxOption.getSelectedItem();

        if(selected.equals("ID"))
            {
                String optionSearch="guest_id";
                System.out.println("baby");
            }else if(selected.equals("First Name"))
            {
                String optionSearch="guest_fname";
            }else if(selected.equals("Middle Name"))
            {
                String optionSearch="guest_mname";
            }else if(selected.equals("Last Name"))
            {
                String optionSearch="guest_lname";
            }else if(selected.equals("Gender"))
            {
                String optionSearch="guest_gender";
            }else if(selected.equals("Date of Birth"))
            {
                String optionSearch="guest_dob";
            }else if(selected.equals("Nationality"))
            {
                String optionSearch="guest_nationality";
                System.out.println(optionSearch);
            }else if(selected.equals("Contact number"))
            {
                String optionSearch="guest_contact";
            }else
            {
                String optionSearch=" ";
            }

           String query="Select * from guest_tbl where '"+optionSearch+"' =? ";
            PreparedStatement pst = conn.prepareStatement(query);
            pst.setString(1, textFieldSearch.getText());
            ResultSet rs =pst.executeQuery();
            tableGuest.setModel(DbUtils.resultSetToTableModel(rs));

Upvotes: 0

Views: 822

Answers (2)

RichardMcK
RichardMcK

Reputation: 35

You declare your String optionSearch within the if block, therefore it is not known outside of this. Declare and set the String optionSearch to some default value outside the if block, then based on conditions assign it a different value.

String optionSearch = "";
if(selected.equals("ID"))
{
    optionSearch="guest_id";
    System.out.println("baby");
}else if(selected.equals("First Name"))
{
    String optionSearch="guest_fname";
}
....
//prepare statement

Upvotes: 1

Florian Schaetz
Florian Schaetz

Reputation: 10652

That should not even compile. You are declaring a NEW "optionSearch" variable inside your if(...) blocks.

if( something ) {
    String myString = "x";
} 
System.out.println(myString); // This will NOT compile! myString is unknown here

The variable "myString" here is only known from the point on where it's declared until the closing brackets. Then it's gone. So what you do is declare a new variable in each block, which then, after the block is done, is lost. And after all the blocks, you try to access it. This will not work.

So, what you should do is something like...

String myString = null; // or whatever default value you want...
if (something) {
    myString = "x";
} else if (something else) {
    myString = "y";
} // etc.
System.out.println(myString); // This will compile and work

Here, you declare "myString" at the beginning, then set some value to it and, of course, can use it then. In your example, you set it, forget it, set it, forget it, etc. - and then try to use it. Probably you also have some other variable called "optionSearch" somewhere there, too, otherwise it wouldn't compile.

(And btw, for such if-blocks, I have found that enums are a good way to manage that in a better way. Declare an enum constant for each possibility and store the required value in a field. But that's probably a little bit to complicated at the moment.)

Upvotes: 1

Related Questions