Advertisement
JFLORES99

Untitled

Dec 14th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package lapr.project.data;
  7.  
  8. import java.sql.CallableStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import lapr.project.model.Bicycle;
  14. import lapr.project.model.EletricBicycle;
  15. import oracle.jdbc.OracleTypes;
  16.  
  17. /**
  18. *
  19. * @author joaoflores
  20. */
  21. public class BicycleDB extends DataHandler {
  22.  
  23. /**
  24. * Returns the park by its id
  25. *
  26. * @param id
  27. * @return the park or null
  28. */
  29. public Bicycle getBicycle(int id) {
  30.  
  31. /* Objeto "callStmt" para invocar a função "getBicycle" armazenada na BD.
  32. *
  33. * FUNCTION getBicycle(id NUMBER) RETURN pkgBicycle.ref_cursor
  34. * PACKAGE pkgSailors AS TYPE ref_cursor IS REF CURSOR; END pkgSailors;
  35. */
  36. CallableStatement callStmt = null;
  37. try {
  38. callStmt = getConnection().prepareCall("{ ? = call getBicycle(?) }");
  39.  
  40. // Regista o tipo de dados SQL para interpretar o resultado obtido.
  41. callStmt.registerOutParameter(1, OracleTypes.CURSOR);
  42. // Especifica o parâmetro de entrada da função "getPark".
  43. callStmt.setInt(2, id);
  44.  
  45. // Executa a invocação da função "getBycycle()".
  46. callStmt.execute();
  47.  
  48. // Guarda o cursor retornado num objeto "ResultSet".
  49. ResultSet rSet = (ResultSet) callStmt.getObject(1);
  50.  
  51. if (rSet.next()) {
  52.  
  53. int id_bicycle = rSet.getInt("id_bicycle");
  54. int type_bicycle = rSet.getInt("id_bibycle_type");
  55. int id_battery = rSet.getInt("id_battery");
  56.  
  57. String type = getBicycleType(id_bicycle, type_bicycle);
  58.  
  59. if (type.equalsIgnoreCase("moutain") || type.equalsIgnoreCase("road")) {
  60. return new Bicycle(id_bicycle, type_bicycle);
  61. } else {
  62. return new EletricBicycle(id_bicycle, type_bicycle, id_battery);
  63.  
  64. }
  65. }
  66.  
  67. } catch (SQLException e) {
  68. e.printStackTrace();
  69. }
  70. throw new IllegalArgumentException("No Bicycle with ID:" + id);
  71. }
  72.  
  73. public String getBicycleType(int id_bicycle, int type_bicycle) {
  74. CallableStatement callStmt = null;
  75. try {
  76. callStmt = getConnection().prepareCall("{ ? = call getBicycleType(?,?) }");
  77.  
  78. // Regista o tipo de dados SQL para interpretar o resultado obtido.
  79. callStmt.registerOutParameter(1, OracleTypes.VARCHAR);
  80. // Especifica o parâmetro de entrada da função "getBicycleType".
  81. callStmt.setInt(1, id_bicycle);
  82. callStmt.setInt(2, type_bicycle);
  83.  
  84. // Executa a invocação da função "getBicycleType".
  85. callStmt.execute();
  86.  
  87. // Guarda o varchar retornado num objeto "String".
  88. String rSet = (String) callStmt.getObject(1);
  89. return rSet;
  90.  
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. }
  94. throw new IllegalArgumentException("No bicicle with ID:" + id_bicycle + " and type" + type_bicycle);
  95. }
  96.  
  97. /**
  98. *
  99. * @param bicycle
  100. */
  101. public void addBicycle(Bicycle bicycle) {
  102. addBicycle(bicycle.getId_bicycle(), bicycle.getType(), "Y");
  103.  
  104. }
  105.  
  106. public void addEletricBicycle(EletricBicycle bicycle) {
  107. String type = getBicycleType(bicycle.getId_bicycle(), bicycle.getType());
  108. if (type.equalsIgnoreCase("Eletric")) {
  109. addEletricBicycle(bicycle.getId_bicycle(), bicycle.getType(), bicycle.getBattery(), "Y");
  110. }
  111. }
  112.  
  113. private void addBicycle(int id_bicycle, int bicycle_type_id, String available) {
  114. try {
  115. openConnection();
  116. CallableStatement callStmt = getConnection().prepareCall("{ call addBicycle(?,?,?) }");
  117.  
  118. callStmt.setInt(1, id_bicycle);
  119. callStmt.setInt(2, bicycle_type_id);
  120. callStmt.setString(3, available);
  121.  
  122. callStmt.execute();
  123. closeAll();
  124. } catch (SQLException e) {
  125. e.printStackTrace();
  126. }
  127. }
  128.  
  129. private void addEletricBicycle(int id_bicycle, int bicycle_type_id, int id_battery, String available) {
  130.  
  131. try {
  132. openConnection();
  133. CallableStatement callStmt = getConnection().prepareCall("{ call addEletricBicycle(?,?,?,?) }");
  134.  
  135. callStmt.setInt(1, id_bicycle);
  136. callStmt.setInt(2, bicycle_type_id);
  137. callStmt.setInt(3, id_battery);
  138. callStmt.setString(4, available);
  139.  
  140. callStmt.execute();
  141. closeAll();
  142. } catch (SQLException e) {
  143. e.printStackTrace();
  144. }
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement