小工具知识网

当前位置: 首页 > 行业知识 > SQLSyntaxErrorException异常产生原因及解决方案

行业知识

SQLSyntaxErrorException异常产生原因及解决方案

2026-11-26 05:57:43 来源:互联网转载

01 异常发生场景

  • 当���使用PreparedStatement 类进行数据库连接时,弹出错误

    ()
    Cause: 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 '? , studentNo=? , age=? ,  classId=? , phone=? , remark=? , sex=?' at line 1​//以下是代码部分try {//            1.注册驱动            Class.forName("com.mysql.cj.jdbc.Driver");//            2.获取连接对象,连接数据库            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/text012?userSSL=false&serverTimezone=Asia/Shanghai", "root", "1234");//            4.执行sql语句,返回对象            String sql="insert into student (name, studentNo , age ,  classId , phone , remark , sex) values (?,?,?,?,?,?,?);";            stmt=connection.prepareStatement(sql);            stmt.setString(1,name);            stmt.setString(2,studentNo);            stmt.setInt(3,age);            stmt.setInt(4,classId);            stmt.setInt(5,phone);            stmt.setString(6,remark);            stmt.setString(7,sex);​            System.out.println(sql);//            3.获得查询执行者            int num=0;            num=stmt.executeUpdate(sql);            System.out.println(num);//            5.解析对象,获得数据库的数据            if (num!=0){                flag=true;            }​        }catch (Exception e){            e.printStackTrace();        }finally {            if (connection!=null){                connection.close();            }            if (rs!=null){                rs.close();            }            if (stmt!=null){                stmt.close();            }    

    02 异常的产生原因

    • num=stmt.executeUpdate(sql);部分不需要再次传入sql

    • executeUpdate:执行数据库的更新、插入和删除操作 ,返回改变记录的行数,但是使用PreparedStatement 类对传入数据进行加密时,在执行到这个语句的时候数据库系统会对()中sql 的语句进行预编译的处理

      ()

      03 解决方式

      • 放弃使用PreparedStatement 类,或者更简单的

        • num=stmt.executeUpdate();部分不传入字符串

上一篇:VB cls入门指南,快速掌握VB编程基础实战秘籍

下一篇:关于域名怎么开通https的信息