首页 新闻 会员 周边 捐助

JDBC中调用PreparedStatement的用法报错

0
[已解决问题] 解决于 2018-01-12 21:16

初学JDBC,调用PreparedStatement对象的executeUpdate方法,提示 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 '%''',请求前辈支援。以下为代码

    private static int UpdateScore(Object... params) throws Exception {
        int result = 0;

        String sql = "UPDATE student SET score=? WHERE  sex=? and NAME LIKE ''?%'';";
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, Integer.parseInt(params[0].toString()));
        preparedStatement.setString(2, params[1].toString());
        preparedStatement.setString(3, params[2].toString());

        result = preparedStatement.executeUpdate();

        return result;
    }
cxiaoluab的主页 cxiaoluab | 初学一级 | 园豆:26
提问于:2018-01-11 23:40
< >
分享
最佳答案
1

sql语句上写%,写在参数上带入。
params[2].toString+"%"

奖励园豆:5
elice丹 | 菜鸟二级 |园豆:209 | 2018-01-12 07:49

知道了,谢谢!

cxiaoluab | 园豆:26 (初学一级) | 2018-01-12 21:15

补充一个解决办法。查询关键字 preparedstatement like 

cxiaoluab | 园豆:26 (初学一级) | 2018-01-12 21:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册