You need to connect to the external MySQL database in the Android application, which would use the resources of the finished service, but without the API. I know that this is not good, but this task is just worth it.

How can I do that?

  • Screw MySQL-client in the Android application, even if it is not good. What exactly is the problem? - D-side
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

In order to connect to the database you will need:

1. Connect to the project JDBC Driver for MySQL . If you are using AS then add the following line in the dependencies:

 compile 'mysql:mysql-connector-java:5.1.6' 

2. Before the first access to the database, you must register the driver:

 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("Where is your MySQL JDBC Driver?"); e.printStackTrace(); return; } 

The com.mysql.jdbc.Driver string depends on the connected driver.

3. Get the connection to the base as follows:

 Connection conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password"); 

Naturally replace the hostname, port, dbname, username and password with your own.

PS : and yes, we do not forget to make the connection and all kinds of manipulations with the base not in the UI stream . You should not forget about permissions in the manifesto.

  • Thanks, I will try. - Nadezhda Chezhegova
  • @NadezhdaChezhegova and yes, I would also like to note that not all hosting services provide a direct connection to the database for free ... that's what I am, if this is your case that would save time) - ermak0ff
  • in what format does the data get this interface? - Shwarz Andrei
  • @ShwarzAndrei tutorials.jenkov.com/jdbc/index.html - here you can get acquainted with the main points of working with jdbc - ermak0ff