我正在寻找通过我的结果集来更新MySQL中的DateTime字段的最佳方法。我在这些行中找到了一些其他问题,但没有一个问题解决了rs.updateDate语句中的Java数据和SQL日期格式。我附上了编辑正在讨论的代码。
public class EmailQueueProcess {
public static Boolean process()
{
Date processedDtm = new Date();
java.util.Date today=new java.util.Date();
Timestamp currentTimestamp=new Timestamp(today.getTime());
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
strSQL = "select * from portal.emailqueue where portal.emailqueue.processedDtm is null";
conn = com.retailapppartners.Utils.staticGetConnection().getConnection();
stmt = conn.prepareStatement(strSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
//now loop through and update the process datetime values
rs = stmt.executeQuery();
ResultSetMetaData rsMetaData = (ResultSetMetaData) rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
while (rs.next()) {
// Grab some data then update the row
rs.updateDate ("processedDtm", currentTimestamp);
rs.updateRow();
}
} catch .....
答案 0 :(得分:0)
updateDate()
需要java.sql.Date
。所以,我相信你有两个选择,
rs.updateTimestamp("processedDtm", currentTimestamp);
java.sql.Date currentTimestamp = new java.sql.Date();
根据表5.1中的MySQL Documentation,
These MySQL Data Types - Can always be converted to these Java types
DATE, TIME, DATETIME, TIMESTAMP - java.lang.String, java.sql.Date, java.sql.Timestamp
所以你可以使用上面的1或2。