Class H2 Access

I am trying to avoid static and singletones by integrating the local H2 database into my JavaFX application (Java8). Five other classes (including controllers) require access to the database, so I'm trying to use one H2 connection between them. I read the Connection Pool avoids unnecessary reconnections, but I'm confused if applicable here. Using a desktop computer, single user.

Next ExtrasDBclass contains the method 3, initializeDB, getDBValuesand performQuery. These methods used to be static, and I would call them from other classes using ExtrasDB.getDBValues()and use the result accordingly, but since my application uses multiple threads, I am looking for a better approach. I am in the process of removing all static / singleton usage from my application.

The method initializeDBcreates a connection and, if necessary, creates a table, and this method is called only once from my main control method initialize. This leads to the fact that the connection connmust be isolated from this instance and inaccessible to other class calls to getDBValuesor performQuery, which causes zero result sets. How to make a database connection accessible to all necessary classes so that these classes can freely access the database using the above methods?

public class ExtrasDB {

    final String JDBC_DRIVER = "org.h2.Driver";
    final String DB_URL = "jdbc:h2:~/mainDB";
    final String USER = "test";
    final String PASS = "test";
    Connection conn = null;
    Statement statement = null;
    String myStatement;
    ResultSet rs;
    DatabaseMetaData meta;

    public void initializeDB() {

        try {
            Class.forName("org.h2.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            System.out.println("Connected to database successfully...");
            statement = conn.createStatement();
            meta = conn.getMetaData();
            rs = meta.getTables(null, null, "EXTRAS", new String[]{"TABLE"});  // <--- Checks for existence of table "EXTRAS"

            if (!rs.next()) {

                System.out.println("Table doesn't exist yet... Creating...");
                sql = "CREATE TABLE EXTRAS " +
                        "(column_1 VARCHAR(255), " +
                        " column_2 VARCHAR(255))";

                statement.executeUpdate(sql);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public void getDBValues() throws SQLException {
        rs = statement.executeQuery("SELECT * FROM EXTRAS");
        while (rs.next()) {
            //..... do something
        }
    }

    public void performQuery(String query) {
        try {
            statement.executeUpdate(query);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
+4
source share
1 answer

You have two options if you do not want some kind of static access:

  • Service- Factory ( ) . Factory - pojo, , . Factory . , , Factory.
  • , . . . , spring .

JavaFX- im . Root javafx.application.Application:: start. Root, , (, ), seters. fxml-loader, Root, init

public void initialize(Root root);

. .

0

Source: https://habr.com/ru/post/1655559/


All Articles