Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 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. import pl.polsl.zti.db1.domain.Client;
  11. import pl.polsl.zti.db1.util.JdbcUtils;
  12.  
  13. public class ClientDaoImplJdbc implements ClientDao {
  14.  
  15.  
  16. public Client getClient(int id) {
  17. Client client = null;
  18. Connection con = null;
  19. PreparedStatement preparedStatement = null;
  20. try {
  21. con = JdbcUtils.getConnection();
  22. preparedStatement = con.prepareStatement("SELECT * FROM CLIENTS WHERE id = ?");
  23. preparedStatement.setInt(1, id);
  24.  
  25. final ResultSet resultSet = preparedStatement.executeQuery();
  26. if (resultSet.next()) {
  27. client = readClient(resultSet);
  28. }
  29. } catch (SQLException ex) {
  30. JdbcUtils.handleSqlException(ex);
  31. } finally {
  32. JdbcUtils.closeSilently(preparedStatement, con);
  33. }
  34.  
  35. return client;
  36. }
  37.  
  38.  
  39. public List<Client> getClients() {
  40. final List<Client> clients = new ArrayList<Client>();
  41.  
  42. Connection con = null;
  43. Statement stmt = null;
  44. try {
  45. con = JdbcUtils.getConnection();
  46. stmt = con.createStatement();
  47. final ResultSet resultSet = stmt.executeQuery("SELECT ID, CLIENT_NO, NAME, SSN, ADDRESS FROM CLIENTS");
  48. while (resultSet.next()) {
  49. final Client client = readClient(resultSet);
  50. clients.add(client);
  51. }
  52. } catch (SQLException ex) {
  53. JdbcUtils.handleSqlException(ex);
  54. } finally {
  55. JdbcUtils.closeSilently(stmt, con);
  56. }
  57.  
  58. return clients;
  59. }
  60.  
  61.  
  62. public List<Client> getClients(String name) {
  63.  
  64.  
  65. final List<Client> clients = new ArrayList<Client>();
  66.  
  67. Connection con = null;
  68. Statement stmt = null;
  69. String command;
  70. if (name == null)
  71. command = "SELECT ID, CLIENT_NO, NAME, SSN, ADDRESS FROM CLIENTS";
  72. else
  73. command = "SELECT ID, CLIENT_NO, NAME, SSN, ADDRESS FROM CLIENTS WHERE NAME LIKE '" + name + "'";
  74. try {
  75. con = JdbcUtils.getConnection();
  76. stmt = con.createStatement();
  77. final ResultSet resultSet = stmt.executeQuery(command);
  78. while (resultSet.next()) {
  79. final Client client = readClient(resultSet);
  80. clients.add(client);
  81. }
  82. } catch (SQLException ex) {
  83. JdbcUtils.handleSqlException(ex);
  84. } finally {
  85. JdbcUtils.closeSilently(stmt, con);
  86. }
  87.  
  88. return clients;
  89. }
  90.  
  91. private Client readClient(final ResultSet rs) throws SQLException {
  92. final Client client = new Client();
  93. client.setId(rs.getInt("ID"));
  94. client.setNo(rs.getInt("CLIENT_NO"));
  95. client.setName(rs.getString("NAME"));
  96. client.setSsn(rs.getString("SSN"));
  97. client.setAddress(rs.getString("ADDRESS"));
  98. return client;
  99. }
  100.  
  101.  
  102. public void insertClients(List<Client> clients) {
  103. Connection con = null;
  104. PreparedStatement pstmt = null;
  105. try {
  106. con = JdbcUtils.getConnection();
  107. pstmt = con.prepareStatement("INSERT INTO clients VALUES (?,?,?,?,?)");
  108. for (Client cln : clients) {
  109. con.setAutoCommit(false);
  110. pstmt.setInt(1, cln.getId());
  111. pstmt.setInt(2, cln.getNo());
  112. pstmt.setString(3, cln.getName());
  113. pstmt.setString(4, cln.getSsn());
  114. pstmt.setString(5, cln.getAddress());
  115. pstmt.executeUpdate();
  116. con.commit();
  117. }
  118. } catch (SQLException ex) {
  119. JdbcUtils.handleSqlException(ex);
  120. } finally {
  121. JdbcUtils.closeSilently(pstmt, con);
  122. }
  123. }
  124.  
  125.  
  126. public void updateClient(Client client) {
  127.  
  128.  
  129. Connection con = null;
  130. PreparedStatement pstmt = null;
  131. try {
  132. con = JdbcUtils.getConnection();
  133. pstmt = con.prepareStatement("UPDATE clients SET CLIENT_NO=?, NAME=?, SSN=?, ADDRESS=? WHERE ID=?");
  134.  
  135. pstmt.setInt(1, client.getNo());
  136. pstmt.setString(2, client.getName());
  137. pstmt.setString(3, client.getSsn());
  138. pstmt.setString(4, client.getAddress());
  139. pstmt.setInt(5, client.getId());
  140. pstmt.executeUpdate();
  141.  
  142. } catch (SQLException ex) {
  143. JdbcUtils.handleSqlException(ex);
  144. } finally {
  145. JdbcUtils.closeSilently(pstmt, con);
  146. }
  147. }
  148.  
  149.  
  150. public void deleteClient(int id) {
  151. Connection con = null;
  152. Statement stmt = null;
  153. try {
  154. con = JdbcUtils.getConnection();
  155. stmt = con.createStatement();
  156. stmt.executeUpdate("DELETE FROM CLIENTS WHERE id = " + id);
  157. } catch (SQLException ex) {
  158. JdbcUtils.handleSqlException(ex);
  159. } finally {
  160. JdbcUtils.closeSilently(stmt, con);
  161. }
  162. }
  163.  
  164.  
  165. public void deleteClient(Client client) {
  166. deleteClient(client.getId());
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement