Advertisement
Guest User

Untitled

a guest
May 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.63 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 Repositorio<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.     public Repositorio() {
  21.     }
  22.  
  23.     public boolean connect() throws SQLException {
  24.         return this.connect(DataAccessDTO.host, DataAccessDTO.user, DataAccessDTO.password, DataAccessDTO.database);
  25.     }
  26.  
  27.     private boolean connect(String host, String user, String password, String dbName) throws SQLException {
  28.         this.host = host;
  29.         try {
  30.             Class.forName(DRIVER);
  31.             String dbMySQL = MySQL + host + "/" + dbName;
  32.             conn = DriverManager.getConnection(dbMySQL, user, password);
  33.             conn.setAutoCommit(false);
  34.             this.connected = true;
  35.  
  36.         } catch (ClassNotFoundException ex) {
  37.             System.out.println("Driver JDBC Não encontrado");
  38.         }
  39.         return this.connected;
  40.  
  41.     }
  42.  
  43.     public void Disconnect() {
  44.         try {
  45.             Class.forName(DRIVER);
  46.             conn = null;
  47.             this.connected = false;
  48.         } catch (ClassNotFoundException ex) {
  49.             System.out.println("Driver JDBC Não encontrado");
  50.         }
  51.     }
  52.  
  53.     public ResultSet executeQuery(String sql) throws SQLException {
  54.         this.connect();    
  55.        
  56.         Statement statement = this.conn.createStatement();
  57.         ResultSet result = statement.executeQuery(sql);
  58.         this.Disconnect();
  59.         return result;
  60.  
  61.  
  62.     }
  63.  
  64.     public void insert(T obj)throws SQLException{
  65.         this.persistInsert(obj);        
  66.         ResultSet res = this.executeQuery(this.sql);
  67.         obj.setId(res.getInt("id"));
  68.     }
  69.    
  70.     public void update(T obj)throws SQLException, Exception{
  71.         if(obj.getId()==0){
  72.             throw new Exception("Não é possivel atualizar o registro sem um 'ID' na Entidade.");
  73.         }
  74.  
  75.         this.persistUpdate(obj);        
  76.         this.executeQuery(this.sql);
  77.  
  78.     }
  79.     public void delete(T obj)throws SQLException{
  80.         this.persistDelete(obj);        
  81.         this.executeQuery(this.sql);
  82.        
  83.     }    
  84.  
  85.     protected abstract Object persistInsert(Object obj);
  86.     protected abstract Object persistDelete(Object obj);
  87.     protected abstract Object persistUpdate(Object obj);
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement