ParentescoSqlserverDAO.java 3.81 KB
Newer Older
Luis Gangas committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package trismegistoplanilla.sqlserverdao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.json.JSONArray;
import org.json.JSONObject;
import trismegistoplanilla.beans.ParentescoBean;
import trismegistoplanilla.dao.ParentescoDAO;
import trismegistoplanilla.utilities.ResponseHelper;
import trismegistoplanilla.utilities.Variables;

public class ParentescoSqlserverDAO implements ParentescoDAO {

16 17 18 19 20 21 22 23
	@Override
	public JSONObject listarParentesco() {
		System.out.println("ParentescoSqlserverDAO: listarParentesco");
		JSONObject jsonListarParentesco = null;
		JSONArray jsonArrayListarParentesco = new JSONArray();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection connection = null;
Luis Gangas committed
24

25
		ResponseHelper response = new ResponseHelper();
Luis Gangas committed
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
		try {
			connection = SqlserverDAOFactory.obtenerConexion(Variables.BD_NAME);
			String sql
				= "select "
				+ "codigo_parentesco codigoParentesco, "
				+ "nombre nombre, "
				+ "estado_registro estadoRegistro "
				+ "FROM parentesco "
				+ "where estado_registro = 1";
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				ParentescoBean parentesco = new ParentescoBean();
				parentesco.setCodigoParentesco(rs.getInt("codigoParentesco"));
				parentesco.setNombre(rs.getString("nombre"));
				parentesco.setEstado_registro(rs.getInt("estadoRegistro"));
				JSONObject jsonObjParentesco = new JSONObject(parentesco);
				jsonArrayListarParentesco.put(jsonObjParentesco);
			}
			JSONObject jsonObjParentesco = new JSONObject();
			jsonObjParentesco.put("parentescos", jsonArrayListarParentesco);
			response.setStatus(true);
			response.setMessage("Los parentescos se han listado correctamente");
			response.setData(jsonObjParentesco);
Luis Gangas committed
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
		} catch (SQLException e) {
			e.printStackTrace();
			response.setStatus(false);
			response.setMessage("Error: " + e.getMessage() + " \n Error Code: [" + e.getErrorCode() + "]");
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
Luis Gangas committed
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		jsonListarParentesco = new JSONObject(response);
		return jsonListarParentesco;
	}

	@Override
	public JSONObject validarExistenciaParentesco(ParentescoBean parentesco) {
		System.out.println("ParentescoSqlserverDAO: validarExistenciaParentesco");
		JSONObject jsonValidarExistenciaParentesco = null;
		ResponseHelper response = new ResponseHelper();
		int existeParentesco = 0;

		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connection = SqlserverDAOFactory.obtenerConexion(Variables.BD_NAME);
			String sql
				= "select "
				+ "count(1) as existeParentesco "
				+ "from parentesco "
				+ "where codigo_parentesco = ? and estado_registro = 1";
			ps = connection.prepareStatement(sql);
			ps.setInt(1, parentesco.getCodigoParentesco());
			rs = ps.executeQuery();
			rs.next();
			existeParentesco = rs.getInt("existeParentesco");
			if (existeParentesco > 0) {
				response.setStatus(true);
				response.setMessage("El parentesco seleccionado existe.");
			} else {
				response.setStatus(false);
				response.setMessage("El parentesco seleccionado no existe.");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			response.setStatus(false);
			response.setMessage("Error: " + e.getMessage() + " \n Error Code: [" + e.getErrorCode() + "]");
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		jsonValidarExistenciaParentesco = new JSONObject(response);
		return jsonValidarExistenciaParentesco;
	}
Luis Gangas committed
127 128

}