1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
public class UserDAO {
/**
* The SQL statement to create a new user
*/
protected static final String CREATE_STATEMENT = "INSERT INTO _user (login, first_name, last_name, email, user_group, pwd) VALUES (?, ?, ?, ?, ?, ?);";
/**
* The SQL statement to update a user (except the encoded key)
*/
protected static final String UPDATE_STATEMENT = "UPDATE _user SET login = ?, first_name = ?, last_name = ?, email = ?, user_group = ? WHERE id = ?;";
/**
* The SQL statement to delete a user
*/
protected static final String DELETE_STATEMENT = "DELETE FROM _user WHERE id = ?;";
/**
* The SQL statement to find all the users
*/
protected static final String FIND_ALL_STATEMENT = "SELECT * FROM _user ORDER BY login;";
/**
* The SQL statement to find a user by login and pwd
*/
protected static final String CONNECT_STATEMENT = "SELECT * FROM _user WHERE login = ? AND pwd = ?;";
public User create(String login, String firstName, String lastName, String email, UserGroup userGroup, String pwd) throws DAOException
{
Connection dbConnection = null;
User user = null;
try {
// DBConnectionManager is the manager of my custom connection pool
dbConnection = DBConnectionManager.getConnection();
dbConnection.setAutoCommit(false);
User user = null;
// Create the user
PreparedStatement ps = dbConnection.prepareStatement(CREATE_STATEMENT);
ps.setString(1, login);
ps.setString(2, firstName);
ps.setString(3, lastName);
ps.setString(4, email);
ps.setLong(5, userGroup.getId());
ps.setString(6, encodedKey);
if( ps.executeUpdate() == 1 )
{
user = new User(login, firstName, lastName, email, userGroup, pwd);
else
{
dbConnection.rollback();
throw new DAOException("Unable to create the user "+login, null);
}
ps.close();
dbConnection.commit();
}
catch (SQLException e) {
try{
if( dbConnection!=null )
dbConnection.rollback();
}
catch (SQLException rollbackException) {
ErrorLog.log("Unable to rollback after creating a user", rollbackException, sqlUser);
}
throw new DAOException("Unable to create the user "+login, e);
}
finally
{
DBConnectionManager.releaseConnection(dbResource, dbConnection);
}
return user;
}
// idem pour les methodes update, delete, authenficate(String login, String pwd) |
Partager