Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. package fr.leansys.business;
  2.  
  3. import fr.leansys.model.ShopItem;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6.  
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class ShopItemServiceImpl implements ShopItemService {
  12. private static final Logger log = LoggerFactory.getLogger(ShopItemServiceImpl.class);
  13.  
  14. private static final String DRIVER_NAME = "org.h2.Driver";
  15. private static final String CONNECT_URL = "jdbc:h2:tcp://localhost/~/formation";
  16. private static final String USERNAME = "sa";
  17. private static final String PASSWORD = "sa";
  18.  
  19. /**
  20. * Create a database connection
  21. * @return Connection
  22. * @throws ClassNotFoundException
  23. * @throws SQLException
  24. */
  25. private Connection getConnection() throws ClassNotFoundException, SQLException {
  26. log.debug("Creating connection with user {}", USERNAME);
  27. Class.forName(DRIVER_NAME);
  28. return DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
  29. }
  30.  
  31. /**
  32. * Delete record
  33. * @param id
  34. * @return
  35. */
  36. public boolean delete(int id) {
  37. long before = System.currentTimeMillis();
  38.  
  39. log.info("Entering Delete Method");
  40.  
  41. boolean result = false;
  42. Connection cnx = null;
  43. PreparedStatement statement = null;
  44.  
  45. try {
  46. cnx = getConnection();
  47.  
  48. //Turn off AutoCommit
  49. cnx.setAutoCommit(false);
  50.  
  51. //Create and execute the SQL statement
  52. String sqlOrder = "DELETE FROM SHOPPINGITEMS WHERE ITEMID = ?";
  53.  
  54. //Create the Statement object
  55. statement = cnx.prepareStatement(sqlOrder);
  56. statement.setInt(1, id);
  57. result = (statement.executeUpdate() == 1);
  58.  
  59. cnx.commit();
  60. } catch (ClassNotFoundException e) {
  61. log.error("Error creating class: {}", e.getMessage());
  62. } catch (SQLException e) {
  63. log.debug("Error creating connection: {}", e.getMessage());
  64. try {
  65. cnx.rollback();
  66. } catch (Exception ex) {
  67. log.error("Can't rollback transaction.");
  68. }
  69. } finally {
  70. log.debug("Closing connections...");
  71. try {
  72. statement.close();
  73. } catch (Exception e1) {
  74. log.error("Can't close statement.");
  75. }
  76. try {
  77. cnx.close();
  78. } catch (Exception e) {
  79. log.error("Can't close connection.");
  80. }
  81. }
  82.  
  83. log.debug("Result: {}", result);
  84. log.debug("Duration: {} ms", (System.currentTimeMillis() - before));
  85.  
  86. return result;
  87. }
  88.  
  89. /**
  90. * Save record
  91. * @param shopItem
  92. * @return
  93. */
  94. public int save(ShopItem shopItem) {
  95. long before = System.currentTimeMillis();
  96.  
  97. log.info("Entering Save Method");
  98.  
  99. Connection cnx = null;
  100. PreparedStatement statement = null;
  101. Statement statementForId;
  102. ResultSet resultSet = null;
  103.  
  104. int results = 0;
  105.  
  106. try {
  107. cnx = getConnection();
  108.  
  109. //Turn off AutoCommit
  110. cnx.setAutoCommit(false);
  111.  
  112. //Create and execute the SQL statement
  113. String sqlOrder = "INSERT INTO SHOPPINGITEMS (NAME, DESCRIPTION, PRICE) VALUES (?, ?, ?)";
  114.  
  115. //Create the Statement object
  116. statement = cnx.prepareStatement(sqlOrder);
  117. statement.setString(1, shopItem.getName());
  118. statement.setString(2, shopItem.getDescription());
  119. statement.setInt(3, shopItem.getPrice());
  120.  
  121. results = statement.executeUpdate();
  122. assert results == 1; // process error
  123.  
  124. // Retrieve our Id from database
  125. statementForId = cnx.createStatement();
  126. resultSet = statementForId.executeQuery("SELECT LAST_INSERT_ID()");
  127.  
  128. if (resultSet.next()) {
  129. shopItem.setId(resultSet.getInt(1));
  130. log.debug("Inserted item with id {}", shopItem.getId(1));
  131. } // process error
  132.  
  133. cnx.commit();
  134. } catch (ClassNotFoundException e) {
  135. log.error("Error creating class: {}", e.getMessage());
  136. } catch (SQLException e) {
  137. log.debug("Error creating connection: {}", e.getMessage());
  138. try {
  139. cnx.rollback();
  140. } catch (Exception ex) {
  141. log.error("Can't rollback transaction.");
  142. }
  143. } finally {
  144. log.debug("Closing connections...");
  145. try {
  146. if (resultSet != null) resultSet.close();
  147. } catch (Exception e1) {
  148. log.error("Can't close resultset.");
  149. }
  150. try {
  151. statement.close();
  152. } catch (Exception e1) {
  153. log.error("Can't close statement.");
  154. }
  155. try {
  156. cnx.close();
  157. } catch (Exception e) {
  158. log.error("Can't close connection.");
  159. }
  160. }
  161.  
  162. log.debug("Results: {}", results);
  163. log.debug("Duration: {} ms", (System.currentTimeMillis() - before));
  164.  
  165. return shopItem.getId(1);
  166. }
  167.  
  168. /**
  169. * Get all records
  170. * @return
  171. */
  172. public List<ShopItem> getAll() {
  173. long before = System.currentTimeMillis();
  174.  
  175. log.info("Entering List Method");
  176.  
  177. Connection cnx = null;
  178. Statement statement = null;
  179. ResultSet resultSet = null;
  180. List<ShopItem> results = new ArrayList<>();
  181.  
  182. try {
  183. cnx = getConnection();
  184.  
  185. // Turn off AutoCommit
  186. cnx.setAutoCommit(false);
  187.  
  188. // Create the Statement object
  189. statement = cnx.createStatement();
  190.  
  191. // Create and execute the SQL statement
  192. String sqlOrder = "SELECT ITEMID, NAME, DESCRIPTION, PRICE FROM SHOPPINGITEMS";
  193.  
  194. resultSet = statement.executeQuery(sqlOrder);
  195.  
  196. // Iterate over ResultSet to build Items List
  197. ShopItem item;
  198. while (resultSet.next()) {
  199. item = new ShopItem();
  200. item.setId(resultSet.getInt("ITEMID"));
  201. item.setName(resultSet.getString("NAME"));
  202. item.setDescription(resultSet.getString("DESCRIPTION"));
  203. item.setPrice(resultSet.getInt("PRICE"));
  204.  
  205. results.add(item);
  206. }
  207. } catch (ClassNotFoundException e) {
  208. log.debug("Error creating class: {}", e.getMessage());
  209. } catch (SQLException e) {
  210. log.error("Error SQL: {}", e.getMessage());
  211. } finally {
  212. log.debug("Closing connections...");
  213. try {
  214. if (resultSet != null) resultSet.close();
  215. } catch (Exception e1) {
  216. log.error("Can't close resultset.");
  217. }
  218. try {
  219. if (statement != null) statement.close();
  220. } catch (Exception e1) {
  221. log.error("Can't close statement.");
  222. }
  223. try {
  224. if (cnx != null) cnx.close();
  225. } catch (Exception e) {
  226. log.error("Can't close connection.");
  227. }
  228. }
  229.  
  230. log.debug("Results: {}", results.size());
  231. log.debug("Duration: {} ms", (System.currentTimeMillis() - before));
  232.  
  233. return results;
  234. }
  235.  
  236.  
  237.  
  238. /**
  239. * Get byId records
  240. * @return
  241. */
  242. public boolean getById(int id) {
  243. long before = System.currentTimeMillis();
  244.  
  245. log.info("Entering getById Method");
  246.  
  247. boolean result = false;
  248. Connection cnx = null;
  249. PreparedStatement statement = null;
  250.  
  251. try {
  252. cnx = getConnection();
  253.  
  254. //Turn off AutoCommit
  255. cnx.setAutoCommit(false);
  256.  
  257. //Create and execute the SQL statement
  258. String sqlOrder = "SELECT NAME, DESCRIPTION, PRICE FROM SHOPPINGITEMS where ITEMID = ?";
  259.  
  260. //Create the Statement object
  261. statement = cnx.prepareStatement(sqlOrder);
  262. statement.setInt(1, id);
  263.  
  264.  
  265. cnx.commit();
  266. } catch (ClassNotFoundException e) {
  267. log.error("Error creating class: {}", e.getMessage());
  268. } catch (SQLException e) {
  269. log.debug("Error creating connection: {}", e.getMessage());
  270. try {
  271. cnx.rollback();
  272. } catch (Exception ex) {
  273. log.error("Can't rollback transaction.");
  274. }
  275. } finally {
  276. log.debug("Closing connections...");
  277. try {
  278. statement.close();
  279. } catch (Exception e1) {
  280. log.error("Can't close statement.");
  281. }
  282. try {
  283. cnx.close();
  284. } catch (Exception e) {
  285. log.error("Can't close connection.");
  286. }
  287. }
  288.  
  289. log.debug("Result: {}", result);
  290. log.debug("Duration: {} ms", (System.currentTimeMillis() - before));
  291.  
  292. return result;
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement