45 comments

    1. You will have to add MySQL JDBC Connector in your project’s build path. If you have already done this then probably its silly problem with IDE. Clean and rebuild the project and it should work.

  1. hi, its work with MySQL Server 5. + ??
    i have this error “unable to connect to any hosts due to exception java.net.socketexception .. ”
    in MySQL man is write that “MySQL JDBC Connector 3.0.17” is not compatibil with MySQL Server 5. +

  2. Hi Mostafa, can you be more specific about problems you are facing.

    If you are getting ClassNotFoundException then probably there is something wrong with your build path. Check carefully if driver is added properly in build path. You might also require INTERNET permission to be able to connect to MySQL server.

    Also as I mentioned earlier, and as Paweł reported, you might get into compatibility issues.

  3. the problem in ClassNotFoundException
    thanks Chitranshu but i don’t know how i can solve
    what is your steps in add jar file im using last version from eclipase

  4. I have a problem in com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. I use “mysql-connector-java-5.1.7-bin.jar”.

    I tried connect with “jdbc:mysql://127.0.0.1:3306/” as well as “jdbc:mysql://localhost:3306/”

    I had also this exception: com.mysql.jdbc.exceptions.jdbc4.communicationsexception communications link failure. The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

    Why it doesn’t work??

  5. It always give me java.sql.SQLException: Unable to connect to any hosts due to exception:……
    i have installed mysql server 5.5.29 and imported MySQL JDBC Connector 3.0.17 to my project…can you attach your example project here please…it would be great 😀
    Thanks in advance…

  6. I resolved this problem. My device haven’t mysql service. I must used IP address server where is my db. Address 127.0.0.1 is wrong.

  7. I resolved the issue here…according to Emulator Networking IP 10.0.2.2 should be used instead of localhost/127.0.0.1….also i tried using mysql connector 5.1.22 and it should not be a problem….But i got a question why do the most people recommend to use PHP to integrate android – mysql…is there some security issue with PHP that can’t be resolved by android programming??sorry for my bad english 😀

    1. It is always advisable to use a web service as an interface between mobile application and DB. That provides flexibility where we can change mobile app, or DB without effecting other things. Also using web services makes it more secure by restricting access to only limited resources.

      1. Hi Chitranshu, i managed to connect from my android emulator to jdbc mysql now..but how can i connect to mysql database from my android phone??is there something i lack…i already set the ip address correctly to the server…any suggestion?thank you

  8. Greetings Chitranshu! Your code is very useful however I’ve encountered a strange problem. I have successfully added the jdbc driver and followed your code. Run the program on emulator and it works. I can retrieve data from MySQL without any problem. However when i transfer the .apk file of the project to my android phone. It shows an error

    “com.mysql.jdbc.exceptions.jdbc4.communicationsexception communications link failure. The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.”

    Why is this so? Any help will be good! Thanks! 😀

  9. hello
    thank you for this helpful article
    I have the following error while executing:
    com.mysql.jdbc.exceptions.jdbc4.communicationsexception communications link failure. The last packet sent successfully to the server was 0 milliseconds ago.
    I’m trying with the server’s address: 192.168.1.134 which is in the same local area network
    and I applied all suggested solutions but nothing helped !
    The server is ok because I tried to connect to it from another client (PC) and using an android application (sql client).
    help please.

  10. hey…your post is nice..i have followed your post…but getting this error ………………..om.mysql.jdbc.exceptions.jdbc4.communicationsexception communications link failure. The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server…………..please suggest me how to remove this problem….

  11. hii…chitranshu…..i am new in android field so i don’t know how to enable remote access for android emulator from my db…please explain it briefly…..

  12. What else do i need to install, and how do it configure server ? im always getting java.lang.CalassNotFoundException: com.mysql.jdbc.Driver.. anyone knows how i can solve it ?

  13. Im also using freemysqlhosting.net database system.. is there any problem ? insted of having a localhost ..

  14. Hi All, for those are getting communications link failure or unable to connect to host, set your midSdkVersion less than 10 and no targetSdkVersion.

  15. this code runs permanently!!! created by diko(Turkey)

    public void mysql() {

    try {
    Class.forName(“com.mysql.jdbc.Driver”);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();

    }

    thrd1 = new Thread(new Runnable() {
    public void run() {
    while (!Thread.interrupted()) {
    try {
    Thread.sleep(100);
    } catch (InterruptedException e1) {

    }
    if (con == null) {
    try {
    con = DriverManager.getConnection(“jdbc:mysql://192.168.1.45:3306/deneme”, “ali”, “12345”);

    } catch (SQLException e) {
    e.printStackTrace();
    con = null;
    }

    if ((thrd2 != null) && (!thrd2.isAlive()))
    thrd2.start();

    }
    }

    }
    });
    if ((thrd1 != null) && (!thrd1.isAlive())) thrd1.start();

    thrd2 = new Thread(new Runnable() {
    public void run() {
    while (!Thread.interrupted()) {

    if (con != null) {
    try {
    // con = DriverManager.getConnection(“jdbc:mysql://192.168.1.45:3306/deneme”, “ali”, “12345”);
    Statement st = con.createStatement();
    String ali = “‘fff'”;
    st.execute(“INSERT INTO deneme (name) VALUES(” + ali + “)”);
    // ResultSet rs = st.executeQuery(“select * from deneme”);
    // ResultSetMetaData rsmd = rs.getMetaData();
    // String result = new String();

    // while (rs.next()) {
    // result += rsmd.getColumnName(1) + “: ” + rs.getInt(1) + “\n”;
    // result += rsmd.getColumnName(2) + “: ” + rs.getString(2) + “\n”;

    // }

    } catch (SQLException e) {
    e.printStackTrace();
    con = null;
    }

    try {
    Thread.sleep(10);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } else {
    try {
    Thread.sleep(300);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    });

    }

  16. Hello Chitranshu,

    For three days I try to do what you have posted in this article but nevertheless I can not fix, I have tried a number of JDBC libraries:

    mysql-connector-java-5.1.26-bin
    mysql-connector-java-5.1.25-bin
    mysql-connector-java-5.0.8-bin
    mysql-connector-java-3.0.17-ga-bin
    mysql-connector-java-5.1.22-bin

    java exception

    Could not find class ‘javax.naming.StringRefAddr’, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl $ Property.storeTo Connection

    I’m using the library Android 4.0
    ADT on Windos 8 64-bit

    How can I fix

    Tanks
    Claudio

  17. Although it is very interesting to be able to access remote db using only android native code. But it is so wrong on so many different levels. Please use this post for educational purpose and do not try to use this mechanism in any live projects. Better approach would be access DB through one of the SOAP or REST web services.

  18. How do I add the JDBC connector to my project? Which file do I have to add from the .zip i downloaded and where exactly in the project path?

    1. Extract the file and copy the .JAR file in your project’s ‘lib’ folder. Go to eclipse (same project), right click on this JAR file and ‘Build Path’->’Add to Build Path’

  19. try
    {
    Statement stmt = GetConnection().createStatement();

    int i = stmt.executeUpdate(“INSERT INTO salesheader(SalesCode) VALUES (‘403’)”);

    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }

    Hi ,I m using like that for data insert..but got null pointer error or “com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    The last packet successfully received from the server was 25,460 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago.” this error…i searched in google also..still not get any solution..can u help me.bro..how can i solve this problem

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s