Java PreparedStatement中的这些值/参数意味着什么?
我正在学习如何使用JDBC创建一个数据层。 然而,我在这一点上陷入了困境:Java PreparedStatement中的这些值/参数意味着什么?,java,database,jdbc,prepared-statement,dao,Java,Database,Jdbc,Prepared Statement,Dao,我正在学习如何使用JDBC创建一个数据层。 然而,我在这一点上陷入了困境:PreparedStatement=prepareStatement(连接、SQL\u更新、false、值)为什么prepareStatement使用这种方式 如有任何解释和建议,我将不胜感激 我已经浏览并搜索了相关的例子,但没有找到任何关于这种结构的解释。我熟悉这样的表达式,当连接对象调用prepareStatement方法时: 但我不明白为什么PreparedStatement会像下面的示例中那样实现: public
PreparedStatement=prepareStatement(连接、SQL\u更新、false、值)代码>为什么prepareStatement使用这种方式
如有任何解释和建议,我将不胜感激
我已经浏览并搜索了相关的例子,但没有找到任何关于这种结构的解释。我熟悉这样的表达式,当连接对象调用prepareStatement方法时:
但我不明白为什么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);
}
}
本教程中给出了此方法,它是以下内容的一部分:
DaoUtil prepareStatement方法由DaoImpl在try with resources构造中使用。那里的初始化工作方式不允许多个语句,每个资源都必须用一个表达式初始化。该方法将相关的初始化代码分组在一起,以便资源初始化代码可以调用它
同样,PreparedStatement的参数以更具声明性的方式给出,而设置它们的代码隐藏在实用程序方法中。参数设置非常松散,容易出错(没有类型检查,还有其他问题),所显示的内容对于简明的教程很有用,但不是在实际项目中复制的最佳内容
如果参数设置代码抛出SQLException,则PreparedStatement将不会由方法调用返回,也不会关闭。实际上,这通常并不可怕,因为它会发生
只有在将参数与sql中的占位符匹配时出错(这意味着您有更严重的问题)
重要的是要关闭连接和preparedstatement。否则,您可以尝试不同的编写方法,并比较生成的代码。教程中给出了此方法,它是:
DaoUtil prepareStatement方法由DaoImpl在try with resources构造中使用。那里的初始化工作方式不允许多个语句,每个资源都必须用一个表达式初始化。该方法将相关的初始化代码分组在一起,以便资源初始化代码可以调用它
同样,PreparedStatement的参数以更具声明性的方式给出,而设置它们的代码隐藏在实用程序方法中。参数设置非常松散,容易出错(没有类型检查,还有其他问题),所显示的内容对于简明的教程很有用,但不是在实际项目中复制的最佳内容
如果参数设置代码抛出SQLException,则PreparedStatement将不会由方法调用返回,也不会关闭。实际上,这通常并不可怕,因为它会发生
只有在将参数与sql中的占位符匹配时出错(这意味着您有更严重的问题)
重要的是要关闭连接和preparedstatement。否则,您可以尝试不同的编写方法,并比较生成的代码。prepareStatement
是用户定义的方法吗?请发布该方法的代码。@JitinKodian我还认为它可能是某个用户定义的方法。但是在prepareStatement
中没有提到,用户定义的方法对吗?请发布该方法的代码。@JitinKodian我还认为它可能是某个用户定义的方法。但你还没有在这里找到它吗
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);
}
}
public static PreparedStatement prepareStatement
(Connection connection, String sql, boolean returnGeneratedKeys, Object... values)
throws SQLException
{
PreparedStatement statement = connection.prepareStatement(sql,
returnGeneratedKeys ? Statement.RETURN_GENERATED_KEYS : Statement.NO_GENERATED_KEYS);
setValues(statement, values);
return statement;
}