我目前正在尝试从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