Sunday, August 19, 2018

JDBC - java.sql.SQLException: ORA-00933: SQL command not properly ended

I am trying to execute the below query in Oracle DB through JDBC but its throwing an exception. The exception is:

java.sql.SQLException: ORA-00933: SQL command not properly ended

Please suggest what needs to be changed ?

String questionQuery = "SELECT PCN_SURVEY_DEFINITION.ID, PCN_SURVEY_DEFINITION.NAME, PCN_QUESTIONS.ID, PCN_QUESTIONS.SURVEY_ID, PCN_QUESTIONS.LABEL, "
                + "PCN_QUESTIONS.TYPE, PCN_QUESTIONS.REQUIRED, PCN_QUESTIONS.COMMENTS, PCN_QUESTIONS.DISPLAY_ORDER "
                + "FROM PCN_SURVEY_DEFINITION, PCN_QUESTIONS "
                + "WHERE PCN_SURVEY_DEFINITION.ID = PCN_QUESTIONS.SURVEY_ID "
                + "AND PCN_SURVEY_DEFINITION.NAME=? "
                + "ORDER BY PCN_QUESTIONS.DISPLAY_ORDER ASC"; 

Solved

Correct the condition in WHERE clause and check the quotes(") properly where to start and where to end.

"WHERE PCN_SURVEY_DEFINITION.ID = " + PCN_QUESTIONS.SURVEY_ID + " AND PCN_SURVEY_DEFINITION.NAME=? " + "ORDER BY PCN_QUESTIONS.DISPLAY_ORDER ASC";

What do you do with the question mark? Maybe you meant to put a column name here of the other table? The one before the ORDER BY? Or are you using a prepared statement afterwards? Try removing this condition temporarily for testing purposes: "AND PCN_SURVEY_DEFINITION.NAME=? "

 "WHERE PCN_SURVEY_DEFINITION.ID = PCN_QUESTIONS.SURVEY_ID " + "AND PCN_SURVEY_DEFINITION.NAME=? " + "ORDER BY 

Oracle does not suport question mark "?". For variable binding oracle uses ":name" or ":1"

https://docs.oracle.com/cd/B10501_01/appdev.920/a96584/oci05bnd.htm


I had something similar and had to add the following property in my application.properties file (since I am using Spring Boot), this resolved the issue for me without having to change any of my SQL

spring.jpa.database=oracle

No comments:

Post a Comment