Guest User

Untitled

a guest
Dec 3rd, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. package Conexion;
  2. import java.sql.*;
  3.  
  4. public class Conexion {
  5. protected Connection conexion;
  6. protected final String URL_DB= "jdbc:mysql://localhost:80/nombre_base_de_datos";
  7. protected final String JDBC_DRIVER ="com.mysql.jdbc.Driver";
  8. protected final String CONSTRASENA_DB="contrasena";
  9. protected final String USUARIO_DB="usuario_BD";
  10.  
  11. public Conexion() {
  12. }
  13. public Connection conexion(){
  14. return this.conexion;
  15. }
  16. public void conectar(){
  17.  
  18. try{
  19. conexion= DriverManager.getConnection(URL_DB, USUARIO_DB,CONSTRASENA_DB);
  20. Class.forName(JDBC_DRIVER);
  21. }catch (SQLException e){
  22.  
  23. e.getMessage();
  24. }catch(ClassNotFoundException e){
  25. System.out.println("Mensaje de error de base de datos" + e.getMessage());
  26.  
  27. }
  28. }
  29.  
  30. public void desconectar(){
  31. try{
  32. if(conexion!=null){
  33. if(conexion.isClosed()){
  34. conexion.close();
  35. }
  36. }
  37. }catch(SQLException e){
  38. System.out.println(e.getMessage());
  39. }
  40. }
  41.  
  42. }
  43.  
  44. package DAOSIMP;
  45.  
  46. import Clases.Persona;
  47. import Conexion.Conexion;
  48. import DAOS.DaoPersona;
  49. import java.sql.PreparedStatement;
  50. import java.sql.ResultSet;
  51. import java.sql.SQLException;
  52. import java.util.ArrayList;
  53.  
  54. public class DaoPersonaImp extends Conexion implements DaoPersona{
  55.  
  56. @Override
  57. public long consultarCantidadAspirantes() {
  58. ResultSet rs;
  59. long cantidad=0;
  60. try {
  61. this.conectar();
  62. String sql="SELECT COUNT(*) FROM persona WHERE tipo_persona='A';";
  63.  
  64. PreparedStatement stm=this.conexion.prepareStatement(sql);
  65. rs = stm.executeQuery();
  66. while(rs.next()){
  67. cantidad=rs.getLong(1);
  68. }
  69. return cantidad;
  70. }catch(SQLException e){
  71. System.out.println(e.getMessage());
  72. return 0;
  73. }finally{
  74. this.desconectar();
  75. }
  76. }
  77. }
  78.  
  79. DaoPersona daoPersona=new DaoPersonaImp();
  80. long cantidadAspirantes=daoPersona.consultarCantidadAspirantes();
Add Comment
Please, Sign In to add comment