Home > OS >  Connection of Android Studio project to MySQL database
Connection of Android Studio project to MySQL database

Time:03-23

I am developing an android studio project and would like to be able to access data from a database sitting on the pc which I am programming from. I would like to use a MySQL database as I have worked with them before. I was wondering if it is possible to do it from android studio. If it is possible what would the code be for me to do it? I have looked through other people's questions and answers and all of them yield errors for me. Thanks in advance

I have tried the following and all my ip, port, username and password information is correct but I keep getting the following error: E/Error in connection: com.mysql.jdbc.Driver My code in my connection class after declaring my attributes is as follows


        //declaring connection policy
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
        StrictMode.setThreadPolicy(policy);
        Connection connection = null;
        String ConnectionURL = null;

        //starts actual connection to db
        try
        {
            //establishing connection to Database
            //all hard coded
            Class.forName("com.mysql.jdbc.Driver");
            ConnectionURL= "jdbc:jtds:sqlserver://"  ip   ":"  port ";"  "databasename="  database ";user=" uname ";password=" pass ";";
            connection = DriverManager.getConnection(ConnectionURL);
        }
        //Catches any error in the data
        catch (Exception e)
        {
            //executes if there is a error connecting to the database
            Log.e("Error in connection", e.getMessage());
            System.out.println("Error connecting to db");
        }
        //returns the connection to the main class
        //it will then be sent to the DBmanager class and used to get the data out of the database
        return connection;
    }
}

CodePudding user response:

a direct connection of apps to a mysql server(and for other rdms also) is discouraged, as you need for this to open your database to the internet with a direct access, which is a fatal security risk.

search for

  • REST API MySQL android

and you will find a lot of tutorials

like this short one https://phppot.com/php/php-mysql-rest-api-for-android/ or this https://www.javahelps.com/2018/12/access-mysql-from-android-through.html

  • Related