JSP Servlet中的完整日历(使用MVC)从MySQL获取记录返回NULL

时间:2017-01-26 09:43:57

标签: jsp servlets calendar

我第一次尝试使用jsp,servlet(MVC)的完整日历jquery插件。

可以在Servlet中使用以下所有内容:

 List allClassesList = new ArrayList();
 TTSlotDB slot1 = new TTSlotDB();

 slot1.setId(1);
 slot1.setTitle("WORK");
 slot1.setStart("2017-01-08");
 allClassesList.add(slot1);


TTSlotDB slot2 = new TTSlotDB();
slot2.setId(2);
slot2.setTitle("REST");
slot2.setStart("2017-01-25");
allClassesList.add(slot2);

String json = new Gson().toJson(allClassesList);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
//System.out.println("HELLO AGAIN"+json);

如果我试图从MySQL获得所有课程时间表,我会陷入困境。我正在使用MVC。现在在Servlet中是:

      TTSlotDB slot = new TTSlotDB();
      Collection<TTSlotDB> timetable = new ArrayList< TTSlotDB>();     
      timetable = slot.displayTimetable();

     String json = new Gson().toJson(timetable);
     response.setContentType("application/json");
     response.setCharacterEncoding("UTF-8");
     response.getWriter().write(json);

上述方法不会将事件带到完整日历视图。 Json在

中返回NULL
System.out.println(json);

TTSlotDB:

    public Collection<TTSlotDB> displayTimetable() throws SQLException {
    Collection<TTSlotDB> timetable = new ArrayList<TTSlotDB>();

    ConnectionPool pool = ConnectionPool.getInstance();
    Connection connection = pool.getConnection();

    Statement st = connection.createStatement();
    ResultSet rs = null;
    PreparedStatement ps = null;


    String sql = "SELECT * FROM ttslot";

    ps = connection.prepareStatement(sql);



    try {
        rs = st.executeQuery(sql);

        while (rs.next()) {

            TTSlotDB tt = new TTSlotDB();
            tt.setId(rs.getInt("ttId"));
            tt.setStart(rs.getString("start"));
            tt.setTitle(rs.getString("title"));

            timetable.add(tt);
          }
           return timetable;

    } catch (SQLException e) {
        System.out.println(e);
        return null;
    } finally {
        DBUtil.closeResultSet(rs);
        DBUtil.closeStatement(st);
        pool.freeConnection(connection);

    }
}

感谢任何帮助或建议。谢谢。

1 个答案:

答案 0 :(得分:1)

错误在于查询

while (rs.next()) {

        TTSlotDB tt = new TTSlotDB();
        tt.setId(rs.getInt("ttId"));
        tt.setStart(rs.getString("start"));
        tt.setTitle(rs.getString("title"));

        timetable.add(tt);
      }

而不是

tt.setId(rs.getInt("ttId"));

应该是

tt.setId(rs.getInt("id"));

因为数据库本身的列名是“id”,而不是“ttId”