洛上言 2023-08-25 15:10 采纳率: 95.4%
浏览 4

框起来的地方为啥报错?

框起来的地方为啥报错?

img

img

完整代码:

public class PSCURDPart extends BaseDao{

    @Test
    public void testInsert() throws ClassNotFoundException, SQLException {
        String sql = "insert into employee(id, salary) values(?, ?);";

        int rows = executeUpdate(sql, 1, 111);
    }
}


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

public class BaseDao {
    public int executeUpdate(String sql, Objects... params) throws Exception {//可变参数必须存在于形参列表的最后一位
        Connection connection = JdbcUtilsV2.getConnection();

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 1; i <= params.length; i++) {
            preparedStatement.setObject(i, params[i]);
        }

        int rows = preparedStatement.executeUpdate();

        preparedStatement.close();
        if (connection.getAutoCommit()) {
            JdbcUtilsV2.freeConnection();
        }

        return rows;
    }
}
  • 写回答

2条回答 默认 最新

  • 家有娇妻张兔兔 Java领域优质创作者 2023-08-25 15:28
    关注

    你参数是object类型的 你传入的是int类型的, 类型转换错误,改成Integer

    评论 编辑记录

报告相同问题?

问题事件

  • 修改了问题 8月25日
  • 创建了问题 8月25日