Guest User

Untitled

a guest
Mar 11th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. package componentes;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. // TODO: Auto-generated Javadoc
  10.  
  11.  
  12. public class ConexionBD {
  13.  
  14. /** The conexion. */
  15. static Connection conexion;
  16.  
  17. /** The estatuto. */
  18. static Statement estatuto;
  19.  
  20. /**
  21. * Metodo encargado de efectuar la conexion a nuestra base de datos.
  22. */
  23. public static void conectar() {
  24.  
  25. // Asignamos a variables todos los datos requeridos para la conexion a
  26. // la base de datos.
  27. String nombreDriver = "com.mysql.jdbc.Driver";
  28. String nombreServidor = "93.189.94.177";
  29. String numeroPuerto = "3306";
  30. String miBaseDatos = "salva";
  31. String url = "jdbc:mysql://" + nombreServidor + ":" + numeroPuerto
  32. + "/" + miBaseDatos;
  33. String dbuser = "salva";
  34. String dbpwd = "salva";
  35.  
  36. try {
  37. Class.forName(nombreDriver).newInstance();
  38. conexion = DriverManager.getConnection(url, dbuser, dbpwd);
  39. estatuto = conexion.createStatement();
  40. System.out.println("conectaBD");
  41. } catch (IllegalAccessException e) {
  42. System.err.println(e.getMessage());
  43. } catch (InstantiationException e) {
  44. System.err.println(e.getMessage());
  45. } catch (ClassNotFoundException e) {
  46. System.err.println(e.getMessage());
  47. } catch (SQLException e) {
  48. System.out.println("akipeta");
  49. System.err.println(e.getMessage());
  50. }
  51. }
  52.  
  53. /**
  54. * Metodo encargado de cerrar la conexion.
  55. */
  56. public static void desconectar() {
  57. try {
  58. estatuto.close();
  59. conexion.close();
  60. System.out.println("DesconectaBD");
  61. } catch (SQLException e) {
  62. System.err.println(e.getMessage());
  63. }
  64. }
  65.  
  66. /**
  67. * Metodo para ejecutar consultas de tipo INSERT, UPDATE, DELETE.
  68. *
  69. * @param querySQL String consulta en SQL
  70. * @throws SQLException the sQL exception
  71. */
  72. public static void executeUpdateQuery(String querySQL) throws SQLException {
  73. conectar();
  74. estatuto.executeUpdate(querySQL);
  75. desconectar();
  76. }
  77.  
  78. /**
  79. * Metodo para ejecutar consultas de tipo SELECT.
  80. *
  81. * @param querySQL String consulta en SQL
  82. * @return ResultSet tabla resultante de la consulta
  83. * @throws SQLException devuelve un error en caso de producirse
  84. */
  85. public static ResultSet executeQuery(String querySQL) throws SQLException {
  86. conectar();
  87. return estatuto.executeQuery(querySQL);
  88. }
  89. }
Add Comment
Please, Sign In to add comment