Advertisement
Guest User

Untitled

a guest
May 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.67 KB | None | 0 0
  1. package Fundacao;
  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.     private static Connection conn;
  13.     private final String DRIVER = "oracle.jdbc.OracleDriver";
  14.     protected String sql;
  15.     private boolean connected = false;
  16.     private String engine = "jdbc:oracle:";
  17.     private boolean connect() throws SQLException {
  18.         return this.connect(DataAccessDTO.host, DataAccessDTO.user, DataAccessDTO.password);
  19.     }
  20.  
  21.     private boolean connect(String host, String user, String password) throws SQLException {
  22.  
  23.         try {
  24.             Class.forName(DRIVER);
  25.             String driverLink = engine + "oci:@" + host + ":1521";
  26.             System.out.print(driverLink);
  27.             conn = DriverManager.getConnection(driverLink, user, password);
  28.             conn.setAutoCommit(false);
  29.             this.connected = true;
  30.         }catch (ClassNotFoundException e){
  31.             System.out.println("Driver JDBC Não encontrado " + e.getMessage());
  32.         } catch (SQLException e) {
  33.             System.out.println("ERRO QUERY " + e.getMessage());
  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 " + ex.getMessage());
  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 void persistInsert(T obj) {
  78.     }
  79.  
  80.     protected void persistDelete(T obj) {
  81.     }
  82.  
  83.     protected void persistUpdate(T obj) {
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement