How to update timestamp column in PostgreSQL database?

I am trying to update PostgreSQL database column. I do not know how to parse and send it to the database which matches the data type? How do I do this?

Here is my Code:

@Override
public void updateTaskStatus(String applicationNo, String prevTask, String currTask, String taskStatus,
            String user) {

        Connection conn = null;
        PreparedStatement ps = null;
        PreparedStatement ps2 = null;
        PreparedStatement ps3 = null;
        ResultSet rs = null;
        java.util.Date date = new java.util.Date();
        Timestamp timestamp = new Timestamp(date.getTime());

        try {

            conn = ConnectionManager.getConnection();

            String query = "SELECT * " + "FROM public.nt_t_task_det " + "where tsd_app_no=? ;";

            ps = conn.prepareStatement(query);
            ps.setString(1, applicationNo);
            rs = ps.executeQuery();

            if (rs.next() == true) {
                if (!(rs.getString("tsd_task_code").equals(currTask))) {

                    // insert to history table
                    String q = "INSERT INTO public.nt_h_task_his "
                            + "(tsd_seq, tsd_vehicle_no, tsd_app_no, tsd_task_code, tsd_status, created_by, created_date) "
                            + "VALUES(?, ?, ?, ?, ?, ?, ?); ";

                    ps2 = conn.prepareStatement(q);
                    ps2.setInt(1, rs.getInt("tsd_seq"));
                    ps2.setString(2, rs.getString("tsd_vehicle_no"));
                    ps2.setString(3, applicationNo);
                    ps2.setString(4, prevTask);
                    ps2.setString(5, taskStatus);
                    ps2.setString(6, rs.getString("created_by"));
                    ps2.setTimestamp(7, rs.getTimestamp("created_date"));
                    int i = ps2.executeUpdate();
                    conn.commit();
                    try {
                        if (ps2 != null)
                            ps2.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    conn.commit();

                    if (i > 0) {
                        // update task details table
                        String q2 = "UPDATE public.nt_t_task_det "
                                + "SET tsd_task_code=?, tsd_status='O', created_by=?, created_date=?  "
                                + "WHERE tsd_app_no=?";

                        ps3 = conn.prepareStatement(q2);
                        ps3.setString(1, currTask);
                        ps3.setString(2, applicationNo);
                        ps3.setString(3, user);
                        ps3.setTimestamp(4, timestamp);
                        ps3.executeUpdate();
                        conn.commit();

                        try {
                            if (ps3 != null)
                                ps3.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        String queueNumber = findQueueNumberFromApplicationNo(conn, applicationNo);
                        if (queueNumber != null && !queueNumber.isEmpty() && !queueNumber.trim().equalsIgnoreCase("")) {
                            updateQueueNumberTaskInQueueMaster(conn, queueNumber, currTask, "O");
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (ps != null)
                    ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (ps2 != null)
                    ps2.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Here is the exception in my console:

org.postgresql.util.PSQLException: ERROR: column "created_date" is of type timestamp without time zone but expression is of type character varying
Hint: You will need to rewrite or cast the expression.

Upvotes: 0

Views: 1698

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246198

The parameters of the UPDATE statement don't match the arguments you are supplying.

The first argument is for the first ?, the second one for the second and so on. You got the order mixed up.

Upvotes: 1

Related Questions