我正在使用 datastax 作为连接到 cassandra 的客户端。我已经通过 Java 成功连接到 cassandra cluster/keyspace/column family。我正在尝试,通过 java 对 cassandra 列族进行一些查询。对我来说,它适用于像
这样的简单查询ResultSet results = session.execute("select * from demodb.customer where id = 1");
现在我想从用户那里获取 id 参数并将其传递给 session.execute(); 语句。 我该怎么办?
最佳答案
这是一个使用准备好的语句插入有关图像的数据的代码示例。
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 的博客文章演示了驱动程序可以做什么,它们包含代码示例,请查看:
关于java - 使用 DataStax 客户端将参数传递给 Cassandra CQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17419142/