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?