Guest User

Untitled

a guest
Mar 11th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. class Prueba {
  4.  
  5. public static void main(String[] args) {
  6. try {
  7. System.out.println("Verificando si has incluido correctamente el conector...");
  8. Class.forName("com.mysql.jdbc.Driver");
  9. System.out.println("Conector incluido correctamente");
  10. } catch (ClassNotFoundException e) {
  11. throw new IllegalStateException("No has incluido el archivo, o escribiste mal su nombre. Error: ", e);
  12. }
  13.  
  14. String host = "localhost";
  15. int puerto = 3306;
  16. String nombreBaseDeDatos = "java";
  17.  
  18. String url = "jdbc:mysql://" + host + ":" + String.valueOf(puerto) + "/" + nombreBaseDeDatos;
  19.  
  20. String usuario = "root";
  21. String palabraSecreta = ""; //La contraseña
  22.  
  23. System.out.println("Conectando a la base de datos " + nombreBaseDeDatos);
  24.  
  25. try (Connection conexion = DriverManager.getConnection(url, usuario, palabraSecreta)) {
  26. System.out.println("Conectado correctamente a " + url);
  27. } catch (SQLException e) {
  28. throw new IllegalStateException("Imposible conectar a la base de datos: ", e);
  29. }
  30. }
  31. }
Add Comment
Please, Sign In to add comment