user1750832
user1750832

Reputation: 75

Java : How to add date in to mysql database and get back

I need to add current date +7 day in to my sql database and retrieve it back how do i do that?

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)));

I found this code

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

Upvotes: 0

Views: 406

Answers (2)

Tom G
Tom G

Reputation: 3660

Download the Java MySql Connector library http://dev.mysql.com/downloads/connector/j/ and add the jar file to the build path.

Now you can add to your table

    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());
    }

Upvotes: 0

juergen d
juergen d

Reputation: 204924

You can do that in MySQL directly

update your_table
set date_column = date_column + interval 7 day

Doc

Upvotes: 1

Related Questions