How can i take value from column? (Java)

Viewed 41

I do simple alarm and i need show alert if value from 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 trying use method getColumn, getColumnModel and getSelectedColumn but but nothing compared my calculations..

1 Answers

Your day variable is of type int and jTable1.getValueAt(0, 0) returns an Object hence both are of different type and will always return false upon comparison.

As you want to compare the equality of them as string, hence you should change your comparison from this,

if(jTable1.getValueAt(0, 0).equals(day)){

to,

if(String.valueOf(jTable1.getValueAt(0, 0)).equals(String.valueOf(day))){

Or, you may parse the value in Jtable column to int and then compare the integer value like this,

if(Integer.parseInt(jTable1.getValueAt(0, 0).toString()) == day){

However, I'll prefer the first way to compare as string, as second way can run into NumberFormatException

Related