Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 1, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/2301998eaa2fbb06528d602d9b1b186f to your computer and use it in GitHub Desktop.
Save parzibyte/2301998eaa2fbb06528d602d9b1b186f to your computer and use it in GitHub Desktop.
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