title: How to bind values to bound statement in a generic way using datastax java driver?
body: <p>I am working with Datastax Java driver to read and write data into Cassandra. I am using datastax java driver 3.1.0 and my cassandra cluster version is 2.0.10.</p>
  
  <p>I have created below two methods to execute my cql query.</p>
  
  <p>I am calling first method when I don't need to set any value in my cql query so it works for cql string like below:</p>
  
  <pre><code>select * from testkeyspace.testtable where row_id=1 // cql-1
  select * from testkeyspace.meta where row_id=1 // cql-2
  select * from testkeyspace.proc where row_id=1 // cql-3
  </code></pre>
  
  <p>Now I need to call second method whenever I have query like this:</p>
  
  <pre><code>select * from testkeyspace.testtabletwo where row_id=1 and active=? // cql-4
  select * from testkeyspace.storage where topic=? and parts=? // cql-5, in this part is Integer and topic is String.
  </code></pre>
  
  <p>So my question is how can I make my second method more generic so that if I need to set n number of values in my cql query using BoundStatement, it should work? Right now I am not sure what I am supposed to do for <code>cql-4</code> and <code>cql-5</code> while calling my second method with values passed to that method? I know I have to set those values using BoundStatement but if I need to set two or three or four value, how can I make that method generic so that I don't need to hardcode anything? Some can be Integer and some can be String.</p>
  
  <p><strong>First Method:-</strong></p>
  
  <pre><code>  public ResultSet executeQuery(final String cql) {
      return executeWithSession(new SessionCallable&lt;ResultSet&gt;() {
        @Override
        public ResultSet executeWithSession(Session session) {
          BoundStatement bs = getStatement(cql);
          bs.setConsistencyLevel(consistencyLevel);
          return session.execute(bs);
        }
      });
    }
  </code></pre>
  
  <p><strong>Second Method:-</strong></p>
  
  <pre><code>  public ResultSet executeQuery(final String cql, final Object... values) {
      return executeWithSession(new SessionCallable&lt;ResultSet&gt;() {
        @Override
        public ResultSet executeWithSession(Session session) {
          BoundStatement bs = getStatement(cql);
          bs.setConsistencyLevel(consistencyLevel);
          // how to set these **values** into my cql query using BoundStatement in a generic way 
          // so that I don't need to hardcode anything for cql-4 and cql-5
          return session.execute(cql, values);
        }
      });
    }
  
    // cache the prepared statement
    private BoundStatement getStatement(final String cql) {
      Session session = getSession();
      PreparedStatement ps = cache.get(cql);
      // no statement cached, create one and cache it now.
      if (ps == null) {
        ps = session.prepare(cql);
        PreparedStatement old = cache.putIfAbsent(cql, ps);
        if (old != null)
          ps = old;
      }
      return ps.bind();
    }
  </code></pre>
  
  <p>Do I have to use <code>BoundStatement</code> here at all? I guess I have to because that's how I can set consistency level?</p>