Executing statements
While the Cluster
acts as a central place to manage connection-level configuration options, you will need to establish a Session
instance to perform actual work against the cluster. This is done by calling the connect()
method on your Cluster
instance. Here, we connect to the contacts
keyspace:
private Session session; // defined at class level session = cluster.connect("contacts");
Once you have created the Session
, you will be able to execute CQL statements as follows:
String insert = "INSERT INTO contact (id, email) " + "VALUES (" + "bd297650-2885-11e4-8c21-0800200c9a66," + "'[email protected]' " + ");"; session.execute(insert);
You can submit any valid CQL statement to the execute()
method, including schema modifications.
Note
Unless you have a large number of keyspaces, you should create one Session
instance for each keyspace in your application, because it provides connection pooling and controls the node selection policy (it uses a round-robin approach by default...