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);
        }
    }
}


posted @ 2021-02-19 17:25  Lian_tiam  阅读(2642)  评论(1)    收藏  举报