For educational purposes, I’m developing a simple desktop/console Java program to persist data into MySQL database. I’d like to know if the following architecure/layers are ok (despite the computational/maintenance cost, interpretability etc):
Class Product
<code>public class Product {
private int id;
private String name;
//getters and setters and constructors
}
</code>
<code>public class Product {
private int id;
private String name;
//getters and setters and constructors
}
</code>
public class Product {
private int id;
private String name;
//getters and setters and constructors
}
ConnectionFactory:
<code>public abstract class ConnectionFactory {
private static Connection conn = null;
public static Connection getConnection() throws SQLException{
if (conn == null)
conn = DriverManager.getConnection("jdbc:mysql://localhost/database_name","user","password");
return conn;
}
}
</code>
<code>public abstract class ConnectionFactory {
private static Connection conn = null;
public static Connection getConnection() throws SQLException{
if (conn == null)
conn = DriverManager.getConnection("jdbc:mysql://localhost/database_name","user","password");
return conn;
}
}
</code>
public abstract class ConnectionFactory {
private static Connection conn = null;
public static Connection getConnection() throws SQLException{
if (conn == null)
conn = DriverManager.getConnection("jdbc:mysql://localhost/database_name","user","password");
return conn;
}
}
Interface DAO
<code>public interface DAO <T>{
void save(T type);
List<T> findAll();
}
</code>
<code>public interface DAO <T>{
void save(T type);
List<T> findAll();
}
</code>
public interface DAO <T>{
void save(T type);
List<T> findAll();
}
Interface ProductDAO
<code>public class ProductDAO implements DAO<Product>{
@Override
public void save(Product p) {
String sql = "INSERT INTO product values (?,?)";
try(Connection con = ConnectionFactory.getConnection();
PreparedStatement stmt = con.prepareStatement(sql)) {
stmt.setInt(1, p.getId());
stmt.setString(2, p.getName());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List<Product> buscarTodos() {
String sql = "SELECT * FROM product";
List<Product> listProd =new ArrayList<>();
try(Connection con = ConnectionFactory.criaConexao();
PreparedStatement stmt = con.prepareStatement(sql)) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Product p = new Product(rs.getInt("id"), rs.getString("name"));
listProd.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return listProd;
}
}
</code>
<code>public class ProductDAO implements DAO<Product>{
@Override
public void save(Product p) {
String sql = "INSERT INTO product values (?,?)";
try(Connection con = ConnectionFactory.getConnection();
PreparedStatement stmt = con.prepareStatement(sql)) {
stmt.setInt(1, p.getId());
stmt.setString(2, p.getName());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List<Product> buscarTodos() {
String sql = "SELECT * FROM product";
List<Product> listProd =new ArrayList<>();
try(Connection con = ConnectionFactory.criaConexao();
PreparedStatement stmt = con.prepareStatement(sql)) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Product p = new Product(rs.getInt("id"), rs.getString("name"));
listProd.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return listProd;
}
}
</code>
public class ProductDAO implements DAO<Product>{
@Override
public void save(Product p) {
String sql = "INSERT INTO product values (?,?)";
try(Connection con = ConnectionFactory.getConnection();
PreparedStatement stmt = con.prepareStatement(sql)) {
stmt.setInt(1, p.getId());
stmt.setString(2, p.getName());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List<Product> buscarTodos() {
String sql = "SELECT * FROM product";
List<Product> listProd =new ArrayList<>();
try(Connection con = ConnectionFactory.criaConexao();
PreparedStatement stmt = con.prepareStatement(sql)) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Product p = new Product(rs.getInt("id"), rs.getString("name"));
listProd.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return listProd;
}
}
Interface ProductService:
<code>public interface Service <T>{
void save(T type);
List<T> findAll();
}
</code>
<code>public interface Service <T>{
void save(T type);
List<T> findAll();
}
</code>
public interface Service <T>{
void save(T type);
List<T> findAll();
}
Class ProductService:
<code>public class ProductService implements Service<Product>{
ProductDAO productDAO ;
public ProductService() {
productDAO= new ProductDAO();
}
@Override
public void save(Product p) {
productDAO.save(p);
}
@Override
public List<Product> findAll() {
return productDAO.findAll();
}
}
</code>
<code>public class ProductService implements Service<Product>{
ProductDAO productDAO ;
public ProductService() {
productDAO= new ProductDAO();
}
@Override
public void save(Product p) {
productDAO.save(p);
}
@Override
public List<Product> findAll() {
return productDAO.findAll();
}
}
</code>
public class ProductService implements Service<Product>{
ProductDAO productDAO ;
public ProductService() {
productDAO= new ProductDAO();
}
@Override
public void save(Product p) {
productDAO.save(p);
}
@Override
public List<Product> findAll() {
return productDAO.findAll();
}
}
I’d be grateful for comments and suggestions.
New contributor
user24618785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.