PreparedStatement报错问题

package com.lian.lesson3;

import com.lian.lesson2.utils.JDBCUtils;

import java.sql.*;

public class TestInsert {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
//        ResultSet resultSet = null;

        try {
            connection = JDBCUtils.getConnection();

            //区别
            //使用问号?占位符代替函数
            String sql = "INSERT INTO student(id, name, age) values (?,?,?)";
            preparedStatement = connection.prepareStatement(sql);//预编译SQL,先写sql,然后不执行

            //手动给参数赋值
            preparedStatement.setInt(1,9);//id
            preparedStatement.setString(2,"tiny");//name
            preparedStatement.setInt(3,14);//age

            //执行
            int i = preparedStatement.executeUpdate(sql);
            if (i > 0){
                System.out.println("insert success");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.release(connection,preparedStatement,null);
        }
    }
}

这是之前学习到的preparedStatement使用代码,但是如果是以上的代码,运行会报错:

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.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1333)
    at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2106)
    at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1243)
    at com.lian.lesson3.TestInsert.main(TestInsert.java:27)

原因在于 int i = preparedStatement.executeUpdate(sql);语句中不需要再额外添加sql参数

修改正确后代码

package com.lian.lesson3;

import com.lian.lesson2.utils.JDBCUtils;

import java.sql.*;

public class TestInsert {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
//        ResultSet resultSet = null;

        try {
            connection = JDBCUtils.getConnection();

            //区别
            //使用问号?占位符代替函数
            String sql = "INSERT INTO student(id, name, age) values (?,?,?)";
            preparedStatement = connection.prepareStatement(sql);//预编译SQL,先写sql,然后不执行

            //手动给参数赋值
            preparedStatement.setInt(1,9);//id
            preparedStatement.setString(2,"tiny");//name
            preparedStatement.setInt(3,14);//age

            //执行
            int i = preparedStatement.executeUpdate();
            if (i > 0){
                System.out.println("insert success");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.release(connection,preparedStatement,null);
        }
    }
}

Original: https://www.cnblogs.com/ancientlian/p/14417165.html
Author: Lian_tiam
Title: PreparedStatement报错问题

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/643481/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球