检索“select ... limit ?,?”的元数据时出现 MySQLSyntaxErrorException准备好的查询

发布于 2024-11-09 20:41:23 字数 1778 浏览 4 评论 0原文

我试图在使用“limit”子句中的参数执行查询后从准备好的语句中获取元数据:

PreparedStatement ps = conn.prepareStatement("select * from tbl limit ?, ?");
ps.setLong(1, 0);
ps.setLong(2, 10);
ps.execute();
ResultSetMetaData rsmd = ps.getMetaData();

代码在最后一行抛出异常:

Exception in thread "main" 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
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.Util.getInstance(Util.java:382)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362)
at com.mysql.jdbc.PreparedStatement.getMetaData(PreparedStatement.java:2882)
at ...... <my classes>

当我跳过检索 ResultSetMetaData 时,代码工作得很好。 我在互联网和错误数据库中找不到任何提及错误的信息。

我尝试过以下版本的连接器/J:5.1.14、5.1.12、5.1.9(Maven 依赖项)。

服务器版本是 5.0.77

有人可以帮助我吗?

I'm trying to get metadata from prepared statement after executing query with parameters in "limit" clause:

PreparedStatement ps = conn.prepareStatement("select * from tbl limit ?, ?");
ps.setLong(1, 0);
ps.setLong(2, 10);
ps.execute();
ResultSetMetaData rsmd = ps.getMetaData();

code throws exception in last line:

Exception in thread "main" 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
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.Util.getInstance(Util.java:382)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362)
at com.mysql.jdbc.PreparedStatement.getMetaData(PreparedStatement.java:2882)
at ...... <my classes>

When I'm skipping retrieving ResultSetMetaData, code works just fine.
I can't find in internet and bug database any mentions for a bug.

I've tried folowing versions of connector/J: 5.1.14, 5.1.12, 5.1.9 (Maven dependency).

Server version is 5.0.77

Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-11-16 20:41:23

这是因为当您执行“setLong()”时准备好的语句如何分配参数。

当您在准备好的语句中使用设置器时,它将参数括在单引号中。

我不确定为什么“executeQuery”有效。
如果直接执行以下查询,您将看到类似的异常。

"select * from tbl limit '0', '10'"

而是使用以下内容。

int startLimit = 0;
int endLimit = 10;
PreparedStatement ps = conn.prepareStatement("select * from tbl limit " + startLimit + " , " + endLimit);
ps.execute();
// you can use "Statement" if you dont have any more parameters

This is because of how prepared statement assigns parameters when you do the "setLong()".

When you use the setters in prepared statement, it encloses the parameters in single quotes .

I am not sure why the "executeQuery" works.
You will see a similar exception if you directly execute the following query.

"select * from tbl limit '0', '10'"

Instead use the following.

int startLimit = 0;
int endLimit = 10;
PreparedStatement ps = conn.prepareStatement("select * from tbl limit " + startLimit + " , " + endLimit);
ps.execute();
// you can use "Statement" if you dont have any more parameters
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文