Advertisement
Guest User

Untitled

a guest
May 20th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.58 KB | None | 0 0
  1. package Interfaces;
  2.  
  3. import Apresentacao.DataAccessDTO;
  4. import Dominio.Entity;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. public abstract class Repository<T extends Entity> {
  12.  
  13.     private static Connection conn;
  14.     private final String DRIVER = "com.mysql.jdbc.Driver";
  15.     private String host;
  16.     protected String sql;
  17.     private String MySQL = "jdbc:mysql://";
  18.     private boolean connected = false;
  19.  
  20.     private boolean connect() throws SQLException {
  21.         return this.connect(DataAccessDTO.host, DataAccessDTO.user, DataAccessDTO.password, DataAccessDTO.database);
  22.     }
  23.  
  24.     private boolean connect(String host, String user, String password, String dbName) throws SQLException {
  25.         this.host = host;
  26.         try {
  27.             Class.forName(DRIVER);
  28.             String dbMySQL = MySQL + host + "/" + dbName;
  29.             conn = DriverManager.getConnection(dbMySQL, user, password);
  30.             conn.setAutoCommit(false);
  31.             this.connected = true;
  32.         } catch (ClassNotFoundException ex) {
  33.             System.out.println("Driver JDBC Não encontrado");
  34.         }
  35.         return this.connected;
  36.     }
  37.  
  38.     private void disconnect() {
  39.         try {
  40.             Class.forName(DRIVER);
  41.             conn = null;
  42.             this.connected = false;
  43.         } catch (ClassNotFoundException ex) {
  44.             System.out.println("Driver JDBC Não encontrado");
  45.         }
  46.     }
  47.  
  48.     protected ResultSet executeQuery(String sql) throws SQLException {
  49.         if (this.connect()) {
  50.             Statement statement = conn.createStatement();
  51.             ResultSet result = statement.executeQuery(sql);
  52.             this.disconnect();
  53.             return result;
  54.         }
  55.         return null;
  56.     }
  57.  
  58.     public void insert(T obj) throws SQLException {
  59.         this.persistInsert(obj);
  60.         ResultSet res = this.executeQuery(this.sql);
  61.         obj.setId(res.getInt("id"));
  62.     }
  63.  
  64.     public void update(T obj) throws SQLException, Exception {
  65.         if (obj.getId() == 0) {
  66.             throw new Exception("Não é possivel atualizar o registro sem um 'ID' na Entidade.");
  67.         }
  68.         this.persistUpdate(obj);
  69.         this.executeQuery(this.sql);
  70.     }
  71.  
  72.     public void delete(T obj) throws SQLException {
  73.         this.persistDelete(obj);
  74.         this.executeQuery(this.sql);
  75.     }
  76.  
  77.     protected abstract void persistInsert(T obj);
  78.  
  79.     protected abstract void persistDelete(T obj);
  80.  
  81.     protected abstract void persistUpdate(T obj);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement