JDBC+mysql remote error while connecting

Asked

Viewed 1,107 times

1

Introducing

I am developing a program in java that can change the connection of the database, I have 1 local database (127.0.0.1:3306) and a remote database (192.168.25.75:3306) that would be a computer of mine, 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

inserir a descrição da imagem aqui

Problem

When I use the local bank works perfectly, but once I start using the remote bank it doesn’t work it returns me a SQLException

null, message from server: "Host 'Ralfting.home' is not allowed to connect to this Mysql server"

  • 5

    The error message is quite clear. You are not allowed to connect to this bank. Make sure the port is open (firewall may interfere with this) and that you can connect from your machine to that bank.

  • 1

    Some remote banks, especially those of free hosts only allow connections from pages hosted on the server itself. A possible solution for your case is to make a REST access to communicate with your local application.

  • I’m sorry, reading your question better, I realized that the remote bank is on another computer of yours, correct? In this case the comment of Kyllopardiun is more useful, because it can be some firewall or something. Anyway, we have no way to find out the cause of the error if you don’t give more details of your problem.

1 answer

2


Solution

That was the same problem @Kyllopardiun said,opening the doors and solved that I had a problem in that same code at the time of inserting, it closed 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

Thus, it frees the allocated resources when creating "Conn"; that is, it closes the connection with the bank. Removing the try catch function correctly.

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

    +1 for the solution, but mainly for having returned after 2 days to answer. : D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.