App下載
首頁javaconnectionJava JDBC - 如何連接到Cassandra

Java JDBC - 如何連接到Cassandra

我們想知道如何連接到Cassandra。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

  public static void main(String[] args) throws Exception {

    Connection con = null;
    Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
    con = DriverManager
        .getConnection("jdbc:cassandra://localhost:9160/Keyspace");

    String query = "SELECT * FROM column_family where gender=f ";

    Statement stmt = con.createStatement();
    ResultSet result = stmt.executeQuery(query);

    while (result.next()) {
      System.out.println(result.getString("user_name"));
      System.out.println(result.getString("gender"));
      System.out.println(result.getString("password"));
    }
    con.close();
  }
}