I'm trying to access to a database using these functions. I have already tested the query and it works, but I have two problems: 1)I have data in Timestamp with zone type on my PostgreSQL database and when I try to print them in java it prints for example
01.01.1970 13.35
instead of the value written in the db.
2)When I print out the urls using the servlet, it prints the first value in the database repeated for the number of the row that it should print, while in the DAO class it prints the right values of url.
For example in the DAO it prints:
http://seesaa.net/cubilia.jpg01.01.1970 13.35
http://seesaa.net/cubilia.jpg01.01.1970 14.07
https://msn.com/sit/amet/consectetuer/adipiscing/elit.aspx01.01.1970 14.10
http://examiner.com/ultrices/posuere/cubilia/curae/donec/pharetra.png01.01.1970 14.27
https://google.co.uk/est/donec/odio/justo/sollicitudin/ut/suscipit.js01.01.1970 14.30
http://seesaa.net/cubilia.jpg01.01.1970 14.32
https://dyndns.org/nulla/justo.js01.01.1970 14.46
http://seesaa.net/cubilia.jpg01.01.1970 15.04
and in the servlet:
http://seesaa.net/cubilia.jpg
http://seesaa.net/cubilia.jpg
http://seesaa.net/cubilia.jpg
http://seesaa.net/cubilia.jpg
http://seesaa.net/cubilia.jpg
http://seesaa.net/cubilia.jpg
http://seesaa.net/cubilia.jpg
http://seesaa.net/cubilia.jpg
This is the DAO class:
public static ArrayList<SessioneLettura> cronologiaPagine(int id) {
DBManager dbmanager = new DBManager();
Connection conn = dbmanager.getConnection();
PreparedStatement pstmt;
Pagina pagina= new Pagina();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH.mm");
ArrayList<SessioneLettura> sessionelettura = new ArrayList<SessioneLettura>();
try {
pstmt = conn.prepareStatement(
"SELECT sessionelettura.url as url, sessionelettura.data as data FROM sessionelettura INNER JOIN sessione ON codice= sessionelettura.sessione WHERE sessione.utente=?");
pstmt.setInt(1, id);
ResultSet p = pstmt.executeQuery();
while (p.next()) {
pagina.setUrl(p.getString("url"));
SessioneLettura s = new SessioneLettura((sdf.format(p.getTimestamp("data"))),pagina);
System.out.println(s.getPagina().getUrl() + "" + s.getDataletta());
sessionelettura.add(s);
}
conn.commit();
pstmt.close();
conn.close();
} catch (
SQLException e) {
e.printStackTrace();
}
return sessionelettura;
}
And this is the servlet:
private void cronologia(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("idUtente"));
for (SessioneLettura sa : DataAccess.cronologiaPagine(id)) {
String url = sa.getPagina().getUrl();
System.out.println(url);
}
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Cronologia.jsp?&idUtente="+id);
request.setAttribute("cronologia", DataAccess.cronologiaPagine(id));
dispatcher.forward(request, response);
}