JDBC + mysql remote connection error

1

Introduction

I'm developing a java program that can change the database connection, I have 1 local bank (127.0.0.1:3306) and a remote bank (192.168.25.75:3306) that would be a my computer, I made the connection using jdbc.

connDb.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author ralfting
 */
public class ConnDb {
    public static Connection getConnection() throws SQLException{
        String conexao = "jdbc:mysql://192.168.25.75:3306/teste";
        String usuario = "root";
        String senha = "root";
        String driver = "com.mysql.jdbc.Driver";


            Class.forName(driver);
            java.sql.Connection conn = DriverManager.getConnection(conexao, usuario, senha);
            System.out.println("Conexão - OK");
            return (Connection) conn;


    }
}

JDBC Driver

Problem

WhenIusethelocalbankitworksfine,butsinceIstartusingtheremotebankitdoesnotworkitreturnsmeaSQLException

  

null,messagefromserver:"Host 'Ralfting.home' is not allowed to connect to this MySQL server"

    
asked by anonymous 15.09.2014 / 02:43

1 answer

2

Solution

It was the same problem @Kyllopardiun spoke, opening the doors and solved this I had a problem in that same code at the time of inserting, it would close connection before inserting:

try{
    Class.forName(driver);
    java.sql.Connection conn = DriverManager.getConnection(conexao, usuario, senha);
    System.out.println("Conexão - OK");
    return (Connection) conn;

}catch(SQLException e){}

Error

  

Closed Connection

This way it frees the resources allocated when creating "conn"; that is, it closes the connection with the bank. Removing try catch works correctly.

Class.forName(driver);
java.sql.Connection conn = DriverManager.getConnection(conexao, usuario, senha);
System.out.println("Conexão - OK");
return (Connection) conn;
    
17.09.2014 / 15:58