MySql connector j allow user variables - java

Please help me to find out how to allow mysql connector j to define user variables and make this code valid:
Statement s = conn.createStatement();
s.executeQuery ("set #categoryId := (Select CategoryId from categories order by CategoryId desc LIMIT 1);\n" +
"set #categoryId := IF(#categoryId is Null, 1, #categoryId);");
now it throws an exception:
MySQLSyntaxErrorException occured : 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 'set #categoryId := IF(#categoryId is Null, 1, #categoryId)' at line 2
I know that in .net you can define "Allow User Variables = true" option in connection string. How to do it in java?

I found out how to get it worked. Just set datasource property allowMultiQueries=true
jdbc:mysql://localhost:3306/DBS?allowMultiQueries=true

Related

Syntax error with mysql database using JDBC

PreparedStatement posted = con.prepareStatement(
"INSERT INTO userdate (description, UUID) VALUES ('"+ desc + "','" + postuuid + "') ON DUPLICATE KEY UPDATE");
this is the error 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 ''
this is the code I have, does anyone know waht might be wrong with this?
Check on MySQL docs for reference on DUPLICATE KEY UPDATE https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
The keyword "UPDATE" is not the absolute end of the statement. You need to specify the fields/values that will be updated

Why can't I set a variable in a prepared statement in MySQL?

I'm running a script to insert some data in a MySQL database, and it runs properly in the MySQL workbench. However, when I try to run it from Java via the JDBC, I get an error. The script is:
INSERT INTO `pa_record` (`username`, `pa_record_type`, `record_time`) VALUES (?, ?, CURRENT_TIMESTAMP);
SET #record_id := 1;
INSERT INTO `pa_crud` (`pa_record_id`, `table_name`) VALUES (#record_id, ?);
The error I get from the JDBC is
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 'SET #record_id := 1;
INSERT INTO pa_crud (pa_record_id, table_name) VALUES' at line 2
Any ideas?
Many (most?) database access libraries do not allow multiple statements in a single execute; those that do, usually need to have the feature activated with a setting change. It's likely not complaining about the SET, but that you had anything after the end of the first query at all.

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '|'

There are two database connection in project.
1. oracle database
2. mssql database
Database connection is OK. Issue is when data transferred/inserted in one database[oracle] then it is display error and same time data inserted in another database[mssql] successfully.Insert query is fine but there is another query which is generate sequesnce number. There have problem.
This is the query which belongs to oracle database
return jdbcTemplate.queryForObject("SELECT 'AK'||LPAD(adds_seq.NEXTVAL,13, '0') adds_seq_no FROM sys.dual ",String.class);
Error is :
SELECT 'AK'||LPAD(adds_seq.NEXTVAL,13, '0') adds_seq_no FROM sys.dual ",String.class ]; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '|'.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
It is working well before when there is no mssql connection.
Below query is for oracle database. When I tried to change code like this:
String sql = " SELECT 'AK'||LPAD(adds_seq.NEXTVAL,13, '0') adds_seq_no FROM sys.dual";
String adSeqNum = null;
try {
adSeqNum = jdbcTemplate.queryForObject(sql, String.class);
} catch (Exception e) {
e.printStackTrace();
}
return addSeqNum;
then the error is
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL []; ORA-01400: cannot insert NULL into ("ADDSS_HST"."ADDS_SEQ_NO")
1.Can you guide me how to solve this issue?
2.Can you please give me example for how to do separate database connection on one java file?
The "Incorrect syntax near '|'" happens because you are sending the Oracle statement "SELECT 'AK'||LPAD(adds_seq.NEXTVAL,13, '0') adds_seq_no FROM sys.dual " to the MSSQL server connection.
The second error, cannot insert null into "ADDSS_HST"."ADDS_SEQ_NO" I suspect happens because before the posted sql that selects from the sequence, you are inserting records into ADDSS_HST table. Is this correct? If so, I recommend that you put the code that generates adds_seq_no into a trigger like the one below (adjust names for your needs):
CREATE OR REPLACE TRIGGER "APPLICATION_BI_TRG"
BEFORE INSERT ON APPLICATION
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
BEGIN
if :new.application_id is null then
// if no value was given in insert statement for column application_id
SELECT APP_WEB_ID_SEQ.NEXTVAL INTO :NEW.application_id FROM dual;
// select a value from sequence into :NEW.application_id
end if;
END;
/

SQL Invalid Syntax in Java but not in Workbench

Why does this statement work fine in workbench but not in Java.
SET #sqlstmt := IF( #exist <= 0, 'select ''INFO: Key does not exist.''', 'ALTER TABLE `SOMETABLE` DROP FOREIGN KEY `SOMEKEY`');
In Java I get
MySQLSyntaxErrorException: 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 'SET #sqlstmt := IF( #exist <= 0, 'select ''INFO: Key does not exist.''', 'ALTER ' at line 1
It turned out to be the fact that multiple statements were being executed (separated by semicolons). In JDBC MYSQL you need allowMultiQueries=true

MyBatis java and MySql local variables

I'm new to java world. And I have a problem with simple query:
<insert id="create" parameterType="models.entities.CategoryEntity">
set #catId := (select categoryId from Categories limit 1);
insert into Categories(CategoryId, Title, LeftValue, RightValue)
values(#catId, 'Test in', 1,2);
....
</insert>
it simply fails when i trying to run it with mybatis:
PersistenceException occured : ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into Categories(CategoryId, Title, LeftValue, RightValue) values(' at line 2 ### The error may involve Category.create-Inline ### The error occurred while setting parameters ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into Categories(CategoryId, Title, LeftValue, RightValue) values(' at line 2
If I remove this line:
set #catId := (select categoryId from Categories limit 1);
then everything is ok. What am i doing wrong? Is it problem with jdbc or mybatis? How to use mysql #variables with mybatis? Does someone have examples of using MySql local variables with mybatis?
I found out how to get it worked. Just set datasource property allowMultiQueries=true
jdbc:mysql://localhost:3306/DBS?allowMultiQueries=true

Categories

Resources