Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.polsl.zti.db1.dao;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator;
- import pl.polsl.zti.db1.domain.Client;
- import pl.polsl.zti.db1.util.JdbcUtils;
- public class ClientDaoJdbcImpl implements ClientDao {
- public Client getClient(int id) {
- Client cln = null;
- Connection con = null;
- Statement stmt = null;
- try {
- String sql = "SELECT * FROM CLIENTS WHERE id = " + id;
- con = JdbcUtils.getConnection();
- stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- if (rs.next()) {
- cln = new Client();
- cln.setId(rs.getInt("id"));
- cln.setNo(rs.getInt("client_no"));
- cln.setSsn(rs.getString("ssn"));
- cln.setName(rs.getString("name"));
- cln.setAddress(rs.getString("address"));
- }
- } catch (SQLException ec) {
- ec.printStackTrace();
- } finally {
- JdbcUtils.closeStatement(stmt);
- JdbcUtils.closeConnection(con);
- }
- return cln;
- }
- public List<Client> getClients() {
- return getClients(null, null);
- }
- // TODO: implementacja
- public List<Client> getClients(String name, String address) {
- // zaimplementowac pobieranie z bazy klientow wedlug nazwy lub/i adresu
- // znak '_' zastepuje dowolny znak we wzorcu
- // znak '%' zastepuje dowolny ciag znakow we wzorcu
- // (wykorzystac operator LIKE)
- // wartosc null w miejscu kryterium powoduje, ze nie jest ono brane pod
- // uwage,
- // przykadowo:
- // * wywolanie getClients(null, null) ma zwrocic wszystkich klientow
- // * wywolanie getClients('A%', null) ma zwrocic wszystkich klientow,
- // ktorych nazwy zaczynaja sie na litere 'A'
- // * wywolanie getClients('A%', 'B%') ma zwrocic wszystkich klientow,
- // ktorych nazwy zaczynaja sie na litere 'A' oraz adres rozpoczyna sie
- // od litery 'B'
- Connection con = null;
- Statement stmt = null;
- ArrayList<Client> listka= new ArrayList<Client>();
- ResultSet rs = null;
- try {
- String sql = "";
- // * wywolanie getClients(null, null) ma zwrocic wszystkich klientow
- if(name == null && address == null) {
- sql = "SELECT * FROM CLIENTS";
- }
- // * wywolanie getClients('A%', null) ma zwrocic wszystkich klientow, ktorych nazwy zaczynaja sie na litere 'A'
- else if(name != null && address == null) {
- sql = "SELECT * FROM CLIENTS WHERE name LIKE '" + name + "'";
- }
- else if(name != null && address != null) {
- sql = "SELECT * FROM `CLIENTS` WHERE name LIKE '" + name + "' AND address LIKE '" + address + "'";
- }
- else if(address != null && name == null)
- {
- sql = "SELECT * FROM `CLIENTS` WHERE address LIKE '" + address + "'";
- }
- else {}
- con = JdbcUtils.getConnection();
- stmt = con.createStatement();
- rs = stmt.executeQuery(sql);
- while (rs.next()) {
- Client cln = new Client();
- cln.setId(rs.getInt("id"));
- cln.setNo(rs.getInt("client_no"));
- cln.setSsn(rs.getString("ssn"));
- cln.setName(rs.getString("name"));
- cln.setAddress(rs.getString("address"));
- listka.add(cln);
- }
- } catch (SQLException ec) {
- ec.printStackTrace();
- } finally {
- JdbcUtils.closeStatement(stmt);
- JdbcUtils.closeConnection(con);
- }
- return listka;
- }
- // ID int NOT NULL AUTO_INCREMENT PRIMARY KEY, -- automatycznie generowany
- // klucz główny
- // CLIENT_NO int, -- numer klienta
- // NAME varchar(20) NOT NULL, -- nazwa klienta
- // SSN char(11), -- “Social Security Number” klienta
- // ADDRESS VARCHAR(50) -- adres klienta
- // );
- // TODO: implementacja
- public void insertClients(List<Client> clients) {
- // zaimplementowac wstawianie do bazy podanej listy klientow
- // wykorzystac:
- // * transakcje
- // * polecenia prekompilowane
- // * aktualizowanie wsadowe
- Connection con = null;
- PreparedStatement pre_stmt = null;
- try {
- String sql = "INSERT INTO CLIENTS(client_no, name, ssn, address) VALUES(?,?,?,?)";
- con = JdbcUtils.getConnection();
- con.setAutoCommit(false);
- // con.setTransactionIsolation(Connection.)
- pre_stmt = con.prepareStatement(sql);
- for (Client tmp : clients) {
- pre_stmt.setInt(1, tmp.getNo());
- pre_stmt.setString(2, tmp.getName());
- pre_stmt.setString(3, tmp.getSsn());
- pre_stmt.setString(4, tmp.getAddress());
- pre_stmt.addBatch();
- }
- pre_stmt.executeBatch();
- con.commit();
- } catch (SQLException ec) {
- ec.printStackTrace();
- } finally {
- JdbcUtils.closeConnection(con);
- }
- }
- // TODO: implementacja
- public void updateClient(Client client) {
- // zaimplementowac aktualizacje danych w bazie podanego klienta
- Connection con = null;
- PreparedStatement pre_stmt = null;
- try {
- String sql = "UPDATE CLIENTS SET client_no = ?, name= ?, ssn = ?, address = ? WHERE id = ?";
- con = JdbcUtils.getConnection();
- pre_stmt = con.prepareStatement(sql);
- pre_stmt.setInt(1, client.getNo());
- pre_stmt.setString(2, client.getName());
- pre_stmt.setString(3, client.getSsn());
- pre_stmt.setString(4, client.getAddress());
- pre_stmt.setInt(5, client.getId());
- pre_stmt.executeUpdate();
- } catch (SQLException ec) {
- ec.printStackTrace();
- } finally {
- JdbcUtils.closeConnection(con);
- }
- }
- // TODO: implementacja
- public void deleteClient(int id) {
- Connection con = null;
- PreparedStatement pre_stmt = null;
- try {
- String sql = "DELETE FROM `CLIENTS` WHERE id = ?";
- con = JdbcUtils.getConnection();
- pre_stmt = con.prepareStatement(sql);
- pre_stmt.setInt(1, id);
- pre_stmt.execute();
- } catch (SQLException ec) {
- ec.printStackTrace();
- } finally {
- JdbcUtils.closeConnection(con);
- }
- }
- public void deleteClient(Client client) {
- deleteClient(client.getId());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement