Boa tarde, alguém com bastante experiencia poderia me dizer se este tipo de conexão com o mysql está correto? Ou melhor está satisfatório? As queries estão tudo rodando mas não sei se o modelo é aplicável.
Estou criando a conexão através de um DataSource.
- server.xml :
<Context crossContext="true" debug="5" docBase="sgm" path="/sgm" reloadable="true" source="org.eclipse.jst.jee.server:sgm">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/ConexaoDB"
password="221088" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/SGM" username="root"/>
</Context>
- UtilDAO:
public class UtilDAO
{
private static Connection conn;
private static DataSource ds;
@SuppressWarnings("unused")
public static Connection getConexao() throws SQLException
{
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/ConexaoDB");
}catch(Exception e) {
e.printStackTrace();
}
conn = ds.getConnection();
return conn;
}
public static void fecharConexao(ResultSet rs,
PreparedStatement pstmt,
Connection conexao) throws SQLException
{
if(!rs.isClosed())
rs.close();
if(!pstmt.isClosed())
pstmt.close();
if(!conexao.isClosed())
conexao.close();
}
}
-Metodo para Exemplo em utilizar a conexão e fecha-la no DAO!
public ArrayList<PessoaJuridica> listarPJ() throws Exception
{
ArrayList<PessoaJuridica> pjs = new ArrayList<PessoaJuridica>();
String sql = "select cd_empresa,ch_nome,ch_razao,ch_cnpj "+
"from pessoajuridica;";
PreparedStatement pstmt=null;
ResultSet rs = null;
try
{
pstmt = this.conexao.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next())
{
PessoaJuridica pj = new PessoaJuridica();
pj.setCodigo(rs.getInt("cd_empresa"));
pj.setNome(rs.getString("ch_nome"));
pj.setRazao(rs.getString("ch_razao"));
pj.setCnpj(rs.getString("ch_cnpj"));
pjs.add(pj);
}
}catch (Exception e)
{
e.printStackTrace();
}
finally
{
UtilDAO.fecharConexao(rs, pstmt, conexao);
}
return pjs;
}
Por favor alguma critica??
Obrigado.