1

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);


    }

1 Answer 1

1

It's doing it correctly according to your code. You wrote:

SessioneLettura s = 
  new SessioneLettura((sdf.format(p.getTimestamp("data"))),pagina);

In this line, you wrote p.getTimestamp("data"). You are calling the JDBC driver to retrieve the TIMESTAMP WITH TIME ZONE column to a java.sql.Timestamp. The JDBC driver automatically converts the value to the time zone of the current JVM.

Then, you are printing it using the format dd.MM.yyyy HH.mm. If want to see the time zone of the retrieved value, ad z to the format, as in dd.MM.yyyy HH.mm z.

Sign up to request clarification or add additional context in comments.

3 Comments

The problem isn't the timezone, it's that it prints '01.01.1970' instead of the data in the db.
What's the column type in the table?
time with time zone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.