How can I add events to the FullCalendar from a database in Postgres

1

good morning.

My problem is that I have a system made in JSP which I added the free code of FullCalendar but I want to add events that I have in a database made in Postgres, the problem is that I do not know how to enter those events that I recover from my BD in the FullCalendar. Could someone help me? ... I show the important code parts:

Class in java with which the query is made to the BD to obtain the required data that would be, name of the event, start date and conclusion date):

//Creamos nuestros objetos para la comunicacion y ejecucion de codigo SQL
private Connection con;
private Statement stmt, stmt_aux;
private ResultSet rs, rs_aux;
private DataSource ds;

//Constructor    
public Conexion() {
    stmt = null;
    stmt_aux = null;
    con = null;
    rs = null;
    rs_aux = null;
}

//RECUPERA LOS DATOS QUE VAN EN EL CALENDARIO

  public ArrayList getCalendario() throws Exception {
    ArrayList datos = new ArrayList();
    try {
     stmt = con.createStatement();
     rs = stmt.executeQuery("SELECT * FROM sp_recuperadatosparacalendariocesip();");
    while (rs.next()) {
      Clase c = new Clase();
      c.setTitle(rs.getString(1));
      c.setDateStart(rs.getString(2));
      c.setTimeStart(rs.getString(3)); 
      c.setDateEnd(rs.getString(4));
      c.setTimeEnd(rs.getString(4)); 
      datos.add(c);
    }
    rs.close();
    stmt.close();
    } catch (SQLException ex) {
   System.err.println("SQLException: " + ex.getMessage() + " getCalendario =(");
    }
   return datos;
 }

Later in my JSP file I retrieve the data obtained from the BD:

<jsp:useBean id="recuperaDatos" scope="request" class="control.Clase" />

ArrayList calendario = new ArrayList();

recuperaDatos.conecta();
calendario = recuperaDatos.getCalendario();
recuperaDatos.desconecta();

//Aquí esta el script del calendario y le agrego una función para recuperar los datos de los eventos que tengo en  mi ArrayList. 

<script type="text/javascript">
  $(document).ready(function() {
    $('#calendar').fullCalendar({
     events:(function(){
       var data=[];
       for(var r=0;r < <%=calendario.size()%>; r++){
         data.push({
          title: <%=calendario%>[r],
          start: <%=calendario%>[r],
          end: <%=calendario%>[r]
         });
      }
     return data; 
    });

    });

    });

   </script>

   <div id='calendar'style="display: none"></div>

The problem with this is that when you add the small code of the events, the calendar disappears and in Netbeans it does not show any errors, so I do not know what happens or what is wrong. I would appreciate your help to correctly enter events into the calendar. I'm new to javascript, so your help would be very useful for me. Thank you in advance =)

    
asked by Nancy 27.01.2017 в 18:01
source

1 answer

2

Done, I managed to solve it. With the code that I already have. Just modify the Script and it looks like this:

<script>
        $(document).ready(function(){
            $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,basicWeek,basicDay'
                },
                events: [
                       <%

                            String t = null;
                            String f_i = null;
                            String h_i = null;
                            String f_i = null;
                            String h_f = null;



                            for (int i = 0; i < calendario.size(); i++){
                                t = ((Clase)calendario.get(i)).getTitle();
                                f_i = ((Clase) calendario.get(i)).getDateStart();
                                h_i = ((Clase) calendario.get(i)).getTimeStart();
                                f_f = ((Clase) calendario.get(i)).getDateEnd();
                                h_f = ((Clase) calendario.get(i)).getTimeEnd();
                        %>


                        {
                        title: "<%=t%>",
                        start: "<%=f_i%>" "T" + "<%=h_i%>",
                        end: "<%=f_f%>" + "T" + "<%=h_f%>"
                        },
                        <%
                           }
                        %>

                ]       
            });
        });

    </script>
    
answered by 16.02.2017 / 15:48
source