从一种方法中提取变量以在另一种方法中使用

时间:2015-12-09 16:26:24

标签: java sql

我目前正在尝试从tableChoice方法中提取字符串变量determineTable,以便在exactWordMatch中使用它。

我的代码:

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();
    }
}

当我运行该程序时,tableChoice将返回" null"。我已经尝试让determineTable函数返回字符串tableChoice并且没有用。我在全班制作变量,但也没有。我不确定从哪里开始。 我希望它返回我的SQL表的名称,因此我可以将它用于SQL查询。

将其更改为:

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";
    } 

从exactWordMatch()

调用tableChoice时仍然使其成为null

0 个答案:

没有答案