Before connecting to the db you must know your ip address this can be displayed in your control panel, you must also have enabled on your router port 3306 that is generally used for mysql and in your java class you should add the corresponding values I leave you an example in code below.
public class conexion {
Connection conexion; // inicializamos la variable conection llamada conexion
// JDBC driver nombre y base de datos URL
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";// aqui indicamos el tipo de base de datos utilizamos
final String DB_URL = "jdbc:mysql://tu_direccion_ip:3306/tu_DB?&useSSL=false"; // aqui indicamos la direccion de la base datos
// base de datos credenciales
final String USER = "tu_usuario"; // datos de usuario
final String PASS = "tu_contraseña"; // datos de contraseña}
where indicated you must enter your data then create a method that makes the connection
public void conexion_defecto() throws Exception {
try { // generamos un try-catch para controlar los errores
Class.forName(JDBC_DRIVER); // inicializamos el metodo class con la variable JDBC_DRIVER
conexion = DriverManager.getConnection(DB_URL, USER, PASS); // almacenamos el metodo getConnection en la variable conexion
} catch (Exception e) { // creamos un catch exception llamado e
throw e; // al generarse un error imprimimos el mensaje del error con el metodo getmessage
}
}
I hope you receive the information. Greetings.