Java 使用DataStax客户端将参数传递给Cassandra CQL查询

Java 使用DataStax客户端将参数传递给Cassandra CQL查询,java,cassandra,cql,datastax-java-driver,Java,Cassandra,Cql,Datastax Java Driver,我使用datastax作为连接cassandra的客户端。我已经通过Java成功连接到cassandra cluster/keyspace/column系列。我正在尝试,对cassandra column family thriugh java发出一些查询。对我来说,它适用于简单的查询,比如 ResultSet results = session.execute("select * from demodb.customer where id = 1"); 现在我想从用户那里获取id参数,并将其

我使用datastax作为连接cassandra的客户端。我已经通过Java成功连接到cassandra cluster/keyspace/column系列。我正在尝试,对cassandra column family thriugh java发出一些查询。对我来说,它适用于简单的查询,比如

ResultSet results = session.execute("select * from demodb.customer where id = 1");
现在我想从用户那里获取id参数,并将其传递给session.execute()语句。
我该怎么做呢?

您需要创建一份准备好的声明。然后需要将该语句与从用户处获得的ID值绑定。然后可以执行绑定语句

下面是一个使用准备好的语句插入图像数据的代码示例

PreparedStatement statement = getSession().prepare(
                               "INSERT INTO pixelstore.image " +
                               "(image_name, " +
                               " upload_time, " + 
                               " upload_by, " + 
                               " file_type, " + 
                               " file_size" +
                               ") VALUES (?, ?, ?, ?, ?);"); 

// create the bound statement and initialise it with your prepared statement
BoundStatement boundStatement = new BoundStatement(statement);

session.execute( // this is where the query is executed
  boundStatement.bind( // here you are binding the 'boundStatement'
    "background", TimeUtil.getTimeUUID(),  "lyubent", "png", "130527"));
最近在planet cassandra上发布了两篇博客文章,其中演示了驱动程序的功能,其中包含代码示例,请查看: