Advertisement
Guest User

Untitled

a guest
Sep 12th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. package conexion;
  2.  
  3. import com.mysql.jdbc.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9.  
  10.  
  11. public class conexion {
  12. Connection co;
  13. Statement stm;
  14.  
  15. //jdbc:mysql://localhost:3306/java?zeroDateTimeBehavior=convertToNull [root on Default schema]
  16.  
  17. public static Connection iniciarConnection(){
  18. try {
  19.  
  20. Class.forName("com.mysql.jdbc.Driver");
  21. Connection co= DriverManager.getConnection("jdbc:mysql//localhost:3306/java", "root", "");
  22. Statement stm = co.createStatement();
  23. System.out.println("Conectado correctamente a la Base de Datos");
  24. return co;
  25. } catch (ClassNotFoundException e) {
  26. System.out.println("Clase no encontrada: "+e);
  27. } catch (SQLException e) {
  28. System.out.println("Error de conexion: "+e);
  29. } catch (Exception e) {
  30. System.out.println("Error desconocido: "+e);
  31. }
  32. return null;
  33. }
  34. }
  35.  
  36. Connection co= DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root", "");
  37.  
  38. // 2.- Creo un objeto conexión.
  39. Connection conexion = DriverManager.getConnection
  40. ("jcbc:mysql://localhost:3306/curso_php_2",
  41. "root", "");
  42.  
  43. // 3.- Creo un objeto Statement a partir del objeto Connection
  44. Statement statement = conexion.createStatement();
  45.  
  46. // 4.- Ejecuto sentencia SQL
  47. ResultSet resultado = statement.executeQuery(query);
  48. while (resultado.next())
  49. {
  50. System.out.println(resultado.getString("USUARIO") + " " +
  51. resultado.getString("CONTRASENIA") + " " +
  52. resultado.getString("DIRECCION"));
  53. }
  54.  
  55. // Libero el resultset
  56. resultado.close();
  57.  
  58. // Cierro la conexion conexion.close();
  59. } catch (ClassNotFoundException e)
  60. {
  61. JOptionPane.showMessageDialog (null,
  62. "Imposible cargar el driver MySQL", "Error",
  63. JOptionPane.ERROR_MESSAGE);
  64. }
  65. catch (SQLException e)
  66. {
  67. String mensajeError = e.getMessage();
  68. JOptionPane.showMessageDialog (null, mensajeError, "Error",
  69. JOptionPane.ERROR_MESSAGE);
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement