I am trying to do JUnit tests with H2, and my APP is configurated to work with PostgreSQL, and with ACL's Spring Boot.
So I have to add into the app the next two lines:
`mutableAclService.setClassIdentityQuery("select currval(pg_get_serial_sequence('acl_class', 'id'))");
mutableAclService.setSidIdentityQuery("select currval(pg_get_serial_sequence('acl_sid', 'id'))");`
to create the AclService.
I have in the application.properties the configuration (spring.datasource.url=jdbc:h2:~/test_db;MODE=PostgreSQL)
But it always returns me:
org.springframework.jdbc.UncategorizedSQLException: StatementCallback;
uncategorized SQLException for SQL [select
currval(pg_get_serial_sequence('acl_sid', 'id'))]; SQL state [90022];
error code [90022]; Function "PG_GET_SERIAL_SEQUENCE" not found; SQL
statement: select currval(pg_get_serial_sequence('acl_sid', 'id'))
[90022-197]; nested exception is org.h2.jdbc.JdbcSQLException:
Function "PG_GET_SERIAL_SEQUENCE" not found; SQL statement ...
Anyone knows how to fix it?
Replacing the
"select currval(pg_get_serial_sequence('acl_class', 'id'))"
for a SQL standard query
"SELECT CURRVAL(SELECT SEQUENCE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'acl_class' AND COLUMN_NAME = 'id')"
it works!
Related
I am facing exception while executing following query from jdbcTemplate
update TEMP_BRD_STATS SET STATS=null WHERE BRDC_STAT_ID=?
and following is the exception
org.springframework.jdbc.UncategorizedSQLException:
PreparedStatementCallback; uncategorized SQLException for SQL [update
TEMP_BRD_STATS SET STATS=null WHERE BRDC_STAT_ID=?]; SQL state [null];
error code [0]; ORA-00900: invalid SQL statement
Where TEMP_BRD_STATS is my table name STATS and BRDC_STAT_ID are column in my table
Note : - null is allowed for STATS column
However I can successfully execute the query in sql developer by providing valid BRDC_STAT_ID column value.
e.g.
update TEMP_BRD_STATS SET STATS=null WHERE BRDC_STAT_ID=523;
Edit: Following is the JAVA code
result = this.jdbcTemplate.query("update TEMP_BRD_STATS SET STATS=null WHERE BRDC_STAT_ID=?", new Object[] {523}, new QueryResultSetExtractor());
Column value is getting changed after executing above code although exception is thrown.
Suppose my column STATS is having 'SUCCESS' value and after executing above java code value is getting changed to null but still it is throwing exception. kind of weird scenario.
The parameter ? evidently is not filled in. Maybe some parameter like:
new Object[] { brdStatId }
In general JDBC it could have been using not the PreparedStatement.executeUpdate() but the base class version Statement.executeUpdate(String sql).
After code added in question:
Ah, query (SELECT) and update (INSERT/UPDATE) mixed up:
this.jdbcTemplate.update("update TEMP_BRD_STATS SET STATS=null WHERE BRDC_STAT_ID=?", 523);
Hibernate generating wrong sql(MySQL) query with syntax error.
given this HQL query :
"update GpClientContacter set id = :newKey where id = :Key"
Hibernate executes this sql query:
Hibernate: update GP_CLIENT_CONTACTER set CODE_CLIENT, NOM_RS=(?, ?) where (CODE_CLIENT, NOM_RS)=(?, ?)
Here is exception message:
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 ' NOM_RS=(15, 'test1') where (CODE_CLIENT, NOM_RS)=(15, 'test5')' at line 1
.
.
org.hibernate.exception.SQLGrammarException: could not execute statement
id is an EmbeddedId that has the codeClient and nomRs fields. :key and :newKey are instances of the EmbeddedId Type
I'm using mybatis library to execute native sql queries.
I'm using jooq to generate dynamic sql queries for different dialects.
Syntax:
condition = condition.and(DSL.field("START_TIME_").between("2014-10-12")
.and("2014-10-12"));
Error:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT * FROM ACT_HI_PROCINST WHERE (DELETE_REASON_ IS NULL AND START_TIME_ BETWEEN CAST(:[*]1 AS VARCHAR) AND CAST(:2 AS VARCHAR))
LIMIT ? OFFSET ? "; expected "NOT, EXISTS, INTERSECTS, SELECT, FROM"; SQL statement:
select *
from ACT_HI_PROCINST
where (DELETE_REASON_ is null and START_TIME_ between cast(:1 as varchar) and cast(:2 as varchar))
I have a preparedStatement for select query in mySQL.
this is what I wrote:
String sQuery = "SELECT Password FROM test WHERE Email = ?";
st = DB.prepareStatement(sQuery);
st.setString(1, email);
ResultSet rs = st.executeQuery(sQuery);
but i'm getting an exception from the glassfish server that says:
Severe: 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 '?' at line 1
I don't understand what is the problem.. all the samples i saw, use that syntax..
You must call st.executeQuery(), without the query as argument. The query has already been passed to the statement when it was prepared.
See http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeQuery%28%29
st.executeQuery() i.e. no param to executeQuery
I have a sql as below in my java program:
String sql = "Select * from mySchema.myTable where product in (?) and myDate = ?";
I have my query params as:
Object[] params = {"\'abc\',\'pqr\',\'lmn\'",'2013-07-18'};
And I am trying to execute as:
List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, params);
where jdbcTemplate is a org.springframework.jdbc.core.JdbcTemplate object.
However, I am getting error as:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [Select * from mySchema.myTable where product in (?) and myDate = ?]; DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.59.81; nested exception is com.ibm.db2.jcc.am.SqlDataException: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.59.81
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
at org.springframework.jdbc.core.JdbcTemplate.queryForList(JdbcTemplate.java:796)
Further down the stack trace:
Caused by: com.ibm.db2.jcc.am.SqlDataException: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.59.81
at com.ibm.db2.jcc.am.dd.a(dd.java:668)
at com.ibm.db2.jcc.am.dd.a(dd.java:60)
at com.ibm.db2.jcc.am.dd.a(dd.java:127)
at com.ibm.db2.jcc.am.bn.c(bn.java:2546)
at com.ibm.db2.jcc.am.bn.a(bn.java:2053)
at com.ibm.db2.jcc.t4.cb.n(cb.java:802)
at com.ibm.db2.jcc.t4.cb.i(cb.java:259)
at com.ibm.db2.jcc.t4.cb.c(cb.java:54)
at com.ibm.db2.jcc.t4.q.c(q.java:44)
at com.ibm.db2.jcc.t4.rb.j(rb.java:147)
at com.ibm.db2.jcc.am.bn.ib(bn.java:2048)
at com.ibm.db2.jcc.am.cn.b(cn.java:3845)
at com.ibm.db2.jcc.am.cn.b(cn.java:3975)
at com.ibm.db2.jcc.am.cn.bc(cn.java:678)
at com.ibm.db2.jcc.am.cn.executeQuery(cn.java:652)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:643)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
How can I pass a string as a parameter to my sql where my string is of type 'abc','pqr','xyz'
Thanks for reading!
I suggest you use a better ORM like MyBatis.
Also it looks like you're passing a string parameter as a date.
Your solution to pass a list of strings to IN statement won't work either, your
"'abc','cde'" will be treated as a single string in the IN statement since all SQL characters like , are ignored in parameters of parametrized statements (it's the feature that prevents SQL injection).