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();
}
}
Considering that you instantiate a new object on your exeUpdateDBquestion_items() function, you have to call your getLesson() function if you want to change the value of the activeLesson attribute inside the other class.
public void exeUpdateDBquestion_items(String ans, String desc) throws ClassNotFoundException, SQLException{
openLesson ol = new openLesson();
ol.getLesson();
Or
public openLesson() {
initComponents();
this.getLesson();
}
And you can restrict access to your attribute and add getter/setter:
private String activeLesson
public getActiveLesson();
public setActiveLesson();
I don't really know how I did it, but after 13 hours of experimenting, this worked for me.
I've made the function exeUpdateDBquestion_items
take 3 parameters (this will now include the lesson
) instead of explicitly putting it on the query:
public void exeUpdateDBquestion_items(String ans, String desc, String lesson) throws ClassNotFoundException, SQLException{
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 + "','" + lesson + "')");
JOptionPane.showMessageDialog(null, "Identified Item Saved!");
System.out.println(ol.activeLesson + " lesson updated too");
} catch (SQLException e){
JOptionPane.showMessageDialog(null, e.getMessage());
} finally {
con.close();
stmt.close();
}
}
and in the button which activates this function, I called the method getLesson()
instead of the String Variable activeLesson
:
private void btnSaveIdentifiedActionPerformed(java.awt.event.ActionEvent evt) {
btnIdentifyAnswer.setVisible(true);
btnIdentifyDesc.setVisible(false);
btnSaveIdentified.setVisible(false);
openLesson ol = new openLesson(); // this is the other class
try {
if (checkIfExists() == true){
JOptionPane.showMessageDialog(null, "Item Already Exists.");
} else {
exeUpdateDBquestion_items(ansTempHandler, descTempHandler, ol.getLesson()); // called the method here instead of the variable
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(mainScreen.class.getName()).log(Level.SEVERE, null, ex);
}
}
and this solved the problem. Thank you guys for all the hints.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.