I have this problem for about 2 days and I cannot find any solution. Thing is I'm using MySQL-Connection-Java to operate MySQL base on Android. While debugging it cannot connect with this base, even though code in Eclipse Java works fine. I was looking throughout many forum discussions on the Internet, but seems like there is no valid solution, or maybe I am THAT blind. Below you can see class code that I'm using. I have no clue why in Eclipse in connects with the base, but on the Android device it doesn't want to.
Error code:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JDBC {
// init database constants
private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
private stat
ic final String DATABASE_URL = "jdbc:mysql://sql7.freemysqlhosting.net/sql7109625?autoReconnect=true";
private static final String USERNAME = "REMOVED";
private static final String PASSWORD = "REMOVED";
private static final String MAX_POOL = "250";
// init connection object
private Connection connection;
// init properties object
private Properties properties;
// create properties
private Properties getProperties() {
if (properties == null) {
properties = new Properties();
properties.setProperty("user", USERNAME);
properties.setProperty("password", PASSWORD);
properties.setProperty("MaxPooledStatements", MAX_POOL);
}
return properties;
}
// connect database
public Connection connect() {
if (connection == null) {
try {
Class.forName(DATABASE_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL, getProperties());
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
return connection;
}
// disconnect database
public void disconnect() {
if (connection != null) {
try {
connection.close();
connection = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
} }