How do I get value from a column? ( java )… here is a solution to the problem.
How do I get value from a column? ( java )
I
do simple alerts, I need to display alerts if the value == in column 0 today.
Calendar cal = new GregorianCalendar();
int day = cal.get(Calendar.DAY_OF_MONTH);
try {
File currDir= new File ("Baza.db");
String sc = currDir.getAbsolutePath();
sc = sc.substring(0, sc.length());
String url = "jdbc:sqlite://"+sc;
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Day FROM Month");
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
if(jTable1.getValueAt(0, 0).equals(day)){
JOptionPane.showMessageDialog(null, "Do your question!");
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
I tried using the methods getColumn, getColumnModel, and getSelectedColumn, but didn’t compare it with my calculation.
Solution
Your day
variable is of type int
and jTable1.getValueAt(0, 0)
returns an Object
, so both are different types and always return false
when comparing.
Because you want to compare them as strings for equality, you should change your comparison
if(jTable1.getValueAt(0, 0).equals(day)){
to,
if(String.valueOf(jTable1.getValueAt(0, 0)).equals(String.valueOf(day))){
Alternatively, you can parse the values in the JTABLE column to int
and then compare the integer values like this
if(Integer.parseInt(jTable1.getValueAt(0, 0).toString()) == day){
However, I prefer the first way to compare strings, because the second way may encounter NumberFormatException