2013-10-01 44 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;