Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in

Posted on

Java Create a connection to a cassandra database

import java.sql.*;
import java.util.Properties;

public class CassandraConnection {

    public static void main(String[] args) {
        try {
            // Load the driver

            // Create a Properties object to store the parameters
            Properties props = new Properties();

            // Set the properties
            props.setProperty("user", "cassandra");
            props.setProperty("password", "cassandra");

            // Create a connection to the cluster and keyspace "demo"
            Cluster cluster = Cluster.builder().addContactPoint("").withCredentials("cassandra", "cassandra").build();
            Session session = cluster.connect("demo");

            // Execute a query
            ResultSet rs = session.execute("SELECT * FROM test");

            // Iterate through the results and print them out
            for (Row row : rs) {
                System.out.println(row.getString("key") + ": " + row.getString("value"));

            // Close the connection
        } catch (Exception e) {
Enter fullscreen mode Exit fullscreen mode

Top comments (0)