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

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
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";

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

Related

IntelliJ IDEA not recognizing SQL dialect

We use IntelliJ IDEA actively and we have our wrappers for work with DB (PostgreSQL). The thing is that when we use placeholders, SQL is stopped being highlighted.
"select * from " + schema + ".users where id = " + id + ";";
This code is not recognised as SQL, so highlighting doesn't work.
I agree with Jesper, you should try using preparedStatements, not only would your Statements get more secure, also the problem which you are currently having should be solved by it.

Column not found in Left Join [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
"SELECT `articles`.`name`, `articles`.`description`,`articles`.`startPrice`,`articles`.`creationDate`,"
+ "`bids`.`date`, `bids`.`price`,`bids`.`userId`"
+ " FROM `articles` LEFT JOIN `bids` ON `articles`.`id` = `bids`.`articleId`"
+ " WHERE 1"
java.sql.SQLException: Column '`articles`.`id`' not found.
bids table has not records, but just wanted to try that, since I understand that the Left Join shows all the records in the table on the left although the right table has no records.
I have changed Left Join by Inner Join and the query runs without errors
with result of zero records (as expected).
MySql version is 5.7.18
articles table:
Some Test:
Example of a query that work Ok without errors:
"select `articles`.`id`, `articles`.`name`, `articles`.`description`,`articles`.`startPrice`,`articles`.`creationDate`," +
"`bids`.`date`, `bids`.`price`,`bids`.`userId`"
+ " FROM `articles` INNER JOIN `bids` ON `articles`.`id` = `bids`.`articleId` "
+ " WHERE 1"
I also try to restart my MySql server and the same thing happens
I am using java and JDBC with
statement.executeQuery()
I do not know why but it was fixed using the Java Statement class instead of PreparedStatement to execute query.
Try this:
"SELECT `articles`.`name`, `articles`.`description`,`articles`.`startPrice`,`articles`.`creationDate`,"
+ "`bids`.`date`, `bids`.`price`,`bids`.`userId`"
+ " FROM `articles` LEFT JOIN `bids` ON `bids`.`articleId` = `articles`.`id`"
+ " WHERE 1"

Hibernate named query issue

I have below name query
#NamedQuery(name="ScInstantTrack.getCustomerDetails",
query="select b.cardDetail.mstCustomer.customerId, last_day(b.endDate), " +
"LISTAGG(b.txnId,'|') WITHIN GROUP (ORDER BY b.endDate), " +
"count(b.txnId), sum(b.amount), sum(b.balanceAmt), sum(b.redemptionAmt) " +
"from ScInstantTrack b " +
"where b.cardNo = b.cardDetail.cardBarcode " +
"AND b.cardDetail.mstCustomer.customerId = :customerId " +
"and b.startDate <= trunc(:todayDate) " +
"and b.endDate >= trunc(:todayDate) " +
"and b.cardDetail.mstStatus.statusId = 3003 group by b.cardDetail.mstCustomer.customerId, last_day(b.endDate)")
When I am executing this query then getting below error :
unexpected token: WITHIN
I am using Oracle Database.
Why I am getting this error? How to solve this issue?
Try to use #NamedNativeQuery instead of #NamedQuery.
Also check this explanation of difference between them.
Basically you are using expressions that are exclusive in Oracle DB. In other words - you want to execute native query (query in native for Oracle DB language). Named queries use Java Persistence Query Language (HQL i.e.).
The error happen because LISTAGG is an oracle specific function.
That function is not avaliable in HQL and there is nothing you can use instead for HQL.
In order to get the result you have to use a SQLQuery wich perform native SQL queryes. This way You have to implement a version of thw query for each database, but it will work.

Error in SQL statement for Derby database

I'm havin an error while trying to execute this piece of sql code... The error I'm getting is:
Encountered "(" at line 1, column 45
The piece of code is:
ALTER TABLE APP.RESPOSTAS ADD coluna" + (numColumns + 1) + " INTEGER(1) AFTER coluna" + numColumns;
Can anyone help me?
Thank you all!
According to The derby details for INTEGER you don't need the (1) in there.
ALTER TABLE APP.RESPOSTAS ADD coluna" + (numColumns + 1) + " INTEGER
should work?
edited to remove the AFTER section as it doesn't look like derby supports that either.
here are the derby details for ALTER TABLE

Issue with a JPA query

I am trying to execute the following JPA query:
public static final String UPDATE_INVENTORY_CUSTOMER_FOR_AMS_MAPPING = "UPDATE Inventory inventory SET"
+ " inventory.customer.id = :" + DataAccessConstants.PARAM_CUSTOMER_ID
+ " ,inventory.lastUpdateUserId = :" + DataAccessConstants.PARAM_USER_ID
+ " where inventory.amsConsignorName = :" + DataAccessConstants.PARAM_AMS_CONSIGNOR_NAME
+ " and inventory.amsConsignorOrgCd = :" + DataAccessConstants.PARAM_AMS_CONSIGNOR_ORG_CD
+ " and inventory.amsConsignorTypeName = :" + DataAccessConstants.PARAM_AMS_CONSIGNOR_TYPE
+ " and inventory.status.code in (:" + DataAccessConstants.PARAM_STATUS + ")";
but it is seeing the following:
update ATL_INVENTORY, set CONSIGNOR_ID=?, LAST_UPDATE_USER_ID=? where AMS_CONSIGNOR_NAME=? and AMS_CONSIGNOR_ORG_CD=? and AMS_CONSIGNOR_TYPE_NAME=? and (CODE in (? , ? , ? , ?))
Any ideal as to why there is a comma after the table name?
Solution
I had to change the original query to the following:
update Inventory inv set "
+ "inv.customer.id = :" + DataAccessConstants.PARAM_CUSTOMER_ID + " "
+ "where inv.amsConsignorName =:" + DataAccessConstants.PARAM_AMS_CONSIGNOR_NAME + " "
+ "and inv.amsConsignorOrgCd =:" + DataAccessConstants.PARAM_AMS_CONSIGNOR_ORG_CD + " "
+ "and inv.amsConsignorTypeName =:" + DataAccessConstants.PARAM_AMS_CONSIGNOR_TYPE + " "
+ "and exists(select 1 from Code code where inv.status = code and code.code in (:" + DataAccessConstants.PARAM_STATUS + "))
Which then produced this:
update ATL_INVENTORY set CONSIGNOR_ID=? where AMS_CONSIGNOR_NAME=? and AMS_CONSIGNOR_ORG_CD=? and AMS_CONSIGNOR_TYPE_NAME=? and (exists (select 1 from ATL_CODE code1_ where ATL_INVENTORY.STATUS=CODE_ID and (code1_.CODE in (? , ? , ? , ?))))
Based on a clarification located here: Incorrect SQL generated for JPA QL Update statement involving multiple entities
Your query is code as UPDATE Inventory inventory SET, but the generated SQL says update ATL_INVENTORY, set. Why is the literal SQL string not what you coded? When I encounter mysteries like this, they're usually caused by assuming that one thing is being done when in fact another is in play.
This suggests that the SQL you coded isn't being used to generate that SQL the way you're assuming. See where else this query might be coming from. I'd bet that the real source has a misplaced comma in it.
Which JPA implementation are you using? If I'm incorrect about a bad assumption, it says that there's a bug in the implementation. Have you used it before? Have you had success with UPDATE? If yes, it's definitely buried somewhere in your code base.
You have an interface with a bunch of constants in it. Personally, I don't care for a design like that. It's an anti-pattern with a name.
A bug in the JPA provider is very unlikely in my opinion so, as #duffymo said, are you sure you're using the right constant, that the code or maybe dependencies are up-to-date? I'd dig in that direction.
That being said, I really wonder why you're not using named queries (that are most of time pre-compiled by the persistence implementation at deployment time), either in the Java code or in meta-data mapping files (the fun part is that people didn't find having EJB-QL queries externalized in XML very manageable in EJB 2.x, hence the #NamedQuery annotation of JPA).

Categories