Bem, estou no caminho certo?
A propósito, alguém conhece o livro:
Modelagem e Projetos Baseados em Objetos - Tradução da 2ª Edição
Rumbaugh, James; Blaha, Michael / CAMPUS
package br.com.trimbase.rastreio.bd;
import java.sql.*;
public class DAO {
// Pretendo fazer desse DAO algo generico (para qq banco), por isso estes atributos
private ConnectionPool pool = null;
private Connection con = null;
private String host = null;
private String usuario = null;
private String senha = null;
private String URL = null;
private String driver = null;
private String baseDados = null;
private String prefixoBase = null;
private String porta = null;
public DAO() {
super();
host = "localhost";
usuario = "postgres";
senha = "agente86";
driver = "org.postgresql.Driver";
baseDados = "os";
prefixoBase = "jdbc:postgresql://";
porta = "5432";
URL = prefixoBase + host + ":" + porta + "/" + baseDados + "/";
}
public synchronized Connection getConnection() {
pool = ConnectionPool.getInstance(URL, usuario, senha, driver, 5);
con = pool.getConnection();
return con;
}
public synchronized void liberaConexao(Connection con) {
if (pool != null) {
pool.returnConnection(con);
}
}
public void terminaConexoes() {
if (pool != null) {
pool.release();
}
pool = null;
}
public ConnectionPool getPool() {
return pool;
}
public void setPool(ConnectionPool pool) {
this.pool = pool;
}
}
package br.com.trimbase.rastreio.bd;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
public abstract class DAOGenerico {
private DAO dao = new DAO();
private Connection conn = dao.getConnection();
public long getCodigo(String sequencia) {
long retorno = 0;
PreparedStatement stmt;
String sql;
try {
sql = " SELECT nextval(\'" + sequencia + "\') ";
stmt = getConn().prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while ((rs != null) && (rs.next())) {
retorno = rs.getLong(1);
}
}catch(Exception e) {
e.printStackTrace();
}
dao.liberaConexao(conn);
sql = null;
stmt = null;
dao = null;
conn = null;
return retorno;
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public DAO getDao() {
return dao;
}
public void setDao(DAO dao) {
this.dao = dao;
}
}
package br.com.trimbase.agente.dao;
import java.sql.PreparedStatement;
import br.com.trimbase.rastreio.bd.DAOGenerico;
import br.com.trimbase.agente.*;
public class FisicaDAO extends DAOGenerico {
public boolean cadastra(Fisica fisica) {
boolean retorno = false;
PreparedStatement stmt;
long codAgente = getCodigo("agente_agecod_seq");
String sql = " INSERT INTO agente (agecod, agenom, ageobs) VALUES (?, ?, ?) ";
try{
stmt = getConn().prepareStatement(sql);
stmt.setLong(1, codAgente);
stmt.setString(2, fisica.getNome());
stmt.setString(3, fisica.getObs());
retorno = stmt.execute();
if (retorno) {
sql = " INSERT INTO fisica (fisage, fiscpf) VALUE (?, ?) ";
stmt = getConn().prepareStatement(sql);
stmt.setLong(1, codAgente);
stmt.setString(2, fisica.getCpf());
retorno = stmt.execute();
}
}catch (Exception ex) {
ex.printStackTrace();
}
getDao().liberaConexao(getConn());
sql = null;
stmt = null;
setDao(null);
setConn(null);
return retorno;
}
}