0

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();
                }
            }
        } }
Niels Masdorp
  • 2,534
  • 2
  • 18
  • 31
  • 8
    If I were you, I'd change my MySQL password really quick as you just posted it publicly on the internet. – das_j Mar 06 '16 at 19:44
  • Why do you think you can use code to connect to a regular MySQL database on your machine to connect to a SQLite database on Android without changing anything? – Niels Masdorp Mar 06 '16 at 19:59
  • @Niels Masdorp When I connect with regular MySql, I take data with this code: [link](http://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database) `String sql = "SELECT * FROM stackoverflow"; try { PreparedStatement statement = mysqlConnect.connect().prepareStatement(sql); ... go on ... ... go on ... ... DONE .... } catch (SQLException e) { e.printStackTrace(); } finally { mysqlConnect.disconnect(); }` – Jakub By ByJacob Mar 06 '16 at 20:10

1 Answers1

0

I find my problem. I use

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 

and my application work :)