Peter Penzov Peter Penzov - 5 months ago 17
Java Question

Call SQL query from Java code

I executed this SQL query in SQL developer successfully:

SELECT
COUNT(*) "All",
COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) "Critical",
COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) "Info",
COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) "Warning",
COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) "Error"
FROM
EVENT;


But when I try to execute the query this way i get error

dbRequest.setPreparedStatement("SELECT "
+ " COUNT(*) 'All',"
+ " COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) \"Critical\", "
+ " COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) \"Info\", "
+ " COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) \"Warning\", "
+ " COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) \"Error\" "
+ " FROM "
+ " EVENT");


Error:

SQL exception: Cannot get object 923: ORA-00923: FROM keyword not found where expected


Do you have any idea how i can fix this issue?

Answer

You mixed up quotes in your request :

On line two, you must replace '' by "

dbRequest.setPreparedStatement("SELECT "
+ " COUNT(*) \"All\","
+ " COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) \"Critical\", "
+ " COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) \"Info\", "
+ " COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) \"Warning\", "
+ " COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) \"Error\" "
+ " FROM "
+ " EVENT");
Comments