[英]How to convert Java String to Teradata DATE and Timestamp
My java code generates two Strings
: 我的Java代码生成两个
Strings
:
String myDate = "10/10/2013";
String myTimestamp = "2013-10-09 14:30:20";
I need to feed these values to a prepared statement
, so that I could upload them using jdbc
to Teradata
我需要将这些值提供给
prepared statement
,以便可以使用jdbc
将它们上载到Teradata
Here is what I tried : 这是我尝试的:
String in = " INSERT INTO " + myTab + " VALUES (?,?) ";
PreparedStatement prst = null;
prst = connection.prepareStatement(in);
// add date
prst.setDate(1, (Date) new SimpleDateFormat("MM/dd/yyyy").parse(myDate));
//add timestamp
prst.setDate(2, (Date) new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(myTimestamp));
The above code compiles but does not work. 上面的代码可以编译,但是不起作用。 I get an
empty string
error . 我收到一个
empty string
错误。 How can I convert a String
into Teradata types DATE, TIMESTAMP
in order to add them to the prepared statement ? 如何将
String
转换为Teradata types DATE, TIMESTAMP
,以将其添加到准备好的语句中?
You could use the java.sql.Date
constructor that takes a long
, by using Date#getTime()
and changing from 您可以使用需要
long
时间的java.sql.Date
构造函数 ,方法是使用Date#getTime()
并从
prst.setDate(1, (Date) new SimpleDateFormat("MM/dd/yyyy").parse(myDate));
to something like 像
prst.setDate(1, new java.sql.Date(new SimpleDateFormat("MM/dd/yyyy")
.parse(myDate)).getTime());
and the other one 另一个
prst.setDate(2, new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse(myTimestamp)).getTime());
Try this: 尝试这个:
prst.setDate(1, new java.sql.Date(new SimpleDateFormat("MM/dd/yyyy").parse(myDate).getTime()));
This will convert your date then create a new sqlDate. 这将转换您的日期,然后创建一个新的sqlDate。
prst.setTimestamp(2, new java.sql.Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(myTimeStamp).getTime());)
for the timestamp use setTimeStamp not setDate. 对于时间戳,请使用setTimeStamp而不是setDate。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.