有两个表 1.Transcation_Details 和 2.Flags 都定义为 1. Cust_ID(主键), last_Login_date; 2. Cust_ID(主键), emailFlag, OfflineFlag, InactiveFlag;
在java程序中:
diff = current_date - last_Login_date
if (diff > 30 and diff <60) set emailFlag as 'Y'
else if( diff > 60 and diff < 120) set OfflineFlag as 'Y'
else if(diff > 120) set InactiveFlag as 'Y'
这段代码可以吗?我可以做得更好吗?
public class SetFlags {
//to edit and feed the date from the temporary table to the fact table
static String sql, sql1, tableName;
private static PreparedStatement pstmt = null, pstmt1 = null;
private static Statement stmt = null;
private static ResultSet resultset = null;
private static ResultSet resultset2 = null;
static SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
public static int differenceOfDays(Date d1, Date d2) {
int daysdiff = 0;
long diff = d2.getTime() - d1.getTime();
long diffDays = diff / (24 * 60 * 60 * 1000) + 1;
daysdiff = (int) diffDays;
return daysdiff;
}
public static void setFlag(String query) throws SQLException {
//String query = "UPDATE Flags SET f.EmailFlag = 'Y' from Flags f, Transaction_Details t where f.Cust_ID = t.Cust_ID";
stmt = ManageDBResource.conn.createStatement();
//Statement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
int result = stmt.executeUpdate(query);
if(result == 1) {
System.out.println("Flag Set");
}
else {
System.err.println("error in updating Flag");
}
}
public static void setFlags() throws SQLException {
sql = "Select * From Transaction_Details";
sql1 = "Select * From Customer_Info";
ManageDBResource.createConnectionToDB();
pstmt = ManageDBResource.conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
pstmt1 = ManageDBResource.conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
resultset = pstmt.executeQuery();
resultset2 = pstmt.executeQuery();
while(resultset.next()) {
java.util.Date last_Login_Date = resultset.getDate("LAST_TIME_LOGIN_DATE");
java.util.Date currentDate = new java.util.Date();
int diff = differenceOfDays(last_Login_Date, currentDate);
if(diff > 30 && diff < 60) {
setFlag("UPDATE Flags SET EmailFlag = 'Y' from Flags where Cust_ID = " + Integer.toString(resultset.getInt("Cust_ID")));
}
else if(diff > 60 && diff < 120) {
setFlag("UPDATE Flags SET OfflineFlag = 'Y' from Flags where Cust_ID = " + Integer.toString(resultset.getInt("Cust_ID")));
}
else if(diff > 120) {
setFlag("UPDATE Flags SET InactiveFlag = 'Y' from Flags where Cust_ID = " + Integer.toString(resultset.getInt("Cust_ID")));
}
}
}
如何使用 PreparedStatement 对象编写和执行查询以更新 java 中的标志。
请提出您喜欢的任何方式。
最佳答案
您可以对所有情况使用单个更新,例如:
UPDATE Flags f
INNER JOIN Transcation_Details t on t.Cust_ID = f.Cust_ID
SET f.EmailFlag = case when datediff(curdate(), t.last_login_date) > 30
and datediff(curdate(), t.last_login_date) <60 then 'Y' else f.EmailFlag end
, f.OfflineFlag = case when datediff(curdate(), t.last_login_date) > 60
and datediff(curdate(), t.last_login_date) <120 then 'Y' else f.OfflineFlag end
, f.InactiveFlag = case when datediff(curdate(), t.last_login_date) > 12 then 'Y' else f.InactiveFlag end
where f.cust_ID = :your_cust_id
关于java - 我如何使用 java 在 mysql 数据库中根据日期差异(来自另一个表的日期和当前日期)将表中的特定标志更新为 'Y',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51408061/