Advertisement
xlrnxnlx

[class] DAOClient

Feb 24th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. package tbanco.dao;
  2.  
  3. import java.sql.*;
  4. import tbanco.modelo.Client;
  5.  
  6. public class DAOClient extends DAO implements InterfaceDAO {
  7.    
  8.     private Client client;
  9.    
  10.     // <editor-fold defaultstate="collapsed" desc="Insert, Delete, Update, Select">
  11.     @Override
  12.     public boolean insert(Object object) {
  13.         boolean success = false;
  14.         if (connect()) {
  15.             this.client = (Client)object; // cast
  16.             String query =
  17.             "insert into client values (" + client.getId() + ", '" + client.getName() +"', '" + client.getAddress() + "')";
  18.             //String query = "begin insert_client(" + client.getId() + ", '" + client.getName() + "','" + client.getAddress() + "'); end";
  19.             try {
  20.                 state.execute(query);
  21.                 success = true;
  22.             } catch (SQLException e) {
  23.                 lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
  24.                 disconnect();
  25.             }
  26.             disconnect();
  27.         } else {
  28.         }
  29.         return success;
  30.     }
  31.  
  32.     @Override
  33.     public boolean delete(Object object) {
  34.         boolean success = false;
  35.         if (connect()) {
  36.             this.client = (Client)object;
  37.             String query = "delete from client c where c.id = " + client.getId();
  38.             //String query = "begin delete_client(" + client.getId() + "); end";
  39.             try {
  40.                 state.execute(query);
  41.                 success = true;
  42.             } catch (SQLException e) {
  43.                 lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
  44.                 disconnect();
  45.             }
  46.             disconnect();
  47.         }
  48.         return success;
  49.     }
  50.  
  51.     @Override
  52.     public boolean update(Object object) {
  53.         boolean success = false;
  54.         if (connect()) {
  55.             this.client = (Client)object;
  56.             String query =
  57.             "update client c set c.id =" + client.getId() + ", c.name = '" + client.getName() + "', c.addr = '" + client.getAddress() + "' where c.id = " + client.getId();
  58.             try {
  59.                 state.execute(query);
  60.                 success = true;
  61.             } catch(SQLException e) {
  62.                 lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
  63.                 disconnect();
  64.             }
  65.             disconnect();
  66.         }
  67.         return success;
  68.     }
  69.  
  70.     @Override
  71.     public boolean select(Object object) {
  72.         boolean success = false;
  73.         if (connect()) {
  74.             this.client = (Client)object;
  75.             String query =
  76.             "select * from client c where c.id = " + client.getId();
  77.             try {
  78.                 result = state.executeQuery(query);
  79.                 while (result.next()) {
  80.                     client.setId(result.getInt("id"));
  81.                     client.setName(result.getString("name"));
  82.                     client.setAddress(result.getString("addr"));
  83.                 }
  84.                 if ((client.getName() != null) && (client.getAddress() != null))
  85.                     success = true;
  86.             } catch (SQLException e) {
  87.                 lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
  88.                 disconnect();
  89.             }
  90.             disconnect();
  91.         }
  92.         return success;
  93.     }
  94.     // </editor-fold>
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement