views:

40

answers:

2

Dear All,

I have one query that returns string value, my query is

select case when (select count (distinct style_packing_spec_id) from packing_spec_uses_pack p,style_pack sp where sp.style_pack_id=p.style_pack_id and sp.style_id=1701) != (select count (distinct style_packing_spec_id) from style_packing_spec where style_id=1701) then 'DonotProceed' else 'Proceed' end "; It gives result as below

 ?case?
----------
 DonotProceed

how to execute this is in java, how to handle this result set,I tried like this

String sql="query";
ResultSet rs=stmt.executeQuery(sql);

It returns nullpointerexception String str=rs.getString(1);

+1  A: 

I've never had boolean returned from a query (Oracle doesn't do that...), but I assume that it works. You just need to go to the first row of the result set, and the column index starts with 1 (not 0).

ResultSet rs=stmt.executeQuery(sql);
try{
  rs.next();                // move to the first row
  return rs.getBoolean(1);  // first column is 1
}
finally{
  rs.close();
}

Update:

It shows type mismatch error cannot convert from boolean to Boolean

Are you using Java 1.4 ? For Java5 and above, this should be taken care of by auto-boxing. But you can also fix it by using a primitive boolean variable to match what JDBC returns:

boolean str = rs.getBoolean(1); // I question the variable name, though
Thilo
Pls kindly note my edited query it shows null pointer exception
Mohan
java version "1.4.2_18" my java version
Mohan
Thilo,with the help of your above code i did it correctly. thanks a lot
Mohan
A: 

I'm suspecting ...

Is this SQL query connect?

Upul
I am using postgresql
Mohan
Pls kindly note my edited query it shows null pointer exception
Mohan
did you mean `correct`?
st0le
no give me right code to handle the return value from the query
Mohan