Há algum problema fazer desta forma?
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CategoriaDAO {
private Connection connection;
public CategoriaDAO(Connection connection) {
this.connection = connection;
}
public void listar() throws SQLException {
try (PreparedStatement pstm = connection.prepareStatement("SELECT * FROM CATEGORIA")) {
pstm.execute();
try (ResultSet rst = pstm.getResultSet()) {
while (rst.next()) {
String id = rst.getString("id");
String nome = rst.getString("nome");
System.out.printf("ID: %s NOME: %s", id, nome);
System.out.println();
}
}
}
}
}
package br.com.alura.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import dao.CategoriaDAO;
import factory.ConnectionFactory;
public class TestaListagemDeCategorias {
public static void main(String[] args) throws SQLException {
try (Connection connection = new ConnectionFactory().recuperarConexao()) {
CategoriaDAO categoriaDAO = new CategoriaDAO(connection);
categoriaDAO.listar();
}
}
}