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