Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. private Connection co;
  2.  
  3. public Conexion() {
  4. this.co = null;
  5. }
  6.  
  7. public Connection get_conection() {
  8. return this.co;
  9.  
  10. }
  11.  
  12. public boolean conectar() throws ClassNotFoundException, SQLException {
  13. /*
  14. Connection c = null;
  15. Class.forName("com.mysql.jdbc.Driver");
  16. //el metodo getConnection() hay que adaptarlo para conectarlo a mi base de daots de hostinguer
  17. c = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306//");
  18. Statement stm = c.createStatement();
  19. System.out.println("Conectado correctamente a la Base de Datos de 'League Of Leguends'");
  20. this.co = c;
  21.  
  22. */
  23. boolean conectado = false;
  24. Connection c = null;
  25. Class.forName("com.mysql.jdbc.Driver");
  26. c = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/LEAGUE_OF_LEGENDS", "miusuario", "micontraseña");
  27. Statement stm = c.createStatement();
  28. conectado = true;
  29. System.out.println("Conectado correctamente a la Base de datos : LEAGUE OF LEGUENDS");
  30.  
  31. if (conectado == true) {
  32. return true;
  33. } else {
  34. return false;
  35. }
  36.  
  37.  
  38. }
  39.  
  40. public int contar_num_personajes() throws SQLException {
  41. int n = 0;
  42. Connection dbConnection = get_conection();
  43. Statement stm = dbConnection.createStatement();
  44. // almaceno resultado de consulta en ResultSet
  45. ResultSet rs = stm.executeQuery("SELECT count(*) FROM personajes");
  46. // chequeo que el result set no sea vacío, moviendo el cursor a la
  47. // primer fila. (El cursor inicia antes de la primer fila)
  48. if (rs.next()) {
  49. //Si hay resultados obtengo el valor.
  50. n = rs.getInt(1);
  51. }
  52. // libero recursos
  53. stm.close();
  54. dbConnection.close();
  55. return n;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement