2014-01-05 65 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