I wrote a simple application in Android which consists of a button that,when clicked,tries to connect to a MySQL database using JDBC.
Now,i tried to enter the URL and credentials of a database located on the internet,and I was able to connect to it.

When i try to connect to the MySQL database i installed on my PC,it throws an SQLException regarding (i think) the JDBC string.
I used the root credentials for the access,in this way:

jdbc:mysql://localhost:3306/db\_name
------------------------------------

I also tried to run the connection in a Java Application,and it runs fine...it has some sort of problem in Android.
There's the code of my application:

```
       package com.example.testconnessionemysql;

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

       import android.app.Activity;
       import android.app.ProgressDialog;
       import android.os.AsyncTask;
       import android.os.Bundle;
       import android.view.Menu;
       import android.view.MenuItem;
       import android.view.View;
       import android.view.View.OnClickListener;
       import android.widget.Button;
       import android.widget.Toast;

        public class MainActivity extends Activity implements               OnClickListener{
         Button bottone;
         @Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         bottone = (Button)findViewById(R.id.button1);
         bottone.setOnClickListener(this);

}

          class ConnectAsync extends AsyncTask<String, Void, String> {

          @Override
          protected String doInBackground(String... urls) {

          String url = "jdbc:mysql://localhost:3306/prova";
          String user = "root";
          String password = "my_psw";

          Connection connection = null;
          try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url,user,password);
           } catch (ClassNotFoundException e) {
            System.out.println("Driver non caricato");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Errore con url e credenziali!");

        }

        if(connection!= null)
        {System.out.println("Connessione riuscita!");}
        return null;}

         }

        public void connectToDB(View view)
        {
         ConnectAsync task = new ConnectAsync();
       task.execute();

               }

              @Override
              public void onClick(View v) {
               connectToDB(v);

             }

                }

```

I also put the logcat here:

```
04-30 06:25:17.670: W/EGL_emulation(1965): eglSurfaceAttrib not     implemented
04-30 06:25:22.150: W/System.err(1965): com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
04-30 06:25:22.150: W/System.err(1965): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
04-30 06:25:22.150: W/System.err(1965):     at java.lang.reflect.Constructor.constructNative(Native Method)
04-30 06:25:22.150: W/System.err(1965):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
04-30 06:25:22.150: W/System.err(1965):     at java.lang.reflect.Constructor.constructNative(Native Method)
04-30 06:25:22.150: W/System.err(1965):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
04-30 06:25:22.150: W/System.err(1965):     at java.sql.DriverManager.getConnection(DriverManager.java:179)
04-30 06:25:22.150: W/System.err(1965):     at java.sql.DriverManager.getConnection(DriverManager.java:213)
04-30 06:25:22.150: W/System.err(1965):     at com.example.testconnessionemysql.MainActivity$ConnectAsync.doInBackground(MainActivity.java:55)
04-30 06:25:22.150: W/System.err(1965):     at com.example.testconnessionemysql.MainActivity$ConnectAsync.doInBackground(MainActivity.java:1)
04-30 06:25:22.150: W/System.err(1965):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-30 06:25:22.150: W/System.err(1965):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-30 06:25:22.150: W/System.err(1965):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-30 06:25:22.150: W/System.err(1965):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-30 06:25:22.150: W/System.err(1965):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-30 06:25:22.150: W/System.err(1965):     at java.lang.Thread.run(Thread.java:841)
04-30 06:25:22.150: W/System.err(1965): Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
04-30 06:25:22.150: W/System.err(1965):     at libcore.io.IoBridge.socket(IoBridge.java:576)
04-30 06:25:22.150: W/System.err(1965):     at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
04-30 06:25:22.150: W/System.err(1965):     at java.net.Socket.checkOpenAndCreate(Socket.java:664)
04-30 06:25:22.150: W/System.err(1965):     at java.net.Socket.setTcpNoDelay(Socket.java:530)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.StandardSocketFactory.configureSocket(StandardSocketFactory.java:134)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:205)
04-30 06:25:22.150: W/System.err(1965):     at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
04-30 06:25:22.150: W/System.err(1965):     ... 20 more
04-30 06:25:22.150: W/System.err(1965): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
04-30 06:25:22.150: W/System.err(1965):     at libcore.io.Posix.socket(Native Method)
04-30 06:25:22.150: W/System.err(1965):     at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
04-30 06:25:22.150: W/System.err(1965):     at libcore.io.IoBridge.socket(IoBridge.java:561)
04-30 06:25:22.150: W/System.err(1965):     ... 26 more
04-30 06:25:22.150: I/System.out(1965): Errore con url e credenziali!

```

As you can see from the logcat,it catches an SQLException and prints out the message I wrote in the code,so I think there's a problem with the database connectivity.
I use MYSQL Workbench to handle this database,i also granted the root user the privilege to access from every IP address,and i also edited the my.ini MySQL configuration file in this way:

```
[mysqld]
...

bind-address    = *
#skip-networking
...

```

I use MySQL Server 5.6,and I used mysql-connector-java-5.1.35-bin.jar as JDBC driver.
As I previously said,I was able to connect to another MySQL database on the internet using the same code without errors...I just can't find what's going on with my database.

I know that JDBC database connection in Android isn't the best solution,but I wanted to practice this way before moving to something more efficient (like JSON and web services).
Thanks in advance!