初学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; }
sql语句上写%,写在参数上带入。
params[2].toString+"%"
知道了,谢谢!
补充一个解决办法。查询关键字 preparedstatement like