2014-01-05 49 views
1

我已经在我的网站上使用cPanel和phpMyAdmin在远程主机上创建了MySQL数据库,现在我想连接到这个数据库我简单的java程序。我试试这个代码:使用Netbeans连接到远程mySQL(cPanel,phpMyAdmin)

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" 

甲建议表示赞赏。

回答

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