我正在学习如何使用JDBC DAO tutorial创建数据层。 但是我在这一点卡住了:PreparedStatement statement = prepareStatement(connection, SQL_UPDATE, false, values);
为什么prepareStatement用这种方式?PreparedStatement中的这些值/参数是什么意思?
我将不胜感激任何解释和建议。
我已经通过documentation看上去和搜索相关的例子,但还没有发现这样一个结构的任何解释。我熟悉这样的表情,当prepareStatement方法由连接称为对象:
Connection connection = daoFactory.getConnection();
PreparedStatement statement = connection.prepareStatement(SQL);
但我不明白为什么PreparedStatement的就像是在下面的例子中实现:
public void update(User user) throws DAOException {
if (user.getId() == null) {
throw new IllegalArgumentException("User is not created yet, the user ID is null.");
}
Object[] values = {
user.getEmail(),
user.getFirstname(),
user.getLastname(),
toSqlDate(user.getBirthdate()),
user.getId()
};
try (
Connection connection = daoFactory.getConnection();
PreparedStatement statement = prepareStatement(connection, SQL_UPDATE, false, values);
) {
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new DAOException("Updating user failed, no rows affected.");
}
} catch (SQLException e) {
throw new DAOException(e);
}
}
'prepareStatement'是用户自定义的方法吗?请张贴该方法的代码。 –
@JitinKodian我也认为它可能是一些用户定义的方法。但是在[BalusC教程:](http://balusc.omnifaces.org/2008/07/dao-tutorial-data-layer.html)中没有提及它。 –