Reputation: 357
Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
java.sql.Time currentTimestamp = new java.sql.Time(now.getTime());
Date date = new Date(currentTimestamp.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(date);
java.sql.Date.valueOf(formattedDate);
System.out.println(formattedDate);
How to get the current formatted System time(yyyy-MM-dd) and insert it to the table? I already tried my best and search for the answer but its no good I get different error messages like when I insert java.sql.Date.valueOf(formattedDate);
I always get java.lang.IllegalArgumentException
but when I remove java.sql.Date.valueOf(formattedDate)
and change this line "+pcList.get(j).getid()+",0,0
into "+pcList.get(j).getid()+",0,2014-07-07
I always get an error saying Data truncation: Incorrect date value: '2000' for column 'date' at row 1
if(ifLoggedIn){
for(int j=0;j<b;j++){
try {
Connection conn = DriverManager.getConnection(url, "root", "");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT MAX(transactionID) from transaction");
while(rs.next()){
tID = rs.getInt("MAX(transactionID)");
}
tID= tID+1;
stmt.executeUpdate("Insert into ccpdb.transaction(membershipNum,productID,time,date,paymentOption,amount,transactionID,quantity) values ("+currentuser.getMembershipNum()+","+pcList.get(j).getid()+",0,0,\"west\","+tf1.getText()+","+tID+","+pcList.get(j).f3.getText()+")"
+ "");
conn.close();
} catch (Exception g) {
System.err.println("Got an exception! ");
System.err.println(g.getMessage());
}
}
}
Please help me!!
Upvotes: 0
Views: 161
Reputation: 3819
Try this:
java.util.Date now = new java.util.Date();
java.sql.Date insertDate = new java.sql.Date(now.getTime());
You can insert this date directly.
You are getting this error as you are trying to save String in date column.
Upvotes: 0
Reputation: 1270873
Just use the database time, rather than the java time. You can get it using the function now()
or CURRENT_DATE
. I can't quite tell where you want to put this in the insert
, but it can go in the list of values.
Upvotes: 1