Created
March 1, 2018 19:42
-
-
Save parzibyte/2301998eaa2fbb06528d602d9b1b186f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
class Prueba { | |
public static void main(String[] args) { | |
try { | |
System.out.println("Verificando si has incluido correctamente el conector..."); | |
Class.forName("com.mysql.jdbc.Driver"); | |
System.out.println("Conector incluido correctamente"); | |
} catch (ClassNotFoundException e) { | |
throw new IllegalStateException("No has incluido el archivo, o escribiste mal su nombre. Error: ", e); | |
} | |
String host = "localhost"; | |
int puerto = 3306; | |
String nombreBaseDeDatos = "java"; | |
String url = "jdbc:mysql://" + host + ":" + String.valueOf(puerto) + "/" + nombreBaseDeDatos; | |
String usuario = "root"; | |
String palabraSecreta = ""; //La contraseña | |
System.out.println("Conectando a la base de datos " + nombreBaseDeDatos); | |
try (Connection conexion = DriverManager.getConnection(url, usuario, palabraSecreta)) { | |
System.out.println("Conectado correctamente a " + url); | |
} catch (SQLException e) { | |
throw new IllegalStateException("Imposible conectar a la base de datos: ", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment