My program is like this: When I click a button, a popup will appear (a different JFrame class, I did not use JOptionPane) in that popup, there is a JTable which contains data from the database (which are file names) I want to take that file name and use it on a different class for use of another function.
Here I declared a String in "OpenLesson"(this is the popup) class:
public class openLesson extends javax.swing.JFrame {
String activeLesson = "Manually Added"; //this is default when nothing is selected
public openLesson() {
initComponents();
}
and then here is the getLesson() function that takes data from the database depending on what is selected on a JTable. And also change the value of the string activeLesson
depending on what is selected:
public String getLesson() throws ClassNotFoundException, SQLException {
int selectedColumn = 0;
int selectedRow = tblOpenLesson.getSelectedRow(); // this is the JTable
Connection con = null;
Statement stmtGetLesson = null;
try {
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite");
stmtGetLesson = con.createStatement();
ResultSet rs = stmtGetLesson.executeQuery("SELECT * FROM lessons WHERE lesson = '" + tblOpenLesson.getModel().getValueAt(selectedRow, selectedColumn) + "';");
if (rs.next()) {
activeLesson = rs.getString("lesson"); // this (should) change the value of activeLesson above with the result of the query
return rs.getString("lesson");
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
} catch (ClassNotFoundException ex) {
Logger.getLogger(openLesson.class.getName()).log(Level.SEVERE, null, ex);
} finally {
con.close();
stmtGetLesson.close();
}
return "";
}
Now, on a different class, I want to use the string activeLesson
because I want to use it on an sqlite query for another JTable. Here's the function:
public void exeUpdateDBquestion_items(String ans, String desc) throws ClassNotFoundException, SQLException{
openLesson ol = new openLesson();
Connection con = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite"); // connect to database
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO question_items (item_name, item_desc, lesson) VALUES ('" + ans + "','" + desc + "','" + ol.activeLesson + "')");
JOptionPane.showMessageDialog(null, "Identified Item Saved!");
} catch (SQLException e){
JOptionPane.showMessageDialog(null, e.getMessage());
} finally {
con.close();
stmt.close();
}
}
And here comes the problem, no matter what I select on the JTable from the openLesson
class, the activeLesson
still uses the default text "Manually Added" and not the selected data.
I'm sorry if this is a dumb question, I'm new to java, and to Programming in general, so I'm not really understanding what is the problem here.
EDIT: This is how my Table is getting its data. When this button is clicked, the popup will appear containing the table (I have a function that updates the database before this function).
public void updateDBlessons() throws SQLException {
Connection con = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM lessons");
DefaultTableModel dtm = (DefaultTableModel) tblOpenLesson.getModel();
dtm.setRowCount(0);
while(rs.next()){
Object o[] = {rs.getString("lesson")};
dtm.addRow(o);
}
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e);
} finally {
con.close();
stmt.close();
}
}