tags:

views:

21

answers:

2

I have the following calls in one of my classes

@Override
public Integer save(NewsItem item){
    ConnectionPool pool = new ConnectionPool();
    Connection connection = pool.getConnection();
    PreparedStatement ps = null;
    try{
        String query = "INSERT INTO newsItem (type,title,content,link,layout,imageUri,timestamp)" +
                       "VALUES (?,?,?,?,?,?,?)";
        ps = connection.prepareStatement(query);
        ps.setInt(1,item.getType());
        ps.setString(2,item.getTitle());
        ps.setString(3,item.getContent());
        ps.setString(4,item.getLink());
        ps.setInt(5,item.getLayout());
        ps.setString(6,item.getImageUri());
        ps.setDate(7,item.getTimestamp());

        return ps.executeUpdate();
    }
    catch(SQLException e){
        e.printStackTrace();
        return 0;
    }
    finally{
        ConnectionUtility utility = new ConnectionUtility();
        utility.closePreparedStatement(ps);
        pool.freeConnection(connection);
    }
}

The NewsItem POJO has the following properties

    private Integer id;
    private Integer type;
    private String title;
    private String content;
    private String link;
    private Integer layout;
    private String imageUri;
    private Date timestamp;

Everything works and has been tested except for the timestamp call which is

ps.setDate(7,item.getTimeStamp())

I am able to set the Date on the NewsItem object by calling

item.setTimestamp(new Date());

but I get the error from my IDE (Eclipse) that tells me the following message

The method setDate(int,Date) in the type NewsItemDAO is not applicable for the arguments setDate(int,Date)

This has been a plague for me throughout the life of this application I have been working on because I have had to result to storing the timestamps as string for the time being.

If the column in my MySQL database is of type DATETIME is there a different way I should be saving that timestamp? Or is there something wrong with the call?

+1  A: 

Check your imports - My guess is that you are using java.util.Date instead of java.sql.Date.

If possible, it's a good idea to use java.sql.Timestamp which extends java.util.Date to avoid this confusion and enhance the readability of your code.

dpatch
The problem is in this particular case not in the imports. The `Timestamp` recommendation is however seconded, but not only due to readability.
BalusC
+2  A: 

The setDate() expects java.sql.Date, not java.util.Date.

ps.setDate(7, new java.sql.Date(item.getTimestamp().getTime()));

But the java.sql.Date contains only the date part of datetime, not the time part. You'd like to use java.sql.Timestamp instead.

ps.setTimestamp(7, new Timestamp(item.getTimestamp().getTime()));
BalusC
Does this method work well with MySQL and can I still use java.sql.Timestamp the way Java.util.Date is used? Should the MySQL datatype for this be TIMESTAMP or DATETIME?
CitadelCSAlum
You should not use `java.sql.Timestamp` in your model object (POJO as you call it). Use it only during persisting in DB. On retrieving you can just upcast `ResultSet#getTimestamp()` to `java.util.Date`. As to the types, you can use both. The difference is only that `TIMESTAMP` is implicitly converted to UTC timezone and thus more recommended to avoid timezone headaches and unportability.
BalusC
excellent! I made the changes. Everything looks great as long as I can go back and forth with the Date type on my POJO and the value when I pull it out of the Database.
CitadelCSAlum
You're welcome.
BalusC