I have generic functions in my project that handle each database type, whether it's MySql or Oracle and more by using Statement and JDBC Connection. However when I try using them for supporting Cassandra, it doesn't work so I created a new function just for Cassandra- that connects via Cluster. Is there an option to use the following without getting exceptions and not Cluster?
Connection conn = DriverManager.getConnection("jdbc:cassandra://TheIp:9042", userName, passWord);
Statement stmt = conn.createStatement();
The dependencies I have on my POM Maven are;
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>3.11.0</version>
</dependency>
It is possible to connect via JDBC using the Simba JDBC Driver for Apache Cassandra provided by DataStax for free. You can download the driver and the manual from DataStax Downloads.
However, if you're already developing your app in Java then have a look at the Cassandra Java driver. Here's a sample code which shows how to connect to and read from a Cassandra cluster:
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.*;
public class ConnectDatabase {
public static void main(String[] args) {
// Create the CqlSession object:
try (CqlSession session = CqlSession.builder()
.addContactPoint(new InetSocketAddress("1.2.3.4", 9042))
.withLocalDatacenter("DC1")
.build()) {
// Select the release_version from the system.local table:
ResultSet rs = session.execute("select release_version from system.local");
Row row = rs.one();
//Print the results of the CQL query to the console:
if (row != null) {
System.out.println(row.getString("release_version"));
} else {
System.out.println("An error occurred.");
}
}
System.exit(0);
}
}
Cheers!