2013-10-01 43 views
0

我已經使用phpmyadmin(wamp)創建了一個mysql數據庫。我可以使用我的電腦的IP地址看到其他電腦的數據庫。然而,當我運行java代碼來檢索數據庫中的條目時,它會給出錯誤。我們已經禁用了主機的防火牆。 這裏是我的代碼:無法從phpMyAdmin(wamp)從其他電腦的MySQL數據庫中檢索輸入

/* 
  • 要改變這種模板,選擇Tools |模板*並在編輯器中打開 模板。 * /軟件包wamp;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;

/** * @author * *用戶/公共類WAMP {

/** 
* @param args the command line arguments 
*/ 

    // TODO code application logic here 
    public static void main(String args[]) throws InstantiationException, IllegalAccessException 
{ 
    try{ 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.2:3306/test","root",""); 
       Statement stmt=con.createStatement(); 
       // stmt.executeUpdate("Insert into student values(1,'abc','nagpur')"); 
       ResultSet rs= stmt.executeQuery("Select names from sample where id=15"); 
       rs.next(); 
       String name= rs.getString("names");    
       System.out.println(name); 
       System.out.println("DOne.."); 
       //INSERT INTO `student`(`id`, `name`, `address`) VALUES (1,'amol','nagpur'); 
      con.close(); 

      } 
catch(ClassNotFoundException | SQLException e){ 
    System.out.println("error"+e); 
} 

} 

} 

這裏是錯誤消息:

errorjava.sql.SQLException: null, message from server: "Host 'user-PC.Home' is not allowed to connect to this MySQL server 

回答

1

類型

GRANT ALL ON *.* to '%'@'%' WITH GRANT OPTION; 

在你的phpmyadmin sql中terface

+0

非常感謝。它確實有效。 – Amol

1

這是因爲你沒有遠程訪問權限授予要從遠程系統訪問MySQL,您必須授予權限。 請嘗試以下方法

GRANT ALL ON *.* to '%'@'%' WITH GRANT OPTION; 

以上就可以訪問所有的,但如果你想給訪問您的家庭網絡的某些特定IP然後再嘗試這種方式

GRANT ALL ON *.* to '%'@'192.168.%' WITH GRANT OPTION; 

以下鏈接可以幫助你

link1

link2

2

這是因爲你沒有給你的其他電腦的正確權限連接MySQL數據庫,如果你在谷歌搜索的錯誤,你會找到答案:

在第一個ping用戶PC,以得到他的IP地址:

Ping user-PC 

然後登錄到MySQL數據庫,並給

用戶PC的IP地址

權限連接MySQL數據庫:

use mysql 
GRANT ALL ON *.* to [email protected]'user-PC' IDENTIFIED BY 'your-root-password'; 
FLUSH PRIVILEGES;