简体   繁体   English

JDBC4语法错误,但MySQL接受查询

[英]JDBC4 syntax error, but MySQL accepts the query

I've tried running the following query with JDBC4, which leads to a syntax error:我尝试使用 JDBC4 运行以下查询,这会导致语法错误:

SELECT m.* 
FROM schema.table m 
INNER JOIN (
    SELECT :id0 as id, :from0 as date_from, :to0 as date_to 
    UNION ALL SELECT :id1 as id, :from1 as date_from, :to1 as date_to 
) tmp 
ON tmp.id = m.id AND m.date BETWEEN tmp.date_from AND tmp.date_to

Strangely enough, when I run the query via mysql (on the same database server), the query is accepted and run.奇怪的是,当我通过mysql (在同一数据库服务器上)运行查询时,查询被接受并运行。

The error message is even more confusing, as there is no closing ) on line 1.错误消息更加令人困惑,因为第 1 行没有关闭)

 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

The server is running MySQL 5.1.服务器正在运行 MySQL 5.1。

JDBC accepts parameters on very specific locations only. JDBC 仅接受非常特定位置的参数。 Usually in place of scalar values.通常代替标量值。 You can't use parameters for structural items such as columns, tables, or SQL clauses.您不能将参数用于列、表或 SQL 子句等结构项。

If you are in need of this, then you should consider Dynamic SQL or proper SQL Injection.如果您需要这个,那么您应该考虑动态 SQL 或适当的 SQL 注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM