Connect mysql from java

0

I created a free account in freehostia in which I have a mysql database which I need to use for a project I'm doing in Java.

I have already made connections to mysql databases from now but in localhost, I know the password, user, name and port of my bd but I do not know what would be the localhost's replacement, in the local phpmyadmin I see localhost : bd X but in the host name it says ip_address: bd x

What parameters should I pass?

    
asked by Julian 23.12.2017 в 23:30
source

2 answers

0

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.

    
answered by 27.12.2017 в 00:25
0

First you have to check if your hosting, accept the option of MySQL remote and if so you can click on that option to review.

I attached a link that I found in the forum of your hosting, where they tell you how to get that data:

link

    
answered by 24.12.2017 в 17:42