我使用 phpmyadmin(wamp) 创建了一个 MySQL 数据库。 我可以使用我的PC的IP地址从其他PC查看数据库。 但是,当我运行 Java 代码从数据库中检索条目时,它会出错。 我们已经禁用了主机PC的防火墙。这是我的代码:
/*
import java
- 要更改此模板,请选择"工具"|"模板 * 并打开 编辑器中的模板。 */包装袋;
.sql.Connection;import java.sql.DriverManager; import java.sql.ResultSet;import java.sql.SQLException;进口 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
这是因为您没有为另一台PC提供连接MySQL数据库的正确权限,如果您在Google上搜索错误,您会找到答案:
首先 ping 用户 PC 以获取他的 IP 地址:
Ping user-PC
然后登录到 MySQL 数据库并给出
用户电脑的 IP 地址
连接 MySQL 数据库的权限:
use mysql
GRANT ALL ON *.* to root@'user-PC' IDENTIFIED BY 'your-root-password';
FLUSH PRIVILEGES;
类型
GRANT ALL ON *.* to '%'@'%' WITH GRANT OPTION;
在你的phpmyadmin SQL界面中
这是因为您没有授予远程访问权限。要从远程系统访问 mysql,您必须授予权限。尝试以下方法:
GRANT ALL ON *.* to '%'@'%' WITH GRANT OPTION;
上面将提供对所有IP的访问权限,但如果您想访问家庭网络的某些特定IP,请尝试这种方式
GRANT ALL ON *.* to '%'@'192.168.%' WITH GRANT OPTION;
以下链接可能会对您有所帮助
链接1
链接2
这是因为您没有授予对 samew 网络中其他 PC 的访问权限:
GRANT ALL ON *.* to '%'@'192.168.%' WITH GRANT OPTION;
类似的问题链接:如何从远程计算机(不是本地主机)访问MySQL?