Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
Add Comment
Please, Sign In to add comment