Advertisement
m4ly

mysqlj

Nov 23rd, 2014
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. package pl.polsl.zti.db1.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator;
  12.  
  13. import pl.polsl.zti.db1.domain.Client;
  14. import pl.polsl.zti.db1.util.JdbcUtils;
  15.  
  16. public class ClientDaoJdbcImpl implements ClientDao {
  17.  
  18.     public Client getClient(int id) {
  19.         Client cln = null;
  20.         Connection con = null;
  21.         Statement stmt = null;
  22.  
  23.         try {
  24.             String sql = "SELECT * FROM CLIENTS WHERE id = " + id;
  25.  
  26.             con = JdbcUtils.getConnection();
  27.             stmt = con.createStatement();
  28.  
  29.             ResultSet rs = stmt.executeQuery(sql);
  30.             if (rs.next()) {
  31.                 cln = new Client();
  32.                 cln.setId(rs.getInt("id"));
  33.                 cln.setNo(rs.getInt("client_no"));
  34.                 cln.setSsn(rs.getString("ssn"));
  35.                 cln.setName(rs.getString("name"));
  36.                 cln.setAddress(rs.getString("address"));
  37.             }
  38.  
  39.         } catch (SQLException ec) {
  40.             ec.printStackTrace();
  41.         } finally {
  42.             JdbcUtils.closeStatement(stmt);
  43.             JdbcUtils.closeConnection(con);
  44.         }
  45.  
  46.         return cln;
  47.     }
  48.  
  49.     public List<Client> getClients() {
  50.         return getClients(null, null);
  51.     }
  52.  
  53.     // TODO: implementacja
  54.     public List<Client> getClients(String name, String address) {
  55.         // zaimplementowac pobieranie z bazy klientow wedlug nazwy lub/i adresu
  56.         // znak '_' zastepuje dowolny znak we wzorcu
  57.         // znak '%' zastepuje dowolny ciag znakow we wzorcu
  58.         // (wykorzystac operator LIKE)
  59.         // wartosc null w miejscu kryterium powoduje, ze nie jest ono brane pod
  60.         // uwage,
  61.         // przykadowo:
  62.         // * wywolanie getClients(null, null) ma zwrocic wszystkich klientow
  63.         // * wywolanie getClients('A%', null) ma zwrocic wszystkich klientow,
  64.         // ktorych nazwy zaczynaja sie na litere 'A'
  65.         // * wywolanie getClients('A%', 'B%') ma zwrocic wszystkich klientow,
  66.         // ktorych nazwy zaczynaja sie na litere 'A' oraz adres rozpoczyna sie
  67.         // od litery 'B'
  68.         Connection con = null;
  69.         Statement stmt = null;
  70.         ArrayList<Client> listka= new ArrayList<Client>();
  71.         ResultSet rs = null;
  72.        
  73.         try {
  74.             String sql = "";
  75.                    
  76.             //  * wywolanie getClients(null, null) ma zwrocic wszystkich klientow
  77.             if(name == null && address == null) {
  78.                 sql = "SELECT * FROM CLIENTS";
  79.                            
  80.             }
  81.             // * wywolanie getClients('A%', null) ma zwrocic wszystkich klientow, ktorych nazwy zaczynaja sie na litere 'A'
  82.             else if(name != null && address == null) {
  83.                 sql = "SELECT * FROM CLIENTS WHERE name LIKE '" + name + "'";                      
  84.             }
  85.             else if(name != null && address != null) {
  86.                 sql = "SELECT * FROM `CLIENTS` WHERE name LIKE '" + name + "' AND address LIKE '" + address + "'";
  87.             }
  88.             else if(address != null && name == null)
  89.             {
  90.                 sql = "SELECT * FROM `CLIENTS` WHERE address LIKE '" + address + "'";
  91.             }
  92.             else {}
  93.        
  94.    
  95.            
  96.             con = JdbcUtils.getConnection();           
  97.             stmt = con.createStatement();
  98.             rs = stmt.executeQuery(sql);
  99.            
  100.             while (rs.next()) {
  101.                 Client cln = new Client();
  102.                 cln.setId(rs.getInt("id"));
  103.                 cln.setNo(rs.getInt("client_no"));
  104.                 cln.setSsn(rs.getString("ssn"));
  105.                 cln.setName(rs.getString("name"));
  106.                 cln.setAddress(rs.getString("address"));
  107.                
  108.                 listka.add(cln);
  109.             }              
  110.  
  111.         } catch (SQLException ec) {
  112.             ec.printStackTrace();
  113.         } finally {
  114.             JdbcUtils.closeStatement(stmt);
  115.             JdbcUtils.closeConnection(con);
  116.         }
  117.  
  118.         return listka;
  119.     }
  120.  
  121.     // ID int NOT NULL AUTO_INCREMENT PRIMARY KEY, -- automatycznie generowany
  122.     // klucz główny
  123.     // CLIENT_NO int, -- numer klienta
  124.     // NAME varchar(20) NOT NULL, -- nazwa klienta
  125.     // SSN char(11), -- “Social Security Number” klienta
  126.     // ADDRESS VARCHAR(50) -- adres klienta
  127.     // );
  128.  
  129.     // TODO: implementacja
  130.     public void insertClients(List<Client> clients) {
  131.         // zaimplementowac wstawianie do bazy podanej listy klientow
  132.         // wykorzystac:
  133.         // * transakcje
  134.         // * polecenia prekompilowane
  135.         // * aktualizowanie wsadowe
  136.  
  137.         Connection con = null;
  138.         PreparedStatement pre_stmt = null;
  139.  
  140.         try {
  141.             String sql = "INSERT INTO CLIENTS(client_no, name, ssn, address) VALUES(?,?,?,?)";
  142.  
  143.             con = JdbcUtils.getConnection();
  144.             con.setAutoCommit(false);
  145.             // con.setTransactionIsolation(Connection.)
  146.  
  147.             pre_stmt = con.prepareStatement(sql);
  148.  
  149.             for (Client tmp : clients) {
  150.  
  151.                 pre_stmt.setInt(1, tmp.getNo());
  152.                 pre_stmt.setString(2, tmp.getName());
  153.                 pre_stmt.setString(3, tmp.getSsn());
  154.                 pre_stmt.setString(4, tmp.getAddress());
  155.                
  156.                 pre_stmt.addBatch();
  157.             }
  158.  
  159.             pre_stmt.executeBatch();
  160.             con.commit();
  161.  
  162.         } catch (SQLException ec) {
  163.             ec.printStackTrace();
  164.         } finally {
  165.             JdbcUtils.closeConnection(con);
  166.         }
  167.     }
  168.    
  169.     // TODO: implementacja
  170.     public void updateClient(Client client) {
  171.         // zaimplementowac aktualizacje danych w bazie podanego klienta
  172.         Connection con = null;
  173.         PreparedStatement pre_stmt = null;
  174.  
  175.         try {
  176.  
  177.             String sql = "UPDATE CLIENTS SET client_no = ?, name= ?, ssn = ?, address = ? WHERE id = ?";
  178.             con = JdbcUtils.getConnection();
  179.             pre_stmt = con.prepareStatement(sql);
  180.  
  181.             pre_stmt.setInt(1, client.getNo());
  182.             pre_stmt.setString(2, client.getName());
  183.             pre_stmt.setString(3, client.getSsn());
  184.             pre_stmt.setString(4, client.getAddress());
  185.             pre_stmt.setInt(5, client.getId());
  186.  
  187.             pre_stmt.executeUpdate();
  188.  
  189.         } catch (SQLException ec) {
  190.             ec.printStackTrace();
  191.         } finally {
  192.  
  193.             JdbcUtils.closeConnection(con);
  194.         }
  195.  
  196.     }
  197.  
  198.     // TODO: implementacja
  199.     public void deleteClient(int id) {
  200.         Connection con = null;
  201.         PreparedStatement pre_stmt = null;
  202.  
  203.         try {
  204.             String sql = "DELETE FROM `CLIENTS` WHERE id = ?";
  205.             con = JdbcUtils.getConnection();
  206.  
  207.             pre_stmt = con.prepareStatement(sql);
  208.             pre_stmt.setInt(1, id);
  209.  
  210.             pre_stmt.execute();
  211.  
  212.         } catch (SQLException ec) {
  213.             ec.printStackTrace();
  214.         } finally {
  215.             JdbcUtils.closeConnection(con);
  216.         }
  217.  
  218.     }
  219.  
  220.     public void deleteClient(Client client) {
  221.         deleteClient(client.getId());
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement