简体   繁体   中英

unable to insert and retrive mysql server database information

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: Access denied for user 'username'@'myipaddress' (using password: YES)

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

grant <permission> on <database> to <user>@<location> identified by <password>

So here you have to use-

grant all on test.* to 'username'@'your_ipaddress' identified by 'password'

Run this command in MySQL command prompt.

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

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

grant all on *.* to '%'@'%' identified by 'password'

And finally you have to use the following command-

FLUSH PRIVILEGES;

To reload all privileges.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM