0

我需要将当前日期 +7 天添加到我的 sql 数据库中并将其取回我该怎么做?

Calendar cal = Calendar.getInstance();
                        cal.add(Calendar.DATE, 7); // add 7 days  
                        int date = cal.get(Calendar.DATE);
                        int month = cal.get(Calendar.MONTH);
                        int year = cal.get(Calendar.YEAR);
                        String newdate = Integer.toString(date);
                        String concat = newdate.concat("-" + Integer.toString(cal.get(Calendar.MONTH)) + "-" + Integer.toString(cal.get(Calendar.YEAR)));

我找到了这段代码

java.util.Date newDate = new Date(result.getDate("VALUEDATE").getTime());
4

2 回答 2

1

您可以直接在 MySQL 中执行此操作

update your_table
set date_column = date_column + interval 7 day

文档

于 2012-10-17T16:51:32.290 回答
0

下载 Java MySql 连接器库http://dev.mysql.com/downloads/connector/j/并将 jar 文件添加到构建路径。

现在您可以添加到您的表

    Connection conn = null;
    try {
        conn =  DriverManager.getConnection("<databaseURLHere>" + "user=<usernameHere>&password=<passwordHere");
        Statement stmt = conn.createStatement();
        String uid = UUID.randomUUID().toString();
        uid = uid.replaceAll("-", "");
        uid = uid.substring(0, 16);
        if (stmt.execute("<sql query here>")) {
        //    rs = stmt.getResultSet();
        }

    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    }
于 2012-10-17T16:55:03.947 回答