Cannot get the result by passing the parameter to MyBatis - java

I have a select SQL in MyBatis like this:
SELECT * FROM MYTABLE WHERE id IN (#{ids})
The parameter ids I passed in is correct, which I can get while I'm debugging. And also, I can run this SQL in MySQL with the parameter correctly and get the results.
But nothing is gotten running with Java code.
I printed the SQL of MyBatis and the SQL is like this:
SELECT * FROM MYTABLE WHERE id IN (?)
and the parameters shown in log is correct too('1', '2', '3').
Why can't I get the data by code?
I'd be appreciated if anyone can help.

You need to use <foreach> as described in the documentation.
See this other SO question if you are using annotations.

Related

Spring boot and jpa query #P0

I'm trying to run the following query
#Query(value = "INSERT INTO controllordinitest.dbo.Records(FSId, FSBlob) SELECT NEWID(), BulkColumn FROM OPENROWSET(BULK :path, SINGLE_BLOB) as f;", nativeQuery= true)
#Modifying
void saveFile(#Param(value="path") String path);
but I keep getting the syntax error #P0, I also tried not to use parameters but "?" and still not working, my guess is that the string is not placed under '' and it ends up just next to bulk, but as soon as I place the single quote I get the error that there is no file in :path.
I also tried to wrap the ? with parenthesis but no luck... even tried to change the hibernate dialog but no luck again...
It could be a possible duplicate of this but their solutions just don't work
I think the cause of this problem is that JPA parameters are only allowed inside the WHERE clause of the query.
You can't use the parameter at any place of the created query.
In your case you use them in FROM part FROM OPENROWSET(BULK :path, SINGLE_BLOB)

jOOQ addConditions: in SQL question mark appears instead of the value

I would like to launch simple code:
SelectQuery query = dsl.select(field ("id"), field("title")).from("dict.models").getQuery();
if (modelId > 0) query.addConditions(field("model_id", SQLDataType.INTEGER).equal(modelId));
But infortunately in getSQL() I can only see:
select id, title from dict.models where model_id = ?
Where is a mistake?
Thanks.
Query.getSQL() generates the SQL statement as it would be generated if you let jOOQ execute a PreparedStatement - with bind variables. The bind variables can be extracted in the right order via Query.getBindValues()
If you want to inline all bind values into the generated SQL, you have various options through the jOOQ API (all equivalent):
Using Query.getSQL(ParamType) with ParamType.INLINE
Using dsl.renderInlined(QueryPart)
Using StatementType.STATIC_STATEMENT in your Settings

JDBC query doesn't work when giving NamedParameterJdbcOperation gets date as parameter (in params map)

I try to query my Mysql DB using JDBC.
I use the org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.
The column DATE_FROM in my_table is defined as DATE column (which is also a PK).
A simplified version of a query which works looks like this:
SELECT * FROM my_table WHERE DATE_FROM >='2015-03-01';
But then, I tried to change the the '2015-03-01' to be given as a named parameter. the query looked like this:
SELECT * FROM my_table WHERE DATE_FROM >=:fromDate;
while I invoked the NamedParameterJdbcOperations.update() like this:
map.put("fromTime", '2015-03-01');
namedParameterJdbcOperations.update(sql, map);
Although everything, to my understanding, remains the same, I get:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: ''2015-03-01'' for column 'MEASUREMENT_DATE' at row 1.
Does someone know why?
Did you realize that you named the placeholder in the SQL "fromDate" and in the map, you named it "fromTime"?

Hibernate like query for escape character

I'm using Mysql database. I got stored data for certain columns with value of a\"a. I used following query to select but it failed:
select * from table_name where coloum_name like "%a\"%";
I spend some hours to find a query to select and the following one is working:
select * from table_name where coloum_name like "%a\\\\\\\\""%";
I'm using hibernate in my application, so I used:
criteria.add(Restrictions.ilike("coloum_name","%a\\\\\\\\""%"));
but it's not working. Any possible way to select via criteria??
Check this one. Hope this will help for you.
http://levanhuy.wordpress.com/2009/02/19/providing-an-escape-sequence-for-criteria-queries/

Passing parameters to JasperReports SQLl statement from Java

I'm using JasperReports engine, and one of the reports gets data from database executing SQL statement. Is there a way to pass parameters to that query?
Thanks in advance!
First, create a new parameter in your report. Then insert the parameter in your query, for example:
SELECT name, department FROM employees WHERE employee_id = $P{employeeId}
Make sure your parameter types matches the data type of the columns in your database. Finally, simply pass your parameters to the JasperReports engine. An example would be:
parameters.put("employeeId", Long.valueOf(14309));
JasperRunManager.runReportToPdf(reportFile, parameters, connection);

Categories

Resources