【发布时间】:2019-04-25 18:26:42
【问题描述】:
我有一个 java 程序,它从 MySQL 获取它的信息,当我使用 localhost 连接到它时它工作正常,但每当我将 ipaddress 放入它时它就不起作用。 我的连接代码和异常如下。
package connection;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author rpsal
*/
public class DBConnection {
public static Connection connect()//working on local host
{
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://"+getIpAddress()+"/ChatMaster";
conn = DriverManager.getConnection(url, "root", "");
} catch (Exception e) {
System.out.println("Exception in connect" + e);
}
return conn;
}
public static String getIpAddress() throws Exception {
InetAddress inetAddress = InetAddress.getLocalHost();
return inetAddress.getHostAddress();
}
}
当我使用String url = "jdbc:mysql:///ChatMaster"; 时,它工作正常。
我得到的异常如下。
Exception in connectjava.sql.SQLException: null, message from server: "Host 'Rp-Salh' is not allowed to connect to this MySQL server"
【问题讨论】:
-
我认为 SQL Server 已配置,所有来自外部(本地主机除外)的请求都会被阻止。
-
我需要它在我网络上的任何系统上工作,这是主要问题。
-
您需要更改数据库配置。
标签: java mysql localhost ip-address