Dark Mode

  • Demonstrate how to perform basic CRUD operations with the unified DataStax Drivers.
  • Enable users to get up and running quickly with the drivers.
  • Connect a driver in the language of your choice to Astra DB.

Walk through a demo application performing basic CRUD operations using the DataStax drivers to connect to Astra DB.The demo application will first insert a row of user data, select that same row back out, update the row, and finally delete the row.

If you want to skip the exercise and run the application with the complete code, go directly to the Full Solution.

To get the most of out your experience with the drivers, refer to Best practices for DataStax drivers.

DataStax provides drivers for multiple programming languages.There are common features across all the drivers with similar capabilities.

**Before you start, install your driver of choice locally**For more in-depth guidance on installing the drivers and connecting to Astra DB, see Connecting to Astra DB databases using DataStax drivers.

Download and install your driver of choice:

  • 1
  • 1
  • 1
  • 1

*Using Gradle with the Java driver*If you prefer to use Gradle for your project with the Java Driver, please see the Minimal Project Structure for Gradle documentation.

Import the necessary classes or modules to use the driver API for this tutorial.Add these lines to the top of your application code:

  • 1
  • 1
  • 1
  • 1

Create a cluster object and session object to connect to your running Astra DB instance.

You will need to provide:

Get your Client ID and Client Secret by creating your application token for your username and password.

In this tutorial, use the default keyspace you created with your first Astra DB instance.In the code examples the keyspace name is demo .

  • 1
  • 1
  • 1
  • 1

A keyspace is a collection of tables.Add code to create the users table.If you are familiar with SQL, the notation will look familiar to you.

  • 1

Execute a simple statement against the database that will create the users table.Be sure to change the name of the keyspace to the one you are using.

  • 1
  • 1
  • 1
  • 1

Now add some simple CRUD operations to interact with your table.Create a method or function to insert one user into the table.In this example, we use positional placeholders in our statement and add values separately.The values are assigned based on the position of placeholders.One advantage of placeholders is that they avoid injection attacks if the values are dynamic.

  • 1
  • 1
  • 1
  • 1

Select the user you inserted in the previous step.

This select query uses named placeholders, an alternative to the positional placeholders used in the insert statement.Using named placeholders has a few advantages over positional placeholders:

  • It is easier to read.If the values 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 send them alongside the query, in their serialized binary form.

Whether you use positional or named placeholders, the number of values must match the number of placeholders in the query string and their types must match the database schema.

  • 1
  • 2
  • 1
  • 1
  • 1

Prepared statements are a good idea if you have to execute the same query more than once.You have to prepare the statement only once, but you can bind values and execute it multiple times.

Why should you use prepared statements?

  • They are faster than just using strings, especially if you are executing the same query over and over.
  • They are parsed once server-side and cached so they can be used again without resending the entire query each time it is executed.
  • They are used for repeated queries in production applications, so using them wherever a repeated query is used is a good habit to learn.
  • They are more secure because they prevent CQL injection attacks. By using prepared statements, you force the user input to be handled as the content of a parameter and not as a part of the statement itself.
  • 1
  • 1
  • 1
  • 1

Like the previous step, you should consider the performance and security of your data and use a prepared statement for your query.

Check out Best Practices for DataStax drivers for more information on using prepared statements with each of the drivers.

  • 1
  • 1
  • 1
  • 1

Provide inputs for the various CRUD operations outlined in the functions that were just created and run the demo application.

  • 1
  • 1
  • 1
  • 1

Full Solution

  • 1
  • 2
  • 1
  • 1
  • 1