0

I am currently trying to pull a string variable, tableChoice, from the determineTable method to use it in exactWordMatch.

My code:

public class DynamicQueryHandler {

    static String tableChoice;

    public static void determineTable() {

        String[] choices = { "Customer", "Department", "Employee", "Vehicle", "Works In" };
        String input = (String) JOptionPane.showInputDialog(null, "Choose the table to search from",
                "Dealership Database", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
        if (input == choices[0])
            tableChoice = "CUSTOMER";
        if (input == choices[1])
            tableChoice = "DEPARTMENT";
        if (input == choices[2])
            tableChoice = "EMPLOYEE";
        if (input == choices[3])
            tableChoice = "VEHICLE";
        if (input == choices[4])
            tableChoice = "WORKS_IN";
    }

    public static void exactWordMatch() throws SQLException {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

        String serverName = "server";
        String portNumber = "port";
        String sid = "sid";
        String url = "url" + serverName + ":" + portNumber + ":" + sid;
        String Result = "";
        String Header = "test header";

        Connection connection = DriverManager.getConnection(url, "user", "pass");
        Statement statemet = connection.createStatement();
        System.out.println(tableChoice);
        ResultSet resultSet = statemet.executeQuery("SELECT * FROM " + tableChoice);
        resultSet.close();
        statemet.close();
        connection.close();
    }
}

When I run the program, tableChoice is coming back as "null". I've tried making the determineTable function return the string tableChoice and that didn't work. I made the variable class-wide and that didn't work either. I'm not sure where to go from here. I want it to return the name of my SQL tables, so I can use it for an SQL query.

Changing it to:

public class DynamicQueryHandler {

    static String tableChoice;

    public static void determineTable() {

        String[] choices = { "Customer", "Department", "Employee", "Vehicle", "Works In" };
        String input = (String) JOptionPane.showInputDialog(null, "Choose the table to search from",
                "Dealership Database", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
        if (input.equals(choices[0]))
            tableChoice = "CUSTOMER";
        if (input.equals(choices[1]))
            tableChoice = "DEPARTMENT";
        if (input.equals(choices[2]))
            tableChoice = "EMPLOYEE";
        if (input.equals(choices[3]))
            tableChoice = "VEHICLE";
        if (input.equals(choices[4]))
            tableChoice = "WORKS_IN";
    } 

Still makes tableChoice null when calling it from exactWordMatch()

CsCody
  • 25
  • 7
  • You are not comparing your `String`'s correctly, so `tableChoice` is always `null`. Use `String#equals`. – gonzo Dec 09 '15 at 16:29
  • Compare the string using equals method and i don't about the case you are using in input. For that you can use equalsignorecase() to compare the strings with case insensitivity. – Shriram Dec 09 '15 at 16:31
  • In your if statements instead of input == choices[0]; try input.equals(choices[0]); because that is way to compare strings – The Guest Dec 09 '15 at 16:31
  • I tried it both ways, changing it to if (input.equals(choices[0])) tableChoice.equals("CUSTOMER"); and changing it to just if (input == choices[0]) tableChoice.equals("CUSTOMER"); – CsCody Dec 09 '15 at 16:39
  • Use `.equals()` for the comparison of two `String`s, not the assignment of a `String`. You were assigning your `tableChoice` correctly. Just not doing the comparison correct. `if (input.equals(choices[0])) tableChoice="CUSTOMER";` – gonzo Dec 09 '15 at 16:41
  • I did that, and tableChoice is still null when I try to use it from the exactWordMatch function. – CsCody Dec 09 '15 at 16:45
  • 1
    Try printing `tableChoice` at the end of your `determineTable`. What prints out? With the proper changes, I do not get `tableChoice` to be null. – gonzo Dec 09 '15 at 16:52
  • You are clicking `OK` in your popup correct? Only way I get `null` is if I hit `Cancel`. Which you should also do a check for by the way. – gonzo Dec 09 '15 at 16:54
  • I am clicking ok in my popup. I just tried printing tableChoice at the end of determineTable, and it didn't print anything. Is the code just not going through my if statement? – CsCody Dec 09 '15 at 17:20
  • I figured out what my problem was. I was calling the wrong function from another class. I apologize for the inconvenience. Thank you very much for your help. – CsCody Dec 09 '15 at 17:25

0 Answers0