截断不正确的INTEGER值:Java和MySQL中的'%'

时间:2017-05-05 11:33:02

标签: java mysql sql hibernate

我使用Java和MySQL按名称进行搜索,之前我曾经连接到Oracle并且运行良好,现在我改为迁移到MYSQL并看到错误..

MySQL的:

SELECT  CAR.ID AS ID, 
    CAR.NAME AS NAME, 
    CAT.ID AS CATEGORY_ID, 
    CAT.NAME AS CATEGORY_NAME,
    C.ID AS CITY_ID,
    C.NAME AS CITY_NAME,
FROM    CARS CAR,       
    CATEGORY CAT,
    CITY C
WHERE   LOWER(CAR.NAME) LIKE '%'||LOWER(?)||'%' 
    AND CAR.CATEGORY_ID         = CAT.ID  
    AND D.CITY_ID               = C.ID
ORDER BY CAT.NAME, CAR.NAME

在部署和调试搜索查询时,发生了错误:

记录消息:

SqlExceptionHelper - SQL Error: 1114, SQLState: HY000
SqlExceptionHelper - The table 'C:\Windows\TEMP\#sql730_a_47' is full
HibernateUtils - error at HibernateUtils.getListResult: could not extract    ResultSet
SqlExceptionHelper - SQL Warning Code: 1292, SQLState: HY000
SqlExceptionHelper - Truncated incorrect INTEGER value: '%'
SqlExceptionHelper - SQL Warning Code: 1114, SQLState: HY000
SqlExceptionHelper - The table 'C:\Windows\TEMP\#sql730_a_47' is full
MySQL说:

#1064 - 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 '?)||'%'
    AND CAR.CATEGORY_ID         = CAT.ID at line 50 

我该如何解决这个问题?非常感谢

2 个答案:

答案 0 :(得分:1)

您的问题(最初)标记为MySQL和Oracle。问题本身和错误强烈暗示MySQL是数据库(例如,Oracle不会产生MySQL错误)。

MySQL不使用||进行字符串连接。编写查询的正确方法是:

SELECT CAR.ID AS ID, CAR.NAME AS NAME, CAT.ID AS CATEGORY_ID, 
       CAT.NAME AS CATEGORY_NAME, C.ID AS CITY_ID, C.NAME AS CITY_NAME,
FROM CARS CAR JOIN      
     CATEGORY CAT
     ON CAR.CATEGORY_ID = CAT.ID JOIN
     CITY C
     ON D.CITY_ID = C.ID
WHERE LOWER(CAR.NAME) LIKE CONCAT('%', LOWER(?), '%')
ORDER BY CAT.NAME, CAR.NAME;

另请注意正确使用JOIN语法。

答案 1 :(得分:1)

而不是:

LIKE '%'||LOWER(?)||'%' 

您可以使用:

"...LIKE LOWER('%"+ str.toLowerCase() +"%')"

或者:

Query query = "...LIKE LOWER('%?%')";
query.setParameter(1, str.toLowerCase());