0

I got part of my answer from Is there any sql-query to check the value is exist or not in db table

In the comment of Paraíso

But I can't copy the return value to a boolean variable. It gives an error as

SEVERE: null<br>
java.sql.SQLException: Invalid operation at current cursor position.

My code:

String sel="select CASE WHEN count(1) > 0 THEN true ELSE false END from ledgerslist where ledger = 'dera'";
PreparedStatement stc=conn.prepareStatement(sel);
ResultSet rsk=stc.executeQuery(); 
Boolean cc=false; 
cc=rsk.getBoolean(1); 
conn.close(); 
System.out.println(cc); 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Arun B R
  • 21
  • 7

1 Answers1

0

Remove the ' characters so you get Boolean value instead of a string:

select CASE WHEN count(1) > 0 THEN TRUE ELSE FALSE END from table where email = 'abc@gmail.com'

See MySQL's boolean literals

The constants TRUE and FALSE evaluate to 1 and 0, respectively. The constant names can be written in any lettercase.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • it shows same error. My code is given here String sel="select CASE WHEN count(1) > 0 THEN true ELSE false END from ledgerslist where ledger = 'dera'"; PreparedStatement stc=conn.prepareStatement(sel); ResultSet rsk=stc.executeQuery(); Boolean cc=false; cc=rsk.getBoolean(1); conn.close(); System.out.println(cc); – Arun B R Aug 19 '18 at 08:40
  • @ArunBR Add `if (rsk.next()) {` before `getBoolean` – Ori Marko Aug 19 '18 at 09:00
  • I used this method and it works fine. – Arun B R Aug 19 '18 at 09:13
  • @ArunBR if it helped you can accept the answer – Ori Marko Aug 19 '18 at 09:14
  • This answer doesn't address the problem of _"java.sql.SQLException: Invalid operation at current cursor position._" – Mark Rotteveel Aug 19 '18 at 11:59