使用CQL jdbc驱动程序时应该是什么连接字符串 [英] What should be the connection string while using CQL jdbc driver

查看:181
本文介绍了使用CQL jdbc驱动程序时应该是什么连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CQL jdbc驱动程序时应该是什么连接字符串?



我能在Java在线上使用CQL JDBC驱动程序找到适当/完整的CQL示例吗?



这里是我通过CLI输入数据后使用的基本测试使用wiki的示例):

  public class CqlJdbcTestBasic {
public static void main(String [] args){
Connection con = null;
try {
Class.forName(org.apache.cassandra.cql.jdbc.CassandraDriver);
con = DriverManager.getConnection(jdbc:cassandra:root / root @ localhost:9160 / MyKeyspace);

String query =SELECT KEY,'first',last FROM User WHERE age = 42;

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

while(result.next()){
System.out.println(result.getString(KEY));
System.out.println(result.getString(first));
System.out.println(result.getString(last));
}

} catch(ClassNotFoundException e){
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
} finally {
if(con!= null){
try {
con.close();
} catch(SQLException e){
// TODO自动生成的catch块
e.printStackTrace();
}
con = null;
}
}
}
}

用户/密码(root / root)似乎是任意的,只是一定要指定Keyspace(MyKeyspace)



注意,在查询字符串中,因为它是一个CQL关键字


What should be the connection string while using CQL jdbc driver?

Will I be able to find a proper/complete example for CQL using CQL JDBC driver in Java online?

解决方案

You'll need the cql jar from the apache site.

Here's the basic test I used after entering data via CLI (using sample from wiki):

public class CqlJdbcTestBasic {
public static void main(String[] args) {
    Connection con = null;
    try {
        Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
        con = DriverManager.getConnection("jdbc:cassandra:root/root@localhost:9160/MyKeyspace");

        String query = "SELECT KEY, 'first', last FROM User WHERE age=42";

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

        while (result.next()) {
            System.out.println(result.getString("KEY"));
            System.out.println(result.getString("first"));
            System.out.println(result.getString("last"));
        }

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            con = null;
        }
    }
}
}

The user/password (root/root) seems arbitrary, just be sure to specify the Keyspace (MyKeyspace)

Note, 'first' is quoted in the query string because it is an CQL keyword

这篇关于使用CQL jdbc驱动程序时应该是什么连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆