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 :)