0

This is my code that doesn't do anything :). It should generate a table with appointments both assigned and free.My database name is test, table name is programari, column name for date is data, column name for hour is ora and column name for user is pacienti.

private void refreshTable(){

 String[] orar={"8:00","8:30","9:00","9:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30","18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00"};

        DefaultTableModel model = new DefaultTableModel();

        model.setRowCount(0);
        try {

               for(int i =0;i<orar.length;i++){
                   pst=conn.prepareStatement("select * from programari");
                   rs=pst.executeQuery();
                   if(rs.next()){
                   if(rs.getDate("data").toString().equals(datap.getDate().toString())){
                       if(rs.getString("ora").equals(orar[i])){
                           model.addRow(new Object[]{orar[i],rs.getString("pacienti")});
                       }else{
                           model.addRow(new Object[]{orar[i],null});
                       }
                   }
                   }else{
                       model.addRow(new Object[]{orar[i],null});
                   }                                
    }                 
        } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
        }         
    }
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
  • Date.toString do not return what you expected – Jens May 30 '16 at 08:00
  • please elaborate im still verry new to java – Dio Vladutz May 30 '16 at 08:19
  • Read about SimpleDateFormat – Jens May 30 '16 at 08:22
  • i`ve added in the try{ "DateFormat df = DateFormat.getDateInstance();" and i`ve changed "if(rs.getDate("data").toString().equals(datap.getDate().toString())){" to " if(df.format(rs.getDate("data")).equals(df.format(datap.getDate()))){" and it does nothing new :-s it doesnt solve my problem – Dio Vladutz May 30 '16 at 08:32

1 Answers1

0

Several points:

  1. Prepare the statement outside the loops.
  2. Use e.g. JodaTime: http://www.joda.org/joda-time/
  3. Consider using prepared statement parameters.
  4. Consider using a column data_y_ora (or something like that) of type DATETIME instead of data and ora.
  5. Use a loop to initialize the hours.

The following is an example using JodaTime, a column data_y_ora of type DATETIME in the database table and prepared statement parameters:

private void refreshTable() {
    // this is the "base" date
    final DateTime data = new DateTime(datap.getDate());
    // here a List is used instead of an array
    final List<DateTime> orar = new ArrayList<DateTime>();
    // set up the dates/times you want to query
    for (int hour = 8; hour <= 22; hour++) {
        orar.add(data.withTime(hour, 0, 0, 0);
        orar.add(data.withTime(hour, 30, 0, 0);
    }
    final DefaultTableModel model = new DefaultTableModel();
    // create a database query with a parameter
    final PreparedStatement pst = conn.prepareStatement("select * from programari where data_y_ora = ?");

    model.setRowCount(0);
    try {
        // iterate over the dates/times
        for (DateTime ora : orar) {
            // set the query parameter
            pst.setDate(1, ora.toDate());
            // execute the query
            rs = pst.executeQuery();
            if (rs.next()) {
                // if a record has been found
                model.addRow(new Object[] { ora, rs.getString("pacienti") });
            } else {
                // no record has been found
                model.addRow(new Object[] { ora, null });
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

Please note that the rows now have a DateTime instance instead of a String as a first element.

Lars Gendner
  • 1,816
  • 2
  • 14
  • 24