title: How to create a prepared statement dynamically - and re-use the query
body: <p>I've been trying to create a class that handles queries from different Classes that create different objects, for example.</p>
  
  <pre><code>Class Employees, Class Customers, Class Sales
  </code></pre>
  
  <p>I'd like to pass a SQL query via the constructor derived from JTextField values (to the query class, "Database").</p>
  
  <p>For example, from two different classes:</p>
  
  <pre><code>new Database (SELECT PRODUCT FROM SALES WHERE DATE = YESTERDAY);
  
  new Database (SELECT FULLNAMES FROM CUSTOMER WHERE ADDRESS = NEWYORK);
  </code></pre>
  
  <p>The problem I'm facing is when it comes to creating the following items dynamically (PreparedStatement Parameters):</p>
  
  <pre><code>stmt.setString(2, NEWYORK);
  </code></pre>
  
  <p>so that "sql" at "?" can be populated:</p>
  
  <pre><code>String sql = "SELECT FULLNAMES FROM CUSTOMER WHERE ADDRESS = ?";
  </code></pre>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>stmt.setString(1, NEWYORK);
  stmt.setString(2, FULLNAMES);
  stmt.setString(3, EMPLOYEE);
  </code></pre>
  
  <p>NOTE: The whole point is to reuse the database class.</p>