So with relation to the previous method. This is my UserDAO, and DaoFactory. The UserDao holds an instance of the daoFactory.
This is my UserDAO:
public class UsuariousDAO {
private static final String SQL_LIST_ALL =
"SELECT DISTINCT * "
+ "FROM usuarios WHERE NOT EXISTS (SELECT * FROM usuarios_grupos WHERE usuarios_grupos.id_grupo = ? AND usuarios_grupos.id_usuario = usuarios.id_usuario)";
private static final String SQL_INSERT =
"INSERT INTO usuarios (nome, setor, senha, email, bloquear, admin) VALUES (?, ?, ?, ?, ?, ?)";
private static final String SQL_UPDATE =
"UPDATE usuario SET nome = ?, setor = ?, senha = ?, email = ?, bloquear = ?, admin = ? WHERE id_usuario = ?";
private static final String SQL_DELETE =
"DELETE FROM usuario WHERE id_usuario = ?";
private DAOFactory daoFactory;
UsuariousDAO(DAOFactory daoFactory) {
this.daoFactory = daoFactory;
}
public Usuarious find(Integer id) throws DAOExceptions {
return find(SQL_LIST_BY_ID_GRUPO, id);
}
I have the following methods in my USERDAO:
private Usuarious find(String sql, Object... values) throws DAOExceptions {
CODE
}
public List<Usuarious> list() throws DAOExceptions {
CODE
}
public List<Usuarious> list(Grupos groups) throws DAOExceptions {
CODE
}
public void create(Usuarious user) throws IllegalArgumentException, DAOExceptions {
CODE
}
public void update(Usuarious user) throws DAOExceptions {
CODE
}
public void save(Usuarious user) throws DAOExceptions {
if (user.getId_usuario() == null) {
create(user);
} else {
update(user);
}
}
public void delete(Usuarious user) throws DAOExceptions {
CODE
}
private static Usuarious mapUser(ResultSet rs) throws SQLException {
Usuarious user = new Usuarious(rs.getInt("id_usuario"), rs.getString("nome"), rs.getString("setor"),
rs.getString("senha"), rs.getString("email"), rs.getString("bloquear"), rs.getString("admin"));
return user;
}
} //end of class
MY DaoFactory class is the following:
public abstract class DAOFactory {
private static final String JNDI_ROOT = "java:comp/env/";
public static DAOFactory getInstance(String name) throws DAOConfigurationException {
if (name == null) {
throw new DAOConfigurationException("Database name is null.");
}
String url = "jdbc:mysql://200.230.71.12:3306/social";
String driverClassName = "com.mysql.jdbc.Driver";
String password = "1234cinco";
String username = "cepein";
DAOFactory instance;
if (driverClassName != null) {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
throw new DAOConfigurationException(
"Driver class '" + driverClassName + "' is missing in classpath.", e);
}
instance = new DriverManagerDAOFactory(url, username, password);
}
// Else assume URL as DataSource URL and lookup it in the JNDI.
else {
DataSource dataSource;
try {
dataSource = (DataSource) new InitialContext().lookup(JNDI_ROOT + url);
} catch (NamingException e) {
throw new DAOConfigurationException(
"DataSource '" + url + "' is missing in JNDI.", e);
}
if (username != null) {
instance = new DataSourceWithLoginDAOFactory(dataSource, username, password);
} else {
instance = new DataSourceDAOFactory(dataSource);
}
}
return instance;
}
abstract Connection getConnection() throws SQLException;
// DAO getters --------------------------------------------------------------------------------
/**
* Returns the User,Grupos, UserGrupos DAO associated with the current DAOFactory.
* @return The User,Grupos, UserGrupos DAO associated with the current DAOFactory.
*/
public UsuariousDAO getUserDAO() {
return new UsuariousDAO(this);
}
public GruposDAO getGruposDAO() {
return new GruposDAO(this);
}
public UsuariousGruposDAO getUsuariousGruposDAO() {
return new UsuariousGruposDAO(this);
}
}
class DriverManagerDAOFactory extends DAOFactory {
private String url;
private String username;
private String password;
DriverManagerDAOFactory(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
}
Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
}
/**
* The DataSource based DAOFactory.
*/
class DataSourceDAOFactory extends DAOFactory {
private DataSource dataSource;
DataSourceDAOFactory(DataSource dataSource) {
this.dataSource = dataSource;
}
Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
/**
* The DataSource-with-Login based DAOFactory.
*/
class DataSourceWithLoginDAOFactory extends DAOFactory {
private DataSource dataSource;
private String username;
private String password;
DataSourceWithLoginDAOFactory(DataSource dataSource, String username, String password) {
this.dataSource = dataSource;
this.username = username;
this.password = password;
}
Connection getConnection() throws SQLException {
return dataSource.getConnection(username, password);
}
}
Here is my list(Grupos grps):
public List<Usuarious> list(Grupos groups) throws DAOExceptions {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Usuarious> users = new ArrayList<Usuarious>();
try {
connection = daoFactory.getConnection();
preparedStatement = connection.prepareStatement(SQL_LIST_ALL);
preparedStatement.setInt(1, groups.getId_grupo());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
users.add(mapUser(resultSet));
}
} catch (SQLException e) {
throw new DAOExceptions(e);
} finally {
close(connection, preparedStatement, resultSet);
}
return users;
}
I call the method in my User managed bean here:
public List<Usuarious> getListOfUsuarios() throws DAOExceptions {
List<Usuarious> usuariosList = userDAO.list(grps);
listOfUsuarios = usuariosList;
return listOfUsuarios;
}
and in my view, the following:
<p:dataTable var="users" value="#{usuariousGruposBean.listOfUsuarios}"
selection="#{users}" selectionMode="single">
<p:column headerText="" style="height:0" rendered ="false">
<h:outputText value="#{users.id_usuario}"/>
</p:column>