2
 My sample  cassandra table looks like        

id | article_read | last_hours | name
----+-----------------------------------
5  |    [4, 5, 6]  |          5  | shashank
10 | [12, 88, 32]  |          1  |      sam
8  |    [4, 5, 6]  |          8  |     aman
7  |       [5, 6]  |          7  |    ashif
6  |    [4, 5, 6]  |          6  |     amit
9  |    [4, 5, 6]  |          9  |  shekhar

My java code to read data from Cassandra table using cql queries,

     Scanner sc = new Scanner(System.in);
     System.out.println("enter name1 ");
     String name1 = sc.nextLine();      
     System.out.println("enter name2");
     String name2 = sc.nextLine();

     Cluster cluster =    Cluster.builder().addContactPoint("127.0.0.1").build();        
     Session session = cluster.connect("tp");

     PreparedStatement queryStmt = session.prepare("select article_read  from bat where name = ?");        
     ResultSet result1 = session.execute(queryStmt.bind(name1));

     ResultSet result2 = session.execute(queryStmt.bind(name2));
      System.out.println(result1.all());
      System.out.println(result2.all());

       if(result1.equals(result2))
      {    
      System.out.println("100% sentiment ");          
      }                
      else
      {
       System.out.println("no sentiment ");
    }    
}

look at my code ,its running but when i am putting name1,name2 shashank and aman its giving 100 % but when giving shashank and ashif result is again 100% match..

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
s.s
  • 93
  • 2
  • 14

1 Answers1

3

Use PreparedStatement

First prepared the query only once like below :

//Prepared only once. 
PreparedStatement queryStmt = session.prepare("select * from bat where name = ?");

Then you can use it any number of time like below :

ResultSet result1 = session.execute(queryStmt.bind("shashank"));
ResultSet result2 = session.execute(queryStmt.bind("aman"));

Edited

try (Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build(); Session session = cluster.connect("test")) {
    Scanner sc = new Scanner(System.in);
    System.out.println("enter name1 ");
    String name1 = sc.nextLine();
    System.out.println("enter name2");
    String name2 = sc.nextLine();

    PreparedStatement queryStmt = session.prepare("select article_read from bat where name = ?");
    ResultSet result1 = session.execute(queryStmt.bind(name1));
    ResultSet result2 = session.execute(queryStmt.bind(name2));


    if (result1.one().getList("article_read", Integer.class).equals(result2.one().getList("article_read", Integer.class))) {
        System.out.println("100% sentiment ");
    } else {
        System.out.println("no sentiment ");
    }
}
Ashraful Islam
  • 12,470
  • 3
  • 32
  • 53
  • nice answer i would to separate the values using `Scanner scan = new Scanner(System.in); String name1 = scan.nextLine(); String name2 = scan.nextLine();` and use `ResultSet result1 = session.execute(queryStmt.bind(name1)); ResultSet result2 = session.execute(queryStmt.bind(name2));` it can be better and this is what the OP mean by *i want to take 2 names from command line and then see the result..* – Youcef LAIDANI Oct 31 '17 at 08:44
  • @Ashraful Islam you are taking the input (queryStmt.bind("shashank")); here in code my question was to take the names from comand line. – s.s Oct 31 '17 at 08:58
  • @YCF_L thankyou for this..can you explain how to put name1 and name2 in my cql query ? – s.s Oct 31 '17 at 10:03
  • what did you mean cql query i don't get you? – Youcef LAIDANI Oct 31 '17 at 10:06
  • this -> select * from bat where name = 'name1'"; select * from bat where name ='name2'; – s.s Oct 31 '17 at 10:13
  • @s.s you can use concatination `String query1 = "select * from bat where name = " + name1;` but **Note** This is not secure and can cause syntax error or SQL Injection attack to avoid all this the solution of Ashraful Islam is perfect don't use concatenation ok – Youcef LAIDANI Oct 31 '17 at 10:16
  • @YCF_L i have edited the question and added my new code have a look – s.s Oct 31 '17 at 10:37
  • this is nice @s.s is there another problem now? – Youcef LAIDANI Oct 31 '17 at 10:38
  • @YCF_L there must be no match between any names ..so my else statement must run with sop("no sentiment ") :( – s.s Oct 31 '17 at 10:42
  • @s.s this should be another problem, and it seems that the problem in `result1.all() == result2.all()` read https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java – Youcef LAIDANI Oct 31 '17 at 10:44