Java Id()=空){ 抛出新的IllegalArgumentException(“用户尚未创建,用户ID为null”); } 对象[]值={ user.getEmail(), user.getFirstname(), user.getLastname(), toSqlDate(user.getBirthdate()), user.getId() }; 试一试( Connection Connection=daoFactory.getConnection(); PreparedStatement=prepareStatement(连接、SQL\u更新、false、值); ) { int affectedRows=语句.executeUpdate(); if(affectedRows==0){ 抛出新的DAOException(“更新用户失败,没有受影响的行”); } }捕获(SQLE异常){ 抛出新的异常(e); } } @凌驾 公共void delete(用户)引发异常{ 对象[]值={ user.getId() }; 试一试( Connection Connection=daoFactory.getConnection(); PreparedStatement=prepareStatement(连接、SQL\u删除、false、值); ) { int affectedRows=语句.executeUpdate(); if(affectedRows==0){ 抛出新的异常(“删除用户失败,没有受影响的行”); }否则{ user.setId(null); } }捕获(SQLE异常){ 抛出新的异常(e); } } @凌驾 public boolean existEmail(字符串电子邮件)引发异常{ 对象[]值={ 电子邮件 }; 布尔存在=假; 试一试( Connection Connection=daoFactory.getConnection(); PreparedStatement=prepareStatement(连接、SQL存在、电子邮件、false、值); ResultSet ResultSet=statement.executeQuery(); ) { exist=resultSet.next(); }捕获(SQLE异常){ 抛出新的异常(e); } 回归存在; } @凌驾 public void changePassword(用户)引发异常{ if(user.getId()==null){ 抛出新的IllegalArgumentException(“用户尚未创建,用户ID为null”); } 对象[]值={ user.getPassword(), user.getId() }; 试一试( Connection Connection=daoFactory.getConnection(); PreparedStatement语句=prepareStatement(连接、SQL\u更改\u密码、false、值); ) { int affectedRows=语句.executeUpdate(); if(affectedRows==0){ 抛出新的DAOException(“更改密码失败,没有受影响的行”); } }捕获(SQLE异常){ 抛出新的异常(e); } } //助手------------------------------------------------------------------------------------ /** *将给定结果集的当前行映射到用户。 *@param resultSet当前行要映射到用户的结果集。 *@从给定结果集的当前行返回映射用户。 *@在数据库级别出现故障时抛出SQLException。 */ 私有静态用户映射(ResultSet ResultSet)引发SQLException{ 用户=新用户(); user.setId(resultSet.getLong(“id”); user.setEmail(resultSet.getString(“电子邮件”); user.setFirstname(resultSet.getString(“firstname”); user.setLastname(resultSet.getString(“lastname”); user.setBirthdate(resultSet.getDate(“生日”); 返回用户; } }
我发现帖子中的一条评论有类似的问题,但我猜巴卢斯没有时间去看它。巴卢斯正在使用语句,它会自动关闭在try块括号内打开的连接(以及语句和结果集)。!。我错过了。如何将连接返回到池?关闭是否会自动返回到池的连接?是。池连接重写close()方法,以便只返回池。Java Id()=空){ 抛出新的IllegalArgumentException(“用户尚未创建,用户ID为null”); } 对象[]值={ user.getEmail(), user.getFirstname(), user.getLastname(), toSqlDate(user.getBirthdate()), user.getId() }; 试一试( Connection Connection=daoFactory.getConnection(); PreparedStatement=prepareStatement(连接、SQL\u更新、false、值); ) { int affectedRows=语句.executeUpdate(); if(affectedRows==0){ 抛出新的DAOException(“更新用户失败,没有受影响的行”); } }捕获(SQLE异常){ 抛出新的异常(e); } } @凌驾 公共void delete(用户)引发异常{ 对象[]值={ user.getId() }; 试一试( Connection Connection=daoFactory.getConnection(); PreparedStatement=prepareStatement(连接、SQL\u删除、false、值); ) { int affectedRows=语句.executeUpdate(); if(affectedRows==0){ 抛出新的异常(“删除用户失败,没有受影响的行”); }否则{ user.setId(null); } }捕获(SQLE异常){ 抛出新的异常(e); } } @凌驾 public boolean existEmail(字符串电子邮件)引发异常{ 对象[]值={ 电子邮件 }; 布尔存在=假; 试一试( Connection Connection=daoFactory.getConnection(); PreparedStatement=prepareStatement(连接、SQL存在、电子邮件、false、值); ResultSet ResultSet=statement.executeQuery(); ) { exist=resultSet.next(); }捕获(SQLE异常){ 抛出新的异常(e); } 回归存在; } @凌驾 public void changePassword(用户)引发异常{ if(user.getId()==null){ 抛出新的IllegalArgumentException(“用户尚未创建,用户ID为null”); } 对象[]值={ user.getPassword(), user.getId() }; 试一试( Connection Connection=daoFactory.getConnection(); PreparedStatement语句=prepareStatement(连接、SQL\u更改\u密码、false、值); ) { int affectedRows=语句.executeUpdate(); if(affectedRows==0){ 抛出新的DAOException(“更改密码失败,没有受影响的行”); } }捕获(SQLE异常){ 抛出新的异常(e); } } //助手------------------------------------------------------------------------------------ /** *将给定结果集的当前行映射到用户。 *@param resultSet当前行要映射到用户的结果集。 *@从给定结果集的当前行返回映射用户。 *@在数据库级别出现故障时抛出SQLException。 */ 私有静态用户映射(ResultSet ResultSet)引发SQLException{ 用户=新用户(); user.setId(resultSet.getLong(“id”); user.setEmail(resultSet.getString(“电子邮件”); user.setFirstname(resultSet.getString(“firstname”); user.setLastname(resultSet.getString(“lastname”); user.setBirthdate(resultSet.getDate(“生日”); 返回用户; } },java,database,jdbc,resultset,Java,Database,Jdbc,Resultset,我发现帖子中的一条评论有类似的问题,但我猜巴卢斯没有时间去看它。巴卢斯正在使用语句,它会自动关闭在try块括号内打开的连接(以及语句和结果集)。!。我错过了。如何将连接返回到池?关闭是否会自动返回到池的连接?是。池连接重写close()方法,以便只返回池。 public void close() throws SQLException { if (this.connection is still eligible for reuse) { do not c
public void close() throws SQLException {
if (this.connection is still eligible for reuse) {
do not close this.connection, but just return it to pool for reuse;
} else {
actually invoke this.connection.close();
}
}
package com.example.dao;
import static com.example.dao.DAOUtil.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.example.model.User;
/**
* This class represents a concrete JDBC implementation of the {@link UserDAO} interface.
*
* @author BalusC
* @link http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html
*/
public class UserDAOJDBC implements UserDAO {
// Constants ----------------------------------------------------------------------------------
private static final String SQL_FIND_BY_ID =
"SELECT id, email, firstname, lastname, birthdate FROM User WHERE id = ?";
private static final String SQL_FIND_BY_EMAIL_AND_PASSWORD =
"SELECT id, email, firstname, lastname, birthdate FROM User WHERE email = ? AND password = MD5(?)";
private static final String SQL_LIST_ORDER_BY_ID =
"SELECT id, email, firstname, lastname, birthdate FROM User ORDER BY id";
private static final String SQL_INSERT =
"INSERT INTO User (email, password, firstname, lastname, birthdate) VALUES (?, MD5(?), ?, ?, ?)";
private static final String SQL_UPDATE =
"UPDATE User SET email = ?, firstname = ?, lastname = ?, birthdate = ? WHERE id = ?";
private static final String SQL_DELETE =
"DELETE FROM User WHERE id = ?";
private static final String SQL_EXIST_EMAIL =
"SELECT id FROM User WHERE email = ?";
private static final String SQL_CHANGE_PASSWORD =
"UPDATE User SET password = MD5(?) WHERE id = ?";
// Vars ---------------------------------------------------------------------------------------
private DAOFactory daoFactory;
// Constructors -------------------------------------------------------------------------------
/**
* Construct an User DAO for the given DAOFactory. Package private so that it can be constructed
* inside the DAO package only.
* @param daoFactory The DAOFactory to construct this User DAO for.
*/
UserDAOJDBC(DAOFactory daoFactory) {
this.daoFactory = daoFactory;
}
// Actions ------------------------------------------------------------------------------------
@Override
public User find(Long id) throws DAOException {
return find(SQL_FIND_BY_ID, id);
}
@Override
public User find(String email, String password) throws DAOException {
return find(SQL_FIND_BY_EMAIL_AND_PASSWORD, email, password);
}
/**
* Returns the user from the database matching the given SQL query with the given values.
* @param sql The SQL query to be executed in the database.
* @param values The PreparedStatement values to be set.
* @return The user from the database matching the given SQL query with the given values.
* @throws DAOException If something fails at database level.
*/
private User find(String sql, Object... values) throws DAOException {
User user = null;
try (
Connection connection = daoFactory.getConnection();
PreparedStatement statement = prepareStatement(connection, sql, false, values);
ResultSet resultSet = statement.executeQuery();
) {
if (resultSet.next()) {
user = map(resultSet);
}
} catch (SQLException e) {
throw new DAOException(e);
}
return user;
}
@Override
public List<User> list() throws DAOException {
List<User> users = new ArrayList<>();
try (
Connection connection = daoFactory.getConnection();
PreparedStatement statement = connection.prepareStatement(SQL_LIST_ORDER_BY_ID);
ResultSet resultSet = statement.executeQuery();
) {
while (resultSet.next()) {
users.add(map(resultSet));
}
} catch (SQLException e) {
throw new DAOException(e);
}
return users;
}
@Override
public void create(User user) throws IllegalArgumentException, DAOException {
if (user.getId() != null) {
throw new IllegalArgumentException("User is already created, the user ID is not null.");
}
Object[] values = {
user.getEmail(),
user.getPassword(),
user.getFirstname(),
user.getLastname(),
toSqlDate(user.getBirthdate())
};
try (
Connection connection = daoFactory.getConnection();
PreparedStatement statement = prepareStatement(connection, SQL_INSERT, true, values);
) {
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new DAOException("Creating user failed, no rows affected.");
}
try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
user.setId(generatedKeys.getLong(1));
} else {
throw new DAOException("Creating user failed, no generated key obtained.");
}
}
} catch (SQLException e) {
throw new DAOException(e);
}
}
@Override
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);
}
}
@Override
public void delete(User user) throws DAOException {
Object[] values = {
user.getId()
};
try (
Connection connection = daoFactory.getConnection();
PreparedStatement statement = prepareStatement(connection, SQL_DELETE, false, values);
) {
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new DAOException("Deleting user failed, no rows affected.");
} else {
user.setId(null);
}
} catch (SQLException e) {
throw new DAOException(e);
}
}
@Override
public boolean existEmail(String email) throws DAOException {
Object[] values = {
email
};
boolean exist = false;
try (
Connection connection = daoFactory.getConnection();
PreparedStatement statement = prepareStatement(connection, SQL_EXIST_EMAIL, false, values);
ResultSet resultSet = statement.executeQuery();
) {
exist = resultSet.next();
} catch (SQLException e) {
throw new DAOException(e);
}
return exist;
}
@Override
public void changePassword(User user) throws DAOException {
if (user.getId() == null) {
throw new IllegalArgumentException("User is not created yet, the user ID is null.");
}
Object[] values = {
user.getPassword(),
user.getId()
};
try (
Connection connection = daoFactory.getConnection();
PreparedStatement statement = prepareStatement(connection, SQL_CHANGE_PASSWORD, false, values);
) {
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new DAOException("Changing password failed, no rows affected.");
}
} catch (SQLException e) {
throw new DAOException(e);
}
}
// Helpers ------------------------------------------------------------------------------------
/**
* Map the current row of the given ResultSet to an User.
* @param resultSet The ResultSet of which the current row is to be mapped to an User.
* @return The mapped User from the current row of the given ResultSet.
* @throws SQLException If something fails at database level.
*/
private static User map(ResultSet resultSet) throws SQLException {
User user = new User();
user.setId(resultSet.getLong("id"));
user.setEmail(resultSet.getString("email"));
user.setFirstname(resultSet.getString("firstname"));
user.setLastname(resultSet.getString("lastname"));
user.setBirthdate(resultSet.getDate("birthdate"));
return user;
}
}