Error in SQL statement for Derby database - java

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

Related

Postgres Sql : Alter table

I am fetching all the records from the Excel sheet and adding columns into the database according to the Excel header row. After fetching I alter the table and adding columns into the newly created table and it works perfectly fine. but when I add one more column into the excel file and running the query again it supposed to add the column which I have created in the excel file but instead, it gives me an error of Column already exists
The query which I m running is
"ALTER TABLE " + name + " ADD " + cellValue + " varchar(5000)"
Thanks in advance..
If you want to create only columns that don't exist write your query like this
"ALTER TABLE " + name + " ADD if not exists " + cellValue + " varchar(5000)"

Running IBM Db2 DDL statements in JDBC

I need to alter a Db2 column using JDBC. The column may change its name and/or its type. In Db2 these two actions are done in two steps, the first ALTER TABLE to change the name, and the second ALTER TABLE to change the type.
For example:
ALTER TABLE T1 RENAME COLUMN C1 TO C2;
ALTER TABLE T1 ALTER COLUMN C2 SET DATA TYPE decimal(4,0);
See below the code, the first statement is executed but the second always throws an exception.
String sql = "ALTER TABLE " + tableName + " RENAME COLUMN " +
originalName + " TO " + name;
PreparedStatement ps1 = conn.prepareStatement(sql);
ps1.executeUpdate();
sql = "ALTER TABLE " + tableName + " ALTER COLUMN " + name +
" SET DATA TYPE decimal(" + sc.getLength() + "," + sc.getDec() + ")";
PreparedStatement ps2 = conn.prepareStatement(sql);
ps2.executeUpdate();
The exception is:
The operation was not performed because the table is in an invalid
state for the operation. Table name: "DB.T1".
Reason code: "23".. SQLCODE=-20054, SQLSTATE=55019, DRIVER=4.27.25
What is the meaning of a table in an "invalid state"? Why is the table in this state? What's wrong with this code?
Always give your Db2-server platform (z/os, linux/unix/windows, i series) and Db2-server version when asking for Db2-help, because the answer can depend on these facts.
The exception SQL20054N reason 23, means that the table has reached a limit on the number of alterations and before continuing, the table need to be reorganized with a REORG command. The documentation for the error is here. The REORG command will put the table back into a normal state. Normally a DBA would consider running RUNSTATS command following the REORG to ensure that table statistics are refreshed following the alterations.
Db2-LUW allows a small number of table changes (often 3) before forcing a reorg for certain kinds of alterations. Previous alterations to this table might have been performed by others, in different transactions , without getting this exception. Schema-evolution tools should detect this state and recover from it.
This is a normal situation, and the recovery is to run the REORG command.
You can either ask your DBA to do reorg for you, or you can (if your authid has the correct permissions) from jdbc call a stored procedure admin_cmd() to perform the command for you, or just use the Db2 command line interface reorg table db.t1 inplace for example . The documentation for admin_cmd is here, and if you do not understand the REORG details, ask your DBA for help.

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

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

What's wrong with trying to insert this dateTime from my Java application?

For this Java code:
stmt.addBatch(
"INSERT INTO Bills (BillDateTime, Table, Item, NoAttended, Service, Payment, Total) " +
"VALUES('" + billDateTime + "', " + Integer.parseInt(createTableNumberOutput.toString()) + ", '" + null + "', '"
+ Integer.parseInt(createGuestNumberOutput.toString()) + "', " + "5" + ", '" +
createPaymentTypeOutput.toString() + "', '" + "')");
I get the following error:
java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Table, Item, NoAttended, Service, Payment, Total) VALUES('2012-03-26 11:15:8', 1' at line 1
The issue is not apparent to me, as MySql requires the format 'YYYY-MM-DD HH:MM:SS' for dateTime, which I have, right?
Table is reserved keyword in mysql . use backticks(`) around it.
Like below:
stmt.addBatch("INSERT INTO Bills (BillDateTime,`Table`, Item,NoAttended,Service,Payment,Total..........")
Even when you fix the reserved keyword table issue, it still won't work. You are trying the insert date.toString() in the date field. (when concatenated, toString() gets called all non-string objects)
Instead, you should use a PreparedStatement and call stm.setDate(..). Get more familiar with the prepared statement, as it is most often the better way to go. (it also has addBatch(), which works in a slightly different way - you set all parameters, then add, then set again.

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