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.EletricBicycle;
- 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");
- int id_battery = rSet.getInt("id_battery");
- String type = getBicycleType(id_bicycle, type_bicycle);
- if (type.equalsIgnoreCase("moutain") || type.equalsIgnoreCase("road")) {
- return new Bicycle(id_bicycle, type_bicycle);
- } else {
- return new EletricBicycle(id_bicycle, type_bicycle, id_battery);
- }
- }
- } 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) {
- addBicycle(bicycle.getId_bicycle(), bicycle.getType(), "Y");
- }
- public void addEletricBicycle(EletricBicycle bicycle) {
- String type = getBicycleType(bicycle.getId_bicycle(), bicycle.getType());
- if (type.equalsIgnoreCase("Eletric")) {
- addEletricBicycle(bicycle.getId_bicycle(), bicycle.getType(), bicycle.getBattery(), "Y");
- }
- }
- private void addBicycle(int id_bicycle, int bicycle_type_id, String available) {
- try {
- openConnection();
- CallableStatement callStmt = getConnection().prepareCall("{ call addBicycle(?,?,?) }");
- callStmt.setInt(1, id_bicycle);
- callStmt.setInt(2, bicycle_type_id);
- callStmt.setString(3, available);
- callStmt.execute();
- closeAll();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private void addEletricBicycle(int id_bicycle, int bicycle_type_id, int id_battery, String available) {
- try {
- openConnection();
- CallableStatement callStmt = getConnection().prepareCall("{ call addEletricBicycle(?,?,?,?) }");
- callStmt.setInt(1, id_bicycle);
- callStmt.setInt(2, bicycle_type_id);
- callStmt.setInt(3, id_battery);
- callStmt.setString(4, available);
- callStmt.execute();
- closeAll();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement