Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've got a MySQL server running on a local Linux machine. I haven't been faced to any problems communicating with it and executing SQL statements through a simple Java program (running inside of eclipse) using the MySQL Connector/J. Here is my simple Java program:

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


public class main{
    private static void request(){
        String url = "jdbc:mysql://myadress:3306/mydatabase";
        String user = "user";
        String passwd = "passwd";

        Connection conn = null;
        try{
            /* Initializing the connection */
            conn = DriverManager.getConnection(url, user, passwd);

            Statement statement = conn.createStatement();

            ResultSet resultset = statement.executeQuery(/* MY SQL reqquest */);

            while(resultset.next()){
                System.out.println(resultset.getString(/* THE COLUMN AND ROW I WANTED IN MY REQUEST */));       
            }

        }catch(SQLException e){
            System.out.println("SQL connection error: " + e.getMessage());
        }finally {
            if(conn != null){
                try{
                    /* CLosing connection */
                    conn.close();
                }catch (SQLException e){
                    System.out.println("Error while closing the connection: " + e.getMessage());
                }
            }
        }
    }
    public static void main(String[] args) {
        try{ // Loading the MySQL Connector/J driver
            Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException e){
            System.out.println("Error while loading the Driver: " + e.getMessage());
        }
        request();
    }

}

This code works perfectly inside of eclipse and can perform as much SELECT as UPDATE SQL requests without any problem. When I tried to adapt this request method to my MainActivity.java class, my android application does load the driver correctly but can't communicate with my MySQL server and returns me an error.

If I enter a local IP adress to my server in the previous code and connect my android phone to the Wi-Fi, then my app returns me:

"Communication link failure. The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server."

But if I try to enter an external IP adress to my server in the same previous code (don't worry the MySQL port is open to the internet and my server is listening to any IP), use my android phone over an LTE internet connection, I get a different error:

"Could not create connection to database server"

Up until now, none of my research has helped me out. Does anyone has any idea of where my problem could come from ? Thanks in advance :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
127 views
Welcome To Ask or Share your Answers For Others

1 Answer

The general misconception among Java developers who is moving to Android world is that everything that has been created for old good Java can be used on Android as well. Since Android usage of Java has violated the major Java principle, "write once, run everywhere", it's simply not true. I've seen people trying to use common Apache jars with Android, which resulted to weird errors (see Can't import Apache HTTP in Eclipse).

The same problems seem to be true for Java JDBC drivers and API's, e.g. see answers here: Connecting to MySQL from Android with JDBC

The common advice - each time when you try to use a 3rd party JAR with Android, check if it's compatible with the latter or if there is a port, which is specific for Android.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...