Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package lapr.project.data;
- import java.sql.CallableStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import lapr.project.model.Bicycle;
- import lapr.project.model.EBicycle;
- import oracle.jdbc.OracleTypes;
- /**
- *
- * @author joaoflores
- */
- public class BicycleDB extends DataHandler {
- /**
- * Returns the park by its id
- *
- * @param id
- * @return the park or null
- */
- public Bicycle getBicycle(int id) {
- /* Objeto "callStmt" para invocar a função "getBicycle" armazenada na BD.
- *
- * FUNCTION getBicycle(id NUMBER) RETURN pkgBicycle.ref_cursor
- * PACKAGE pkgSailors AS TYPE ref_cursor IS REF CURSOR; END pkgSailors;
- */
- CallableStatement callStmt = null;
- try {
- callStmt = getConnection().prepareCall("{ ? = call getBicycle(?) }");
- // Regista o tipo de dados SQL para interpretar o resultado obtido.
- callStmt.registerOutParameter(1, OracleTypes.CURSOR);
- // Especifica o parâmetro de entrada da função "getPark".
- callStmt.setInt(2, id);
- // Executa a invocação da função "getBycycle()".
- callStmt.execute();
- // Guarda o cursor retornado num objeto "ResultSet".
- ResultSet rSet = (ResultSet) callStmt.getObject(1);
- if (rSet.next()) {
- int id_bicycle = rSet.getInt("id_bicycle");
- int type_bicycle = rSet.getInt("id_bibycle_type");
- String available = rSet.getString("available");
- String type = getBicycleType(id_bicycle, type_bicycle);
- if (type.equalsIgnoreCase("moutain") || type.equalsIgnoreCase("road")) {
- return new Bicycle(id_bicycle, type_bicycle, available);
- //
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- throw new IllegalArgumentException("No Bicycle with ID:" + id);
- }
- public String getBicycleType(int id_bicycle, int type_bicycle) {
- CallableStatement callStmt = null;
- try {
- callStmt = getConnection().prepareCall("{ ? = call getBicycleType(?,?) }");
- // Regista o tipo de dados SQL para interpretar o resultado obtido.
- callStmt.registerOutParameter(1, OracleTypes.VARCHAR);
- // Especifica o parâmetro de entrada da função "getBicycleType".
- callStmt.setInt(1, id_bicycle);
- callStmt.setInt(2, type_bicycle);
- // Executa a invocação da função "getBicycleType".
- callStmt.execute();
- // Guarda o varchar retornado num objeto "String".
- String rSet = (String) callStmt.getObject(1);
- return rSet;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- throw new IllegalArgumentException("No bicicle with ID:" + id_bicycle + " and type" + type_bicycle);
- }
- /**
- *
- * @param bicycle
- */
- public void addBicycle(Bicycle bicycle) {
- String type = getBicycleType(bicycle.getId_bicycle(), bicycle.getType());
- addBicycle(type,bicycle.getId_bicycle(),bicycle.getAvailable());
- }
- private void addBicycle( String bicycle_type,int id_bicycle ,String available) {
- try {
- openConnection();
- CallableStatement callStmt = getConnection().prepareCall("{ call addBicycle(?,?,?) }");
- callStmt.setString(1, bicycle_type);
- callStmt.setInt(2, id_bicycle);
- callStmt.setString(3, available);
- callStmt.execute();
- closeAll();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void addEletricBicycle(EBicycle bicycle) {
- String type = getBicycleType(bicycle.getId_bicycle(), bicycle.getType());
- if (type.equalsIgnoreCase("Eletric")) {
- addEletricBicycle(
- bicycle.getId_bicycle(),
- type,
- bicycle.getAvailable(),
- bicycle.getTension(),
- bicycle.getBattery_capacity(),
- bicycle.getBattery_remain(),
- bicycle.getMotor_power());
- }
- }
- private void addEletricBicycle(int id_bicycle, String bicycle_type, String available, double tension, double battery_capacity, double battery_remain, double motor_power) {
- try {
- openConnection();
- CallableStatement callStmt = getConnection().prepareCall("{ call addEletricBicycle(?,?,?,?) }");
- callStmt.setInt(1, id_bicycle);
- callStmt.setString(2, bicycle_type);
- callStmt.setString(3, available);
- callStmt.setDouble(4, tension);
- callStmt.setDouble(5, battery_capacity);
- callStmt.setDouble(6, battery_remain);
- callStmt.setDouble(7, motor_power);
- callStmt.execute();
- closeAll();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement