Using bound statements

The previous tutorial used simple CQL statements to read and write data, but you can also use prepared statements, which only need to be parsed once by the cluster, and then bind values to the variables and execute the bound statement you read or write data to a cluster.

About this task

In the previous tutorial, you added a loadData method which creates a new statement for each INSERT, but you may also use prepared statements and bind new values to the columns each time before execution. Doing this increases performance, especially for repeated queries. You add code to your client for:

Procedure

  1. Add a new method, loadDataUsingBoundStatements, and implement it.
    public void loadDataUsingBoundStatements() { }
  2. Add code to prepare an INSERT statement. You get a prepared statement by calling the prepare method on your session.
    PreparedStatement statement = getSession().prepare(
          "INSERT INTO simplex.songs " +
          "(id, title, album, artist, tags) " +
          "VALUES (?, ?, ?, ?, ?);");
    
  3. Add code to bind values to the prepared statement's variables and execute it. You create a bound statement by calling its constructor and passing in the prepared statement. Use the bind method to bind values and execute the bound statement on the your session..
    BoundStatement boundStatement = new BoundStatement(statement);
    Set<String> tags = new HashSet<String>();
    tags.add("jazz");
    tags.add("2013");
    getSession().execute(boundStatement.bind(
          UUID.fromString("756716f7-2e54-4715-9f00-91dcbea6cf50"),
          "La Petite Tonkinoise'",
          "Bye Bye Blackbird'",
          "Joséphine Baker",
          tags ) );
    
    Note that you cannot pass in string representations of UUIDs or sets as you did in the loadData method.
  4. Add code for the other two insertions into the simplex.songs table.
  5. Add code to create a new bound statement for inserting data into the simplex.playlists table.
    public void loadDataUsingBoundStatements() {
       PreparedStatement statement = getSession().prepare(
             "INSERT INTO simplex.songs " +
             "(id, title, album, artist, tags) " +
             "VALUES (?, ?, ?, ?, ?);");
       BoundStatement boundStatement = new BoundStatement(statement);
       Set<String> tags = new HashSet<String>();
       tags.add("jazz");
       tags.add("2013");
       getSession().execute(boundStatement.bind(
             UUID.fromString("756716f7-2e54-4715-9f00-91dcbea6cf50"),
             "La Petite Tonkinoise'",
             "Bye Bye Blackbird'",
             "Joséphine Baker",
             tags ) );
       statement = getSession().prepare(
             "INSERT INTO simplex.playlists " +
             "(id, song_id, title, album, artist) " +
             "VALUES (?, ?, ?, ?, ?);");
       boundStatement = new BoundStatement(statement);
       getSession().execute(boundStatement.bind(
             UUID.fromString("2cc9ccb7-6221-4ccb-8387-f22b6a1b354d"),
             UUID.fromString("756716f7-2e54-4715-9f00-91dcbea6cf50"),
             "La Petite Tonkinoise",
             "Bye Bye Blackbird",
             "Joséphine Baker") );
    }
    
  6. Replace the call in the class main method to loadData to loadDataUsingBoundStatements.
    public static void main(String[] args) {
       SimpleClient client = new SimpleClient();
       client.connect("127.0.0.1");
       client.createSchema();
       client.loadDataUsingBoundStatements();
       client.querySchema();
       client.updateSchema();
       client.dropSchema();
       client.close();
    }