-1

I have Java code to access and process data in Cassandra. How do I pass a Java variable to Cassandra CQL query written in Java. Here is the code:

My code goes like this:

itemname="Item01";

com.datastax.driver.core.PreparedStatement result = 
session.execute("select itemname from demodb.retail_transaction where itemnamw = itemname;");

But it gives the following error:

no viable alternative at input ';'
mauris
  • 42,982
  • 15
  • 99
  • 131
Saurabh Deshpande
  • 1,153
  • 4
  • 18
  • 35

1 Answers1

0

i think you should bind your variables to the prepared statement like this:

    itemname="Item01";
    itemtype="type01";
    com.datastax.driver.core.PreparedStatement result = 
    session.execute("select itemname from demodb.retail_transaction where itemnamw = ? and 
                    itemtype =?;");
    BoundStatement boundStatement = new BoundStatement(statement);
    ResultSet results = session.execute(boundStatement.bind(
              itemname,itemtype));
Sayed Jalil Hassan
  • 2,535
  • 7
  • 30
  • 42