0

Hello everyone I'm trying to make a scheduling system for my System and Analysis Design thesis and I am having trouble trying to connect/bind/make the jTable listen to the jDateChooser's input. Elaborately, I want my scheduling to be like this:

I choose a date in the jDateChooser

jTable will 'sort out' itself via the date inputted on the jDatechooser

is there anyway to do this? For now all I have is a table propertyChangelistener:

    private void sched_tablePropertyChangeListener(java.beans.PropertyChangeEvent evt) {
try{
            String calendar = ((JTextField)jdc.getDateEditor().getUiComponent()).getText();
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/accountsDB?zeroDateTimeBehavior=convertToNull","root","");
            String query = "select * from accountsdb.schedules where Date= ?";
            ps= conn.prepareStatement(query);
            ps.setString(1, calendar);
            ResultSet rs = ps.executeQuery();
            sched_table.setModel(DbUtils.resultSetToTableModel(rs));   
        } 
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }   finally {
    if (conn != null) 
        try { conn.close(); 
        } catch (SQLException ignore) {}
    if (ps != null){
        try {
            ps.close();
        } catch (SQLException ignore){}
    }
    
}  
}

Somehow when I run my application it doesn't seem to open if that block of code is on it which means I really did do something wrong. Can anyone change or tell me what I should do or where should I start with the jTable listening to the jDatechooser thing?

~Thanks in advance for those who will answer!~

Community
  • 1
  • 1
edohedo
  • 61
  • 5

1 Answers1

0

Nevermind, turns out all I had to do was change the jDateChooser's Date Format since it wasn't exactly the same formatting hence I couldn't call anything from the database. If anyone's interested on what I did I'll just leave this here

private void jdcPropertyChange(java.beans.PropertyChangeEvent evt) {
try{
                   String d1  = ((JTextField)jdc.getDateEditor().getUiComponent()).getText();
                   Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/accountsDB?zeroDateTimeBehavior=convertToNull","root","");

            String query = "select * from accountsdb.schedules where Date= ? order by time,timezone";

            ps = conn.prepareStatement(query);
            ps.setString(1, d1);
            ResultSet rs = ps.executeQuery();
            sched_table.setModel(DbUtils.resultSetToTableModel(rs));
               } catch(Exception e){
                   JOptionPane.showMessageDialog(null, e);
               }    finally {
    if (conn != null) {
        try { conn.close(); 
        } catch (SQLException ignore) {}

    }
    if (ps != null){
        try {
            ps.close();
        } catch (SQLException ignore){}
    }
    }
    }

What this does is that everytime I pick on a date from the jDateChooser(named it jdc) the table/database calls that date and sorts it out. Which is what I wanted.

edohedo
  • 61
  • 5