Advertisement
Guest User

Untitled

a guest
Apr 16th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package modelo.conexion;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Connect
  6. {
  7. private Connection conn;
  8. private String host;
  9. private String db;
  10. private String username;
  11. private String password;
  12.  
  13. private static Connect connect;
  14.  
  15. public Connect()
  16. {
  17. host = "localhost";
  18. db = "conacad";
  19. username = "root";
  20. password = "Inside09";
  21. try //regresa un objeto del tipo especificado, com.mysql.jdbc.Driver es la clase que implementa java.sql.Driver
  22. {
  23. Class.forName("com.mysql.jdbc.Driver").newInstance();
  24. //Intentamos conectarnos a la base de Datos en este caso una base llamada temp
  25. System.out.println("Conectando a la base...");
  26. String url ="jdbc:mysql://"+host+"/"+db;
  27. conn = DriverManager.getConnection(url, username, password);
  28. System.out.println("Conexion a BD establecida");
  29. } catch(SQLException ex) {
  30. System.out.println("Error de mysql"+ex.getMessage());
  31. } catch (ClassNotFoundException e) {
  32. e.printStackTrace();
  33. } catch(Exception e) {
  34. System.out.println("Se produjo un error inesperado: "+e.getMessage());
  35.  
  36. }
  37.  
  38. connect = this;
  39. }
  40.  
  41. public Connect(String host, String db, String username, String password) throws ClassNotFoundException, SQLException
  42. {
  43. Class.forName("com.mysql.jdbc.Driver");
  44. this.host = host;
  45. this.db = db;
  46. this.username = username;
  47. this.password = password;
  48. conn = DriverManager.getConnection ("jdbc:mysql://" + host +"/"+db,username,password);
  49. connect = this;
  50. }
  51.  
  52. public String getHost() {
  53. return host;
  54. }
  55.  
  56. public void setHost(String host) {
  57. this.host = host;
  58. }
  59.  
  60. public String getDb() {
  61. return db;
  62. }
  63.  
  64. public void setDb(String db) {
  65. this.db = db;
  66. }
  67.  
  68. public String getUsername() {
  69. return username;
  70. }
  71.  
  72. public void setUsername(String username) {
  73. this.username = username;
  74. }
  75.  
  76. public String getPassword() {
  77. return password;
  78. }
  79.  
  80. public void setPassword(String password) {
  81. this.password = password;
  82. }
  83.  
  84. public Connection connection() {
  85. try
  86. {
  87. return conn;
  88. }
  89. finally
  90. {
  91.  
  92. }
  93. }
  94.  
  95. public void close() {
  96. try
  97. {
  98. conn.close();
  99. }
  100. catch (SQLException e)
  101. {
  102. System.err.println ("Error: " + e.getMessage () + "\n" + e.getErrorCode ());
  103. }
  104. }
  105.  
  106. public static Connect getConnect() {
  107. return connect;
  108. }
  109.  
  110. public static void setConnect(Connect connect) {
  111. Connect.connect = connect;
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement