E aí beleza?
Desculpem se estou me tornando cansativo, mas me surgiu outras duvidas:
Baseado novamente no tutotial do struts parte 1 posso dizer que a classe AdminUsers é uma espécie de bo?
Em minha aplicação para implementar a camada de persistência fiz da seguinte forma:
Criei uma interface para cada tabela do bando de dados com todos os métodos que deveria implementar, como insert,upadate e delete, inclusive os metodos de conexão com o banco. Mas percebi que os metodos de conexão com o banco de dados é sempre o mesmo para todas as classes. Como java não suporta herança multipla, posso colocar os metodos de conexão em outra classe e criar objetos para conexão?
Minha dúvida tem lógica ou estou comendo bolinhas?
Aí vai um exemplo dos DAOs que estou criando:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import vo.ClienteVO;
public interface ClienteDAO {
public Connection getConnection() throws SQLException;
public void closeConnection(Connection conn, PreparedStatement stmt, ResultSet rs);
public LinkedList getClienteList() throws SQLException;
public void insertCliente(ClienteVO cliente) throws SQLException;
public void updateCliente(ClienteVO cliente) throws SQLException;
public void deleteCliente(String cpfCliente) throws SQLException;
}
E:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import vo.ClienteVO;
public class AdminClienteDAO implements ClienteDAO{
protected static DataSource dataSource;
private final String insert = "insert into cliente (cpfCliente, nomCliente, emaCliente, telCliente, senCliente, totAlugado) values (?, ?, ?, ?, ?, ?, ?)";
private final String update = "update cliente set nomCliente = ?, emaCliente = ?, telCliente = ?, senCliente = ?, totAlugado = ? where icpfCliente = ?";
private final String delete = "delete from cliente where cpfCliente = ?";
private final String select = "select * from cliente";
public AdminClienteDAO() throws Exception {
if (dataSource == null) {
try {
InitialContext ic = new InitialContext();
// para o tomcat
dataSource = (DataSource) ic.lookup("java:comp/env/jdbc/Locadora");
}
catch (NamingException ex) {
System.out.println(ex.getMessage());
throw ex;
}
}
}
public Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = dataSource.getConnection();
}
catch (SQLException e) {
throw e;
}
return conn;
}
public void closeConnection(Connection conn, PreparedStatement stmt, ResultSet rs){
if (rs != null){
try {
rs.close();
}
catch (SQLException e) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
public LinkedList getClienteList() throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
LinkedList clientes = new LinkedList();
try {
conn = getConnection();
stmt = conn.prepareStatement(select);
rs = stmt.executeQuery();
while (rs.next()) {
ClienteVO cliente = new ClienteVO();
cliente.setCpfCliente(rs.getString("cpfCliente"));
cliente.setNomeCliente(rs.getString("nomCliente"));
cliente.setEmailCliente(rs.getString("emaCliente"));
cliente.setTelCliente(rs.getString("telCliente"));
cliente.setSenhaCliente(rs.getString("senCliente"));
cliente.setTotAlugado(rs.getInt("totAlugado"));
clientes.add(cliente);
}
}
catch (SQLException e) {
throw e;
}
finally {
closeConnection(conn, stmt, rs);
}
return clientes;
}
public void insertCliente(ClienteVO cliente) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.prepareStatement(insert);
stmt.setString(1, cliente.getCpfCliente());
stmt.setString(2, cliente.getNomCliente());
stmt.setString(3, cliente.getEmailCliente());
stmt.setString(4, cliente.getTelCliente());
stmt.setString(5, cliente.getSenhaCliente());
stmt.setInt(6, cliente.getTotAlugado());
stmt.executeUpdate();
}
catch (SQLException e) {
throw e;
}
finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
}
}
public void updateCliente(ClienteVO cliente) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.prepareStatement(update);
stmt.setString(1, cliente.getCpfCliente());
stmt.setString(2, cliente.getNomCliente());
stmt.setString(3, cliente.getEmailCliente());
stmt.setString(4, cliente.getTelCliente());
stmt.setString(5, cliente.getSenhaCliente());
stmt.setInt(6, cliente.getTotAlugado());
stmt.executeUpdate();
}
catch (SQLException e) {
throw e;
}
finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
}
}
public void deleteCliente(String cpfCliente) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.prepareStatement(delete);
stmt.setString(1, cpfCliente);
stmt.executeUpdate();
}
catch (SQLException e) {
throw e;
}
finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
}
}
}
e o vo está assim:
package vo;
public class ClienteVO{
private String cpfCliente;
private String nomCliente;
private String emaCliente;
private String telCliente;
private String senCliente;
private int totAlugado;
//setters
public void setCpfCliente(String cpf) {
this.cpfCliente = cpf;
}
public void setNomeCliente(String nome) {
this.nomCliente = nome;
}
public void setEmailCliente(String email) {
this.emaCliente = email;
}
public void setTelCliente(String tel) {
this.telCliente = tel;
}
public void setSenhaCliente(String senha) {
this.senCliente = senha;
}
public void setTotAlugado(int alugado) {
this.totAlugado = alugado;
}
//Getters
public String getCpfCliente() {
return this.cpfCliente;
}
public String getNomCliente() {
return this.nomCliente;
}
public String getEmailCliente() {
return this.emaCliente;
}
public String getTelCliente() {
return this.telCliente;
}
public String getSenhaCliente() {
return this.senCliente;
}
public int getTotAlugado() {
return this.totAlugado;
}
}
Até agora estou implementando corretamente os pattens DAO e VO?
Onde coloco o bo aqui?
Desde já a agradeço!!