Advertisement
Guest User

DatabaseService

a guest
Dec 1st, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.73 KB | None | 0 0
  1. /**
  2.  * Author: João Vieira
  3.  * Date: Oct-2015
  4.  */
  5.  
  6. package jfsa.backend;
  7.  
  8. import java.sql.*;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. /** Separate Java service class.
  14.  * Backend implementation for the address book application, with "detached entities"
  15.  * simulating real world DAO. Typically these something that the Java EE
  16.  * or Spring backend services provide.
  17.  */
  18. // Backend service class. This is just a typical Java backend implementation
  19. // class and nothing Vaadin specific.
  20. public class DatabaseService extends DataService {
  21.  
  22.     private static DatabaseService INSTANCE;
  23.     private static List<Processo> processos = new ArrayList<Processo>();
  24.     private static String url, username, password;
  25.     private SimpleDateFormat dateFormat;
  26.     private static int lastID;
  27.  
  28.     public DatabaseService() {
  29.         dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  30.         lastID = 0;
  31.     }
  32.  
  33.     public synchronized static DataService getInstance() {
  34.         if (INSTANCE == null) {
  35.             INSTANCE = new DatabaseService();
  36.  
  37. //            Context context = new InitialContext();
  38. //            DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
  39.  
  40.             url = "jdbc:mysql://localhost:3306/licenciamento";
  41.             username = "root";
  42.             password = "";
  43.  
  44.             try {
  45.                 Class.forName("com.mysql.jdbc.Driver");
  46. //                System.out.println("Driver loaded!");
  47.             } catch (ClassNotFoundException e) {
  48.                 throw new IllegalStateException("Cannot find the driver in the classpath!", e);
  49.             }
  50.  
  51.             try {
  52.                 Connection conn = DriverManager.getConnection(url, username, password);
  53.  
  54.                 Statement stmt = conn.createStatement();
  55.                 ResultSet rs = stmt.executeQuery("SELECT * FROM processos");
  56.                 while (rs.next()) {
  57.                     Processo p = new Processo();
  58.                     int id = rs.getInt("id");
  59.                     if (id > lastID)
  60.                         lastID = id;
  61.  
  62.                     p.setId(id);
  63.                     p.setNumeroProcesso(rs.getString("numeroprocesso"));
  64.                     p.setNomeReq(rs.getString("nomereq"));
  65.                     p.setMoradaReq(rs.getString("moradareq"));
  66.                     p.setCodigoPostalReq(rs.getString("codigopostalreq"));
  67.                     p.setLocalidadeReq(rs.getString("localidadereq"));
  68.                     p.setNif(rs.getString("nif"));
  69.                     p.setNome(rs.getString("nome"));
  70.                     p.setMorada(rs.getString("morada"));
  71.                     p.setCodigoPostal(rs.getString("codigopostal"));
  72.                     p.setLocalidade(rs.getString("localidade"));
  73.                     p.setValidade(rs.getDate("validade"));
  74.                     p.setEmail(rs.getString("email"));
  75.                     p.setTelemovel(rs.getString("telemovel"));
  76.                     p.setTelefone(rs.getString("telefone"));
  77.                     p.setObservacoes(rs.getString("observacoes"));
  78.                     processos.add(p);
  79.                 }
  80.                 rs.close();
  81.                 stmt.close();
  82.                 conn.close();
  83.  
  84.             } catch (SQLException e) {
  85.                 throw new IllegalStateException("Não foi possível ligar à  base de dados!", e);
  86.             }
  87.         }
  88.         return INSTANCE;
  89.     }
  90.  
  91.     @Override
  92.     public synchronized List<Processo> getAllProcessos() {
  93.         return processos;
  94.     }
  95.  
  96.     @Override
  97.     public synchronized void insertProcesso(Processo p) {
  98.         p.setId(++lastID);
  99.         try {
  100.             String sql = "INSERT INTO processos (id, numeroprocesso, nomereq, moradareq, codigopostalreq, localidadereq, nif, "
  101.                     + "nome, morada, codigopostal, localidade, validade, email, telemovel, telefone, observacoes) VALUES (";
  102.             sql += "'" + p.getId() + "',";
  103.             sql += "'" + p.getNumeroProcesso() + "',";
  104.             sql += "'" + p.getNomeReq() + "',";
  105.             sql += "'" + p.getMoradaReq() + "',";
  106.             sql += "'" + p.getCodigoPostalReq() + "',";
  107.             sql += "'" + p.getLocalidadeReq() + "',";
  108.             sql += "'" + p.getNif() + "',";
  109.             sql += "'" + p.getNome() + "',";
  110.             sql += "'" + p.getMorada() + "',";
  111.             sql += "'" + p.getCodigoPostal() + "',";
  112.             sql += "'" + p.getLocalidade() + "',";
  113.             sql += "'" + dateFormat.format(p.getValidade()) + "',";
  114.             sql += "'" + p.getEmail() + "',";
  115.             sql += "'" + p.getTelemovel() + "',";
  116.             sql += "'" + p.getTelefone() + "',";
  117.             sql += "'" + p.getObservacoes() + "')";
  118.  
  119.             Connection conn = DriverManager.getConnection(url, username, password);
  120.             Statement stmt = conn.createStatement();
  121.             stmt.executeUpdate(sql);
  122.             stmt.close();
  123.             conn.close();
  124.  
  125.         } catch (SQLException e) {
  126.             throw new IllegalStateException("Não foi possível guardar o processo " + p.getId() + " na base de dados!", e);
  127.         }
  128.         processos.add(p);
  129.     }
  130.  
  131.     @Override
  132.     public synchronized void updateProcesso(Processo p) {
  133.         try {
  134.             String sql = "UPDATE processos SET ";
  135.             sql += "numeroprocesso='" + p.getNumeroProcesso() + "',";
  136.             sql += "nomereq='" + p.getNomeReq() + "',";
  137.             sql += "moradareq='" + p.getMoradaReq() + "',";
  138.             sql += "codigopostalreq='" + p.getCodigoPostalReq() + "',";
  139.             sql += "localidadereq='" + p.getLocalidadeReq() + "',";
  140.             sql += "nif='" + p.getNif() + "',";
  141.             sql += "nome='" + p.getNome() + "',";
  142.             sql += "morada='" + p.getMorada() + "',";
  143.             sql += "codigopostal='" + p.getCodigoPostal() + "',";
  144.             sql += "localidade='" + p.getLocalidade() + "',";
  145.             sql += "validade='" + dateFormat.format(p.getValidade()) + "',";
  146.             sql += "email='" + p.getEmail() + "',";
  147.             sql += "telemovel='" + p.getTelemovel() + "',";
  148.             sql += "telefone='" + p.getTelefone() + "',";
  149.             sql += "observacoes='" + p.getObservacoes() + "' ";
  150.             sql += "WHERE id='" + p.getId() + "'";
  151.  
  152.             Connection conn = DriverManager.getConnection(url, username, password);
  153.             Statement stmt = conn.createStatement();
  154.             stmt.executeUpdate(sql);
  155.             stmt.close();
  156.             conn.close();
  157.  
  158.         } catch (SQLException e) {
  159.             throw new IllegalStateException("Não foi possível actualizar o processo " + p.getId() + " na base de dados!", e);
  160.         }
  161.  
  162.         for (int i = 0; i < processos.size(); i++) {
  163.             if (processos.get(i).getId() == p.getId()) {
  164.                 processos.set(i, p);
  165.                 return;
  166.             }
  167.         }
  168.     }
  169.  
  170.     @Override
  171.     public synchronized void deleteProcesso(Processo p) {
  172.         try {
  173.             String sql = "DELETE FROM processos WHERE ";
  174.             sql += "id='" + p.getId() + "'";
  175.  
  176.             Connection conn = DriverManager.getConnection(url, username, password);
  177.             Statement stmt = conn.createStatement();
  178.             stmt.executeUpdate(sql);
  179.             stmt.close();
  180.             conn.close();
  181.  
  182.         } catch (SQLException e) {
  183.             throw new IllegalStateException("Não foi possível eliminar o processo " + p.getId() + " da base de dados!", e);
  184.         }
  185.         processos.remove(p);
  186.     }
  187.  
  188.     @Override
  189.     public synchronized Processo getProcessoById(int processoID) {
  190.         for (int i = 0; i < processos.size(); i++) {
  191.             if (processos.get(i).getId() == processoID)
  192.                 return processos.get(i);
  193.         }
  194.         return null;
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement