Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Conexion;
- import java.sql.*;
- public class Conexion {
- protected Connection conexion;
- protected final String URL_DB= "jdbc:mysql://localhost:80/nombre_base_de_datos";
- protected final String JDBC_DRIVER ="com.mysql.jdbc.Driver";
- protected final String CONSTRASENA_DB="contrasena";
- protected final String USUARIO_DB="usuario_BD";
- public Conexion() {
- }
- public Connection conexion(){
- return this.conexion;
- }
- public void conectar(){
- try{
- conexion= DriverManager.getConnection(URL_DB, USUARIO_DB,CONSTRASENA_DB);
- Class.forName(JDBC_DRIVER);
- }catch (SQLException e){
- e.getMessage();
- }catch(ClassNotFoundException e){
- System.out.println("Mensaje de error de base de datos" + e.getMessage());
- }
- }
- public void desconectar(){
- try{
- if(conexion!=null){
- if(conexion.isClosed()){
- conexion.close();
- }
- }
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }
- }
- }
- package DAOSIMP;
- import Clases.Persona;
- import Conexion.Conexion;
- import DAOS.DaoPersona;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- public class DaoPersonaImp extends Conexion implements DaoPersona{
- @Override
- public long consultarCantidadAspirantes() {
- ResultSet rs;
- long cantidad=0;
- try {
- this.conectar();
- String sql="SELECT COUNT(*) FROM persona WHERE tipo_persona='A';";
- PreparedStatement stm=this.conexion.prepareStatement(sql);
- rs = stm.executeQuery();
- while(rs.next()){
- cantidad=rs.getLong(1);
- }
- return cantidad;
- }catch(SQLException e){
- System.out.println(e.getMessage());
- return 0;
- }finally{
- this.desconectar();
- }
- }
- }
- DaoPersona daoPersona=new DaoPersonaImp();
- long cantidadAspirantes=daoPersona.consultarCantidadAspirantes();
Add Comment
Please, Sign In to add comment