Search code examples
cassandraprepared-statementdatastax-java-driver

Prepared statements vs Bound statements in Cassandra?


I am wondering what is the advantage of using BoundStatement over PreparedStatement?

PreparedStatement statement = session.prepare(
                      "INSERT INTO simplex.songs " +
                      "(id, title, album, artist) " +
                      "VALUES (?, ?, ?, ?);");

BoundStatement boundStatement = new BoundStatement(statement);
            session.execute(boundStatement.bind(
                  UUID.fromString("756716f7-2e54-4715-9f00-91debea6cf50"),
                  "La Petite Tonkinoise",
                  "Bye Bye Blackbird",
                  "Joséphine Baker");

The simplest way would be:

PreparedStatement ps = session.prepare(
                      "INSERT INTO simplex.songs " +
                      "(id, title, album, artist, tags) " +
                      "VALUES (?, ?, ?, ?, ?);");
ps.bind(UUID.fromString("756716f7-2e54-4715-9f00-91debea6cf50"),
                      "La Petite Tonkinoise",
                      "Bye Bye Blackbird",
                      "Joséphine Baker");

As you can see, I can bind data to preparedStatement without boundStatements. Where are boundStatement useful?


Solution

  • No advantage: a BoundStatement is nothing more than a PreparedStatement with variables bounded. The bind() method of a PreparedStatements, in fact, returns a BoundStatement.