I want to make an Android app. Please tell me how to create and configure a configuration class to send queries to the database. The database is located in the cloud. What should this class look like?
Let's say a client clicks on a button and a list of building materials is shown to him. Clicks on another button, a list of applications appears.
I understand that you need to use jdbc, but it's not clear how to do it correctly ((
I use Intellij idea.
public class DatabaseController {
private String host = "jdbc:mysql://*********/*********?autoReconnect=true&useSSL=false";
private String DB_name = "*********";
private String username = "*********";
private String password = "*********";
private Connection getDbConn() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(host,username, password);
}
public void insertTask(String task) throws SQLException, ClassNotFoundException {
PreparedStatement prSt = getDbConn().prepareStatement("DELETE FROM User WHERE id = 1");
prSt.executeUpdate();
}
public ArrayList<String> geTasks() throws SQLException, ClassNotFoundException {
String sql = "SELECT * FROM User";
Statement statement = getDbConn().createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ArrayList<String> tasks = new ArrayList<>();
while (resultSet.next()) {
tasks.add(resultSet.getString("task"));
}
return tasks;
}
}
an exception is thrown
Exception in thread "main" java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>