lusonnet 2022-06-04 22:22 采纳率: 0%
浏览 55

statement.executeUpdate();抛出异常

在maven项目里面的problemDAO模块中插入操作出现问题,但是删除,修改等操作都没有问题。
可能是int ret = statement.executeUpdate();这里出了问题(?)

public class ProblemDAO {
    /**
     * 插入
     * @param problem
     */
    public void insert(Problem problem){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into oj_table values(null,?,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, problem.getTitle());
            statement.setString(2, problem.getLevel());
            statement.setString(3,problem.getDescription());
            statement.setString(4, problem.getTestCode());
            statement.setString(5, problem.getTemplateCode());

            int ret = statement.executeUpdate();
            if(ret != 1){
                System.out.println("题目新增失败!");
            }else {
                System.out.println("题目新增成功!");
            }
        } catch (SQLException e) {
            //System.out.println("出错了");
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }

    /**
     * 删除
     * @param id
     */
    public void delete(int id){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from oj_table where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            int ret = statement.executeUpdate();
            if(ret != 1){
                System.out.println("删除失败!");
            }else {
                System.out.println("删除成功!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }

    /**
     * 查找题目列表
     * @return
     */
    public List<Problem> selectAll(){
        ArrayList<Problem> problems = new ArrayList<>();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Connection connection = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select id,title,level from oj_table";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();//执行查询
            while(resultSet.next()){
                Problem problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problems.add(problem);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return problems;
    }



    public static void main(String[] args) {

        Problem problem = new Problem();
        ProblemDAO problemDAO = new ProblemDAO();
        problem.setTitle("两数相加");
        problem.setLevel("简单");
        problem.setDescription("2");
        problem.setTemplateCode("2");
        problem.setTestCode("2");
        problemDAO.insert(problem);
        System.out.println("插入成功");

    }
}

运行结果及报错内容
java.sql.SQLException: #HY000
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3933)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3869)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2675)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1915)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2136)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2070)
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5187)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2055)
    at dao.ProblemDAO.insert(ProblemDAO.java:35)
    at dao.ProblemDAO.main(ProblemDAO.java:154)
插入成功
Process finished with exit code 0
  • 写回答

1条回答 默认 最新

  • WaitIKnowYou 2022-06-04 22:29
    关注

    数据库中有字段要求不能为空,但是insert插入的时候,该字段没有值
    看看数据库第一个字段是否为not null 如果是自增id 主键
    你可以绕过它写
    比如 String sql = "insert into oj_table(字段2,字段3,字段4,字段5,字段6) values(?,?,?,?,?)";

    img

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 6月4日

悬赏问题

  • ¥15 关于stm32hal库驱动ft6336触摸屏遇到的问题
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥100 关于#audobe audition#的问题,如何解决?
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥15 高通uboot 打印ubi init err 22