Last active
May 4, 2022 22:30
-
-
Save DavidBernalGonzalez/cfbe4ec15e3b84becbdb6d8c85290892 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.Connection; | |
import java.sql.DriverManager; | |
public class Main { | |
public static void main(String[] args) { | |
Connection connection; | |
// Ponemos la URL de la BBDD a la que queremos conectarnos mediante a JDBC | |
final String url = "jdbc:mysql://localhost:3306/db_test"; | |
// Accesos a la BBDD | |
final String user = "admin"; | |
final String pass = "admin"; | |
try { | |
// Realizando al conexión | |
connection = DriverManager.getConnection(url, user, pass); | |
// Si todo ha ido OK | |
System.out.println("Connection success."); | |
// --> Aquí continuaremos realizando el resto del operaciones de SQL | |
// Si todo ha ido OK una vez finalizado el uso de la conexión la cerraremos | |
connection.close(); | |
// Y en este caso, confirmamos que la conexión ha sido cerrado mediante a un | |
// mensaje | |
System.out.println("Connection closed."); | |
} catch (Exception e) { | |
System.err.println(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment