I've been trying to create a class that handles queries from different Classes that create different objects, for example.
Class Employees, Class Customers, Class Sales
I'd like to pass a SQL query via the constructor derived from JTextField values (to the query class, "Database").
For example, from two different classes:
new Database (SELECT PRODUCT FROM SALES WHERE DATE = YESTERDAY);
new Database (SELECT FULLNAMES FROM CUSTOMER WHERE ADDRESS = NEWYORK);
The problem I'm facing is when it comes to creating the following items dynamically (PreparedStatement Parameters):
stmt.setString(2, NEWYORK);
so that "sql" at "?" can be populated:
String sql = "SELECT FULLNAMES FROM CUSTOMER WHERE ADDRESS = ?";
In my project there could be one statement that passes values to the parameter just as above, or there could be more parameters, meaning more statements, hence the above can't be reused.
Could anyone have ideas on how to generate "stmt.setString(2, NEWYORK);" dynamically so that I could generate it dynamically and as per the number of parameters being passed. So that I could for example have:
stmt.setString(1, NEWYORK);
stmt.setString(2, FULLNAMES);
stmt.setString(3, EMPLOYEE);
NOTE: The whole point is to reuse the database class.