首页 > 数据库 >You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

时间:2024-05-01 22:12:51浏览次数:54  
标签:use preparedStatement setString syntax user sql your

在进行增加User对象时采用了PreparedStatement对象,方法executeQuery()和executeUpdate()都是无参方法,所以不要写成prepareStatement.executeQuery(sql)或prepareStatement.executeUpdate(sql)

public int add(User user) {
        Connection connection = JDBCTools.getConnection();
        int result = 0;
        String sql = "INSERT INTO tb_user VALUE(?, ?, ?, ?, ?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //设置占位符
            preparedStatement.setString(1, user.getUserid());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getRealname());
            preparedStatement.setString(4, user.getEmail());
            preparedStatement.setString(5, user.getAdmin());

            //处理结果
            result = preparedStatement.executeUpdate();

            //关闭链接
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

标签:use,preparedStatement,setString,syntax,user,sql,your
From: https://www.cnblogs.com/fenglilai/p/18169711

相关文章