Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 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.EBicycle;
  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. String available = rSet.getString("available");
  56.  
  57. String type = getBicycleType(id_bicycle, type_bicycle);
  58.  
  59. if (type.equalsIgnoreCase("moutain") || type.equalsIgnoreCase("road")) {
  60.  
  61. return new Bicycle(id_bicycle, type_bicycle, available);
  62. //
  63. }
  64. }
  65.  
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. }
  69. throw new IllegalArgumentException("No Bicycle with ID:" + id);
  70. }
  71.  
  72. public String getBicycleType(int id_bicycle, int type_bicycle) {
  73. CallableStatement callStmt = null;
  74. try {
  75. callStmt = getConnection().prepareCall("{ ? = call getBicycleType(?,?) }");
  76.  
  77. // Regista o tipo de dados SQL para interpretar o resultado obtido.
  78. callStmt.registerOutParameter(1, OracleTypes.VARCHAR);
  79. // Especifica o parâmetro de entrada da função "getBicycleType".
  80. callStmt.setInt(1, id_bicycle);
  81. callStmt.setInt(2, type_bicycle);
  82.  
  83. // Executa a invocação da função "getBicycleType".
  84. callStmt.execute();
  85.  
  86. // Guarda o varchar retornado num objeto "String".
  87. String rSet = (String) callStmt.getObject(1);
  88. return rSet;
  89.  
  90. } catch (SQLException e) {
  91. e.printStackTrace();
  92. }
  93. throw new IllegalArgumentException("No bicicle with ID:" + id_bicycle + " and type" + type_bicycle);
  94. }
  95.  
  96. /**
  97. *
  98. * @param bicycle
  99. */
  100. public void addBicycle(Bicycle bicycle) {
  101. String type = getBicycleType(bicycle.getId_bicycle(), bicycle.getType());
  102. addBicycle(type,bicycle.getId_bicycle(),bicycle.getAvailable());
  103. }
  104.  
  105. private void addBicycle( String bicycle_type,int id_bicycle ,String available) {
  106. try {
  107. openConnection();
  108. CallableStatement callStmt = getConnection().prepareCall("{ call addBicycle(?,?,?) }");
  109.  
  110. callStmt.setString(1, bicycle_type);
  111. callStmt.setInt(2, id_bicycle);
  112. callStmt.setString(3, available);
  113.  
  114. callStmt.execute();
  115. closeAll();
  116. } catch (SQLException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120.  
  121.  
  122. public void addEletricBicycle(EBicycle bicycle) {
  123. String type = getBicycleType(bicycle.getId_bicycle(), bicycle.getType());
  124. if (type.equalsIgnoreCase("Eletric")) {
  125. addEletricBicycle(
  126. bicycle.getId_bicycle(),
  127. type,
  128. bicycle.getAvailable(),
  129. bicycle.getTension(),
  130. bicycle.getBattery_capacity(),
  131. bicycle.getBattery_remain(),
  132. bicycle.getMotor_power());
  133. }
  134. }
  135.  
  136. private void addEletricBicycle(int id_bicycle, String bicycle_type, String available, double tension, double battery_capacity, double battery_remain, double motor_power) {
  137.  
  138. try {
  139. openConnection();
  140. CallableStatement callStmt = getConnection().prepareCall("{ call addEletricBicycle(?,?,?,?) }");
  141.  
  142. callStmt.setInt(1, id_bicycle);
  143. callStmt.setString(2, bicycle_type);
  144. callStmt.setString(3, available);
  145. callStmt.setDouble(4, tension);
  146. callStmt.setDouble(5, battery_capacity);
  147. callStmt.setDouble(6, battery_remain);
  148. callStmt.setDouble(7, motor_power);
  149.  
  150. callStmt.execute();
  151. closeAll();
  152.  
  153. } catch (SQLException e) {
  154. e.printStackTrace();
  155. }
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement