Advertisement
Guest User

Untitled

a guest
May 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. package inyeccionsql;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6.  
  7. public class BD {
  8. private static Connection conexion = null;
  9. private static String host = "localhost";
  10. private static String port = "3306";
  11. private static String bd = "test";
  12. private static String user = "root";
  13. private static String password = "";
  14. // El constructor privado no permite que se generen clases
  15. private BD() {}
  16.  
  17. public static Connection getConexion() {
  18. if (BD.conexion == null) {
  19. BD.conectar();
  20. }
  21. return BD.conexion;
  22. }
  23.  
  24. private static void conectar() {
  25. try {
  26. DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  27. BD.conexion = DriverManager.getConnection ("jdbc:mysql://" + BD.host + ":" + BD.port + "/" + BD.bd,
  28. BD.user,
  29. BD.password
  30. );
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement