0

I am developing an Android app as a project for college and I want to connect to a local database by IP, but I get this error: "java.sql.SQLNonTransientConnectionException: Could not create connection to database server."

I have tried all possible IPs: localhost, 127.0.0.1, 10.0.2.2 etc.

Any ideas how to fix it?

Code:

public class DatabaseConnection {
    private static Connection connection;

    private DatabaseConnection () {

    }

    public static Connection getInstance() throws SQLException {
        if(connection == null) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                System.out.println(" Unable to load driver. ");
            }
            String url = "jdbc:mysql://10.0.2.2:3306/businessoffice";
            String username = "root";
            String password = "";
            try {
                connection = DriverManager.getConnection(url, username, password);
                System.out.println(" Connection Established. ");
            } catch (SQLException e) {
                System.out.println(" Error connecting to database:  "
                        + e);
            }
        }
        return connection;
    }
}
  • Please [reconsider using JDBC in Android](https://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android). – CommonsWare Sep 27 '21 at 22:35

0 Answers0