check the manual that corresponds to your MySQL server version for the right syntax to use near ‘?‘

文章讲述了在Java中使用PreparedStatement预编译SQL语句时遇到的错误,具体为SQLSyntaxErrorException,原因是executeQuery()方法中重复传入了SQL语句。解决方案是去除executeQuery()中的SQL字符串,正确方式是直接调用executeQuery()执行预编译后的语句。
摘要由CSDN通过智能技术生成
 @Test
    public void prepare2() throws Exception {
//        用prepareStatement预编译的方式插入数据
//        准备配置信息
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&serverTimezone=UTC";
        String username = "root";
        String password = "root";

//     JDBC必备六部曲
//        1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

//        2.连接数据库,代表数据库
        Connection connection = DriverManager.getConnection(url, username, password);
//        3.编写sql
        String sql = "select * from users where name=?";

//        4.先预编译sql:CRUD
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setObject(1,"Ocean");

//        5.执行sql查询

        ResultSet rs = preparedStatement.executeQuery(sql);

        if (rs.next()){
            System.out.println(rs.getString("email"));
        }

//        6.关闭连接:先开的后关,后开的先关

        preparedStatement.close();
        connection.close();


    }

错误提示:java.sql.SQLSyntaxErrorException: 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 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1200)
at com.Alibaba.test.TestJDBC03.prepare2(TestJDBC03.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Process finished with exit code -1

String sql = “select * from users where name=?”;

预编译sql里带?就报错
错误提示:java.sql.SQLSyntaxErrorException: 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
java.sql.SQLSyntaxErrorException:您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册java.sql.SQLSyntaxErrorException:您的SQL语法有错误;请查看MySQL服务器版本对应的手册,以了解’?第1行
MySQL服务器版本对应的手册是啥??

踩坑经历:
在有道词典翻译后,“你的SQL语法有错误;检查手册对应的MySQL服务器版本的正确语法附近’?'在第1行”。这句话的意思是在?附近的SQL语句有错误。当我去看整个SQL语句时,发现SQL语句并没有写错。然后我就觉得应该是PreparedStatement这个类的预编译或编译的方法那出错了,于是我去看了.prepareStatement()和.executeQuery()方法,发现.executeQuery()传入了sql语句,没想到自己还遇到了这种低级的错误。。。
解决方法:
将ppstm.executeQuery(sql);中的sql去掉,改成ppstm.executeQuery();

作者:Windows_XP_XP
链接:https://www.jianshu.com/p/db58eb86cb88
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

预编译的时候sql语句已经放进去了,
PreparedStatement preparedStatement = connection.prepareStatement(sql);
放完参数以后,
preparedStatement.setObject(1,“Ocean”);
只用执行就好了,不用再传sql了
ResultSet rs = preparedStatement.executeQuery(sql); ×
ResultSet rs = preparedStatement.executeQuery(); √

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值