0

I am trying to insert Date and time in a DATETIME type column through session.createSQLQuery() method and getting java.lang.NullPointerException.

Thanks in Advance.

Controller class Code:

    package dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class AccountDAO {
    private HibernateTemplate template;
    Session session;
    int returnValue=0;

    public HibernateTemplate getTemplate() {
        return template;
    }
    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }
    public int insertLogoutTime(int srNum, String userName) {
        System.out.println("------>Inside AccountDAO<------");
        System.out.println("*************-- In insertLogoutTime() Method--************");
        try{
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = new Date();
            //System.out.println("update login_info set Logout_DateTime ='"+sdfDate.format(now)+"'" +" where SrNum= "+srNum+"");

            Query query=session.createSQLQuery("update login_info set Logout_DateTime =:theDate where SrNum=:srNum");
            query.setParameter("theDate", now);
            query.setParameter("srNum", srNum);
            returnValue= query.executeUpdate();
             System.out.println(returnValue);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }
}

Error:

17:50:21,167 ERROR [STDERR] java.lang.NullPointerException
Pankaj Dagar
  • 87
  • 10
  • 1
    Look at this about [Null Pointers](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OcelotcR Jan 05 '18 at 12:25
  • You shouldn’t be using the long outdated `Date` and the notoriously trouble some `SimeplDateFormat` classes. Can’t you pass a `LocalDateTime` to Hibernate? This class is from [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 05 '18 at 13:15

2 Answers2

1

Don't build SQL like this; use parameters:

   Query query=session.createSQLQuery("update login_info set Logout_DateTime =:theDate where SrNum=:srNum");
    query.setParameter("theDate", now);
    query.setParameter("srNum", srNum);
    returnValue= query.executeUpdate();
     System.out.println(returnValue);

UPDATE

Try this:

    query.setTimestamp("theDate", now);
    query.setString("srNum", srNum);
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

You are not using the query parameter replacement so either add single quotes:

 Query query=session.createSQLQuery("update login_info set Logout_DateTime 
        ='"+sdfDate.format(now)+"'" +" where SrNum= "+srNum+"");

or use a param:

 Query query=session.createSQLQuery("update login_info set Logout_DateTime = :datetime
 where SrNum= "+srNum+"");
query.setString("datetime", sdfDate.format(now));

In mysql you set the datetime column by placing the value inside single quotes

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63