0

I use tabbedpane and I have 2 tabs - materials and worker (both in separate classes). Both tabs have JTables where I can edit information.

Both use SQLite database - one uses MATERIALS other uses WORKERS.

I initiate the connection from both panels in the same way to upload the info from database to the tables. Example from one class:

//Connection to SQLite
    void connectSQL() throws SQLException {
        try {
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        c = DriverManager.getConnection("jdbc:sqlite:database.db");
        c.setAutoCommit(false);

    }

    //Loading table from database (SQLite)
    void tableSQL () throws SQLException {
        statement = c.createStatement();
        rs = statement.executeQuery("SELECT * FROM MATERIALS");
        while(rs.next()) {
            id = rs.getInt("ID");
            material = rs.getString("Material");
            quantity = rs.getInt("Quantity");
            model.addRow(new Object[]{id, material, quantity});
        }
        rs.close();
        statement.close();
    }

My questions are:

1) Should I close the connection once I upload the data? 2) If I need to update the table or to insert new data later as a button is pressed, should I connect to database again or how is it done? 3) Is it legit if the two classes connect to database in this way (I am not sure if it happens at the same time).

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Karolizzz
  • 119
  • 1
  • 12
  • 1
    This question has nothing to do with Swing, JTabbedPanes, JTables, or the like. Rather it boils down to one issue: should you leave the database connection open after connecting with it, or should you close it once you've extracted information from it or uploaded information to it? That's it, pure and simple. – Hovercraft Full Of Eels Nov 05 '16 at 13:15
  • Actually, [this is a better duplicate](http://stackoverflow.com/questions/1039419/when-to-close-connection-statement-preparedstatement-and-resultset-in-jdbc) – Hovercraft Full Of Eels Nov 05 '16 at 13:16
  • Also see [these similar questions](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+sql+leave+open). – Hovercraft Full Of Eels Nov 05 '16 at 13:17

0 Answers0