0

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>
VadiMayer
  • 21
  • 5
  • "I understand that you need to use jdbc" -- please [reconsider your use of JDBC on Android](https://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android). – CommonsWare Jan 13 '23 at 20:46
  • aside from JDBC concerns, to do this you need to expose your database to the public. this is a very bad idea; you should always use a service between the app and the database. – ysth Jan 13 '23 at 21:10
  • Everything works directly through intellij Idea. Idea + base it's ok, but with app not work. – VadiMayer Jan 13 '23 at 21:45

0 Answers0