0

I'm developing an android native module in a react native app. I'm using JDBC to directly connect with a mysql database hosted on my pc. My Phone and pc are on same wifi network and as I'm trying to access the db I'm getting the following error.

 java.sql.SQLException: Access denied for user 'root'@'172.20.24.11'(using password:YES)

The following is the code I'm using to connect the database:

try{

  Connection con=DriverManager.getConnection(
          "jdbc:mysql://172.20.25.207:3306/dgcis","root","root");

  Statement stmt=con.createStatement();
  ResultSet rs=stmt.executeQuery("select COUNTRY_NAME from exp_imp_brc limit 1;");
  while(rs.next())
    str = rs.getString(1);

  System.out.print(str);

  con.close();
}
catch(Exception e){ str = e.toString();}

Also note that this code works perfectly fine on my personal laptop but I get this error when I try to run this on our company network using the company's pc.

phanom9797
  • 43
  • 1
  • 10

2 Answers2

0

Your connection is right but the current user dont have permissions to access database. first you have to grand access permission to respective user. You can give that by loging into cpanel and select database and give permission to respected user you are login in..

TejpalBh
  • 427
  • 4
  • 13
  • what do you mean by cpanel, and btw I'm using windows and can you please provide any code snippet – phanom9797 Jun 25 '18 at 12:35
  • cPanel means your domain hosting control panel where u host your api and database. If you are on windows server and using mysql as backend then go to phpmyadmin select database and assign user to respected database, this is not related to any coding part – TejpalBh Jun 25 '18 at 12:43
  • I don't have any api, I'm trying to connect my app directly to a database(i know it's not recommended but there's no other option) – phanom9797 Jun 25 '18 at 13:01
0

The problem was resolved when I granted all the privileges to the user through which I'm trying to connect to.

I've used the following command:

GRANT ALL PRIVILEGES ON *.* TO 'user_name'@'%' identified by 'user_password';
FLUSH PRIVILEGES;
phanom9797
  • 43
  • 1
  • 10