检索“select ... limit ?,?”的元数据时出现 MySQLSyntaxErrorException准备好的查询
我试图在使用“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为当您执行“setLong()”时准备好的语句如何分配参数。
当您在准备好的语句中使用设置器时,它将参数括在单引号中。
我不确定为什么“executeQuery”有效。
如果直接执行以下查询,您将看到类似的异常。
而是使用以下内容。
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.
Instead use the following.