Templates by BIGtheme NET

Bound Statement Vs Prepared Statement

In this Article, I will show How to work with Cassandra Bound Statement.
How this differ from Prepared Statement.

Few questions come to mind, while using Bound Statement are.

When to Use Bound Statement?

– If the provided statements that map to different tables? Yes

– The goal of the bound statement is to be able to execute a bunch
of different queries that follow some similar query structure,
But not the exact same query concurrently.

Tools Uses :

1) spring-data-cassandra-1.2.1.RELEASE
2) Apache-cassandra-2.1.6
3) eclipse version Luna 4.4.1.
4) Maven 3.3.3
5) JDK 1.6 or above

If you are completely new to Apache Cassandra,
please refer apache-cassandra-quick-start

Prerequisites :

1) Cassandra Server should be up and running.
2) key space should be created with name “devJavaSource”

Steps to be followed :

1) Create a maven project in eclipse.
2) Add cassandra-driver-core 2.1.6 jar file as dependency in pom.xml file.
3) Start Cassandra server.
4) Write a simple java program.
5) Demo

2) Add cassandra-driver-core 2.1.6 jar file as dependency in pom.xml file :

Cassandra java driver cassandra-driver-core 2.1.6 jar is required to run java program,
So add this as dependency in pom.xml file.


3) Start Cassandra server :

Go to your home directory, where cassandra server is installed or extracted.
Command to start Casandra server is,

C:\apache-cassandra-2.1.6\bin> cassandra.bat -f


4) Write a simple java program :

Create a simple maven project and add Cassandra dependency.

Complete pom.xml file is Here,

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





Written two java files,
User.java – Is a simple Value Object, holds one user details.


package com.devjavasource.cassandra.boundStatement;

public class User {
	private String id;
	private String name;
	private String address;
	public User(String id, String address, String name){
		this.id = id;
		this.address = address;
		this.name = name;
	public String getId() {
		return id;
	public void setId(String id) {
		this.id = id;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public String getAddress() {
		return address;
	public void setAddress(String address) {
		this.address = address;

Another java that doing cassandra insert and update the user details in User.


package com.devjavasource.cassandra.boundStatement;

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;

public class App {
	public static void main(String[] args) {

		Cluster cluster = null;
		Session session = null;

		try {
			// Connect to the cluster and keyspace "devjavasource"
			final Cluster.Builder clusterBuilder = Cluster.builder()
					.withCredentials("devjavasource", "devjavasource");
			cluster = clusterBuilder.build();
			session = cluster.connect("devjavasource");

			// database operations insert
			PreparedStatement insertStatement = session.prepare(INSERT_QUERY);

			User user = new User("11104", "USA", "Stuart");
			bindValuesToStatement(insertStatement, session, user);


			// database operations update
			PreparedStatement updateStatement = session.prepare(UPDATE_QUERY);
			bindValuesToStatement(updateStatement, session, user);


			// delete the inserted record
			session.execute(" delete FROM users where id = 11104");

		} catch (final Exception exp) {
		} finally {
			// Close Cluster and Session objects

	private static void bindValuesToStatement(
			final PreparedStatement inPreparedStatement,
			final Session inSession, final User inUser) {
		BoundStatement boundStatement = new BoundStatement(inPreparedStatement);
				inUser.getName(), Integer.parseInt(inUser.getId())));

	private static void getUsersAllDetails(final Session inSession) {
		// Use select to get the users table data
		ResultSet results = inSession.execute("SELECT * FROM users");
		for (Row row : results) {
			System.out.format("%s %d %s\n", row.getString("name"),
					row.getInt("id"), row.getString("address"));

	final static String INSERT_QUERY = "INSERT INTO users (address, name, id ) VALUES (?, ?, ?)";
	final static String UPDATE_QUERY = "UPDATE users set address = ?, name=? WHERE id= ?";

Run Maven project :

Select and Run As -> Java Application.

Out Put :

******* database operations insert ********
Stuart 11104 USA
******* database operations update ********
Stuart_Updated 11104 USA_Updated

You can download complete project, Here


*** Venkat – Happy learning ****