我已经在远程主机上使用cPanel和phpMyAdmin为我的网站创建了MySQL数据库,现在我想连接到这个数据库我的简单java程序。我试试这段代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
public static void main(String[] args) {
try{
Connection con=DriverManager.getConnection("jdbc:mysql://sfg.ge/sfgge_mysql", "my_username", "my_password");
Statement state=(Statement) con.createStatement();
String name="shota";
int id=3;
String insert = "INSERT INTO student VALUES ("+ id +",'"+ name +"')";
state.executeUpdate(insert);
}
catch(Exception ex){
}
}
}
在此代码中
DriverManager.getConnection(String url, username, pass)
SQLException抛出此类错误:
null, message from server:
"Host '188.129.228.176' is not allowed to connect to this MySQL server"
任何建议表示赞赏。
答案 0 :(得分:1)
添加新帐户:
'user1'@'exact_host' - host is specified
CREATE USER 'user1'@'exact_host'
或
'user1'@'%' - for any host.
CREATE USER 'user1'@'%'
授予您所需的权限并与新帐户建立关联。 另外看看:
Make sure that the server has not been configured to ignore network connections or (if you are attempting to connect remotely) that it has not been configured to listen only locally on its network interfaces. If the server was started with --skip-networking, it will not accept TCP/IP connections at all. If the server was started with --bind-address=127.0.0.1, it will listen for TCP/IP connections only locally on the loopback interface and will not accept remote connections