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).