Simple statements

Use SimpleStatement for queries that will be executed only once (or a few times) in your application:

SimpleStatementstatement=newSimpleStatement("SELECT value FROM application_params WHERE name = 'greeting_message'");session.execute(statement);

When you don’t need to customize anything on the SimpleStatement object, there is a convenient shortcut:

session.execute("SELECT value FROM application_params WHERE name = 'greeting_message'");

Each time you execute a simple statement, DSE will parse the query string again; nothing is cached (neither on the
client nor on the server):

If you execute the same query often (or a similar query with different column values), consider a
prepared statement instead.

Using values

Instead of sending a raw query string, you can use bind markers and provide values separately:

by position:

StringparamName=...session.execute("SELECT value FROM application_params WHERE name = ?",paramName);

by name:

// Just a convenience to build a java.util.Map with a one-linerimportcom.google.common.collect.ImmutableMap;StringparamName=...session.execute("SELECT value FROM application_params WHERE name = :n",ImmutableMap.<String,Object>of("n",paramName));

This syntax has a few advantages:

if the values already come from some other part of your code, it looks cleaner than doing the concatenation yourself;

you don’t need to translate the values to their string representation. The driver will sent them alongside the query,
in their serialized binary form.

The number of values must match the query string, and their types must match the database schema. Note that the driver
does not parse query strings, so it cannot perform those checks on the client side; if you make a mistake, the query
will be sent anyway, and the error will be caught by DSE (InvalidQueryException is a server-side error):

Value type inference

Another consequence of not parsing query strings is that the driver has to make a guess on how to serialize values,
based on their Java type (see the default type mappings). This can be tricky, in
particular for numeric types:

The problem here is that the literal 1 has the Java type int. So the driver serializes it as a CQL int (4 bytes),
but the server expects a CQL bigint (8 bytes). The fix is to specify the correct Java type:

session.execute("INSERT INTO bigints (b) VALUES (?)",1L);

In the same vein, strings are always serialized to varchar, so you could have a problem if you target an ascii
column:

DataStax, Titan, and TitanDB are registered trademark of DataStax, Inc.
and its subsidiaries in the United States and/or other countries.

Apache Cassandra, Apache, Tomcat, Lucene, Solr, Hadoop, Spark, TinkerPop,
and Cassandra are trademarks of the Apache
Software Foundation or its subsidiaries in Canada, the United States
and/or other countries