[英]Insert date to datetime type column
I want to insert a date to a mysql database table. 我想在mysql数据库表中插入日期。 The datatype of the related column in mysql database table is datetime.
mysql数据库表中相关列的数据类型是datetime。 I used the following code.
我使用了以下代码。
String date="2013.05.15";
Timestamp timestamp = new Timestamp(Long.parseLong(date));
when I am inserting timestamp value, it throws numberformat exception. 当我插入时间戳值时,它会抛出numberformat异常。 Anybody please tell me how to insert the value to the database.
有人请告诉我如何将值插入数据库。 Thank You .
谢谢 。
You're not parsing the date correctly. 您没有正确解析日期。 Actually you're trying to
其实你正在尝试
parse it as long which is a number, not a date. 解析它是一个数字,而不是一个日期。 You can use this code.
您可以使用此代码。
String date="2013.05.15";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
Date dt = sdf.parse(date);
Timestamp timestamp = new Timestamp(dt.getTime());
Either use NOW()
function of mysql or use timestamp to insert into mysql. 要么使用mysql的
NOW()
函数,要么使用timestamp插入mysql。
and make sure datatype of mysql field datetime
并确保mysql字段
datetime
数据类型
Timestamp timestamp = new Timestamp(new Date().getTime());
Timestamp dateForDb= timestamp;
insert dateForDb
into your database. 将
dateForDb
插入数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.