无法插入和检索mysql服务器数据库信息

问题描述:

我已经在我朋友的电脑上创建了一个名为test(表名演示)的数据库。我可以使用我朋友的电脑的IP地址从我的浏览器中看到这个表。但我想使用我的电脑上的java代码将数据插入和检索到这个数据库(测试)。我试试,但netbeans显示错误信息。

i have created a database named test(table name demo) on wamp on my friend's pc.i am able to see this table from my browser using ip address of my friend's pc. but i want to insert and retrive data into this database(test) using java code from my pc. i try that but netbeans shows an error message.

here is my code :


package ashdemo;

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

public class Ashdemo {

        public static void main(String args[]) throws InstantiationException, IllegalAccessException
    {
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
                 Connection con = DriverManager.getConnection("jdbc:mysql://friend's_ipaddress:3306/test","username","password



"); 
                    Statement stmt=con.createStatement();
                  stmt.executeUpdate("Insert into demo values(1,'abc','nagpur')");
                    //ResultSet rs= stmt.executeQuery("Select name from demo where id=1");
                    //rs.next();
                   //String name= rs.getString("name");               
                    //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);
    }

    }

    }
error message is : 




errorjava.sql.SQLException:访问被拒绝用户
'用户名'@'myipaddress'(使用密码:是)

errorjava.sql.SQLException: Access denied for user 'username'@'myipaddress' (using password: YES)


您需要将mysql设置为允许特定用户的远程连接。默认语法为:

You need to set up mysql to allow remote connection for a particular user. The default syntax is:

grant< permission> on< database>到< user> @< location>由< password>

标识所以这里你必须使用 -

So here you have to use-

将所有测试。*授予'用户名'@'your_ipaddress'标识为'password'

运行此命令在MySQL命令提示符下。

Run this command in MySQL command prompt.

这将允许用户名使用密码从您的IP连接 / strong>并授予数据库中所有表的所有权限 - test

This will allow username to connect from your IP using that password and give all permissions on all tables in the database-test.


  • 要允许任何用户连接来自任何IP地址并使用任何数据库中的所有表,请使用以下语法 -

  • To allow any user to connect from any IP address and use all the tables in any database use the following syntax-

将所有*。*全部授予'%'@'%'由'password'标识

最后你必须使用以下命令 -

And finally you have to use the following command-

FLUSH PRIVILEGES;

重新加载所有权限。