让我们摆放下面的桌子:
CREATE MULTISET TABLE DATABASE.TABLE ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
START_DATE TIMESTAMP(0) FORMAT 'DD-MMM-YYYYBHH:MI:SS',
STATUS DECIMAL(5,0) ,
PROCESS VARCHAR(100) CHARACTER SET LATIN CASESPECIFIC NOT NULL
)
PRIMARY INDEX ( PROCESS)
UNIQUE INDEX ( PROCESS, START_DATE );
我想做的是使用Java上的PreparedStatement插入到该表中,但是它不起作用。
那就是我在Java上所做的:
setConnection(); //IT IS OK, SO DONT WORRY!! =)
String insert = "insert into DATABASE.TABLE (START_DATE , STATUS, PROCESS)";
PreparedStatement pstm = getConnection().prepareStatement(insert);
ArrayList<Object> values = new ArrayList<Object>();
values.add(0, new java.sql.Timestamp(calendar.getTime().getTime()));
values.add(1, new Integer(0));
values.add(2, "TEST_PROCESS");
for (int i = 0; i < values.size(); i++)
{
if (values.get(i).getClass().equals(Integer.class))
pstm.setInt(i+1, (Integer) values.get(i));
if (values.get(i).getClass().equals(String.class))
pstm.setString(i+1, (String) values.get(i));
if (values.get(i).getClass().equals(Timestamp.class))
pstm.setTimestamp(i+1, (Timestamp) values.get(i));
if (values.get(i).getClass().equals(Date.class))
pstm.setDate(i+1, (Date) values.get(i));
if (values.get(i).getClass().equals(Double.class))
pstm.setDouble(i+1, (Double) values.get(i));
if (values.get(i).getClass().equals(Float.class))
pstm.setFloat(i+1, (Float) values.get(i));
}
pstm.execute();
那就是我得到的错误:
java.sql.SQLException: [Teradata Database] [TeraJDBC 15.10.00.05] [Error 5404] [SQLState HY000] Datetime field overflow.
at com.teradata.jdbc.jdbc_4.util.ErrorFactory.makeDatabaseSQLException(ErrorFactory.java:308)
at com.teradata.jdbc.jdbc_4.statemachine.ReceiveInitSubState.action(ReceiveInitSubState.java:109)
at com.teradata.jdbc.jdbc_4.statemachine.StatementReceiveState.subStateMachine(StatementReceiveState.java:307)
at com.teradata.jdbc.jdbc_4.statemachine.StatementReceiveState.action(StatementReceiveState.java:196)
at com.teradata.jdbc.jdbc_4.statemachine.StatementController.runBody(StatementController.java:123)
at com.teradata.jdbc.jdbc_4.statemachine.PreparedStatementController.run(PreparedStatementController.java:46)
at com.teradata.jdbc.jdbc_4.TDStatement.executeStatement(TDStatement.java:386)
at com.teradata.jdbc.jdbc_4.TDStatement.executeStatement(TDStatement.java:328)
at com.teradata.jdbc.jdbc_4.TDPreparedStatement.doPrepExecute(TDPreparedStatement.java:165)
at com.teradata.jdbc.jdbc_4.TDPreparedStatement.execute(TDPreparedStatement.java:2598)
at ProcessStatus.set(ProcessStatus.java:71)
at CMain.main(CMain.java:42)
我知道我可以例如将START_DATE数据类型更改为TIMESTAMP(3),但这似乎并不是一件容易的事,所以这就是为什么我寻求您的帮助。
无论如何,我感谢您的时间和帮助。谢谢大家!!!
最佳答案
好吧,我找到了一种方法。
基本上我已经使用了SimpleDateFormat并将Timestamp添加为字符串而不是Timestamp对象。
像这样的东西:
Calendar calendar = Calendar.getInstance();
Timestamp ts = new java.sql.Timestamp(calendar.getTime().getTime());
String mydate = new SimpleDateFormat("yyy-MM-dd hh:mm:ss").format(ts);
ArrayList<Object> values = new ArrayList<Object>();
values.add(0, mydate);
最终代码如下:
setConnection(); //IT IS OK, SO DONT WORRY!! =)
String insert = "insert into DATABASE.TABLE (START_DATE , STATUS, PROCESS)";
PreparedStatement pstm = getConnection().prepareStatement(insert);
Calendar calendar = Calendar.getInstance();
Timestamp ts = new java.sql.Timestamp(calendar.getTime().getTime());
String mydate = new SimpleDateFormat("yyy-MM-dd hh:mm:ss").format(ts);
ArrayList<Object> values = new ArrayList<Object>();
values.add(0, mydate);
values.add(1, new Integer(0));
values.add(2, "TEST_PROCESS");
for (int i = 0; i < values.size(); i++)
{
if (values.get(i).getClass().equals(Integer.class))
pstm.setInt(i+1, (Integer) values.get(i));
if (values.get(i).getClass().equals(String.class))
pstm.setString(i+1, (String) values.get(i));
if (values.get(i).getClass().equals(Timestamp.class))
pstm.setTimestamp(i+1, (Timestamp) values.get(i));
if (values.get(i).getClass().equals(Date.class))
pstm.setDate(i+1, (Date) values.get(i));
if (values.get(i).getClass().equals(Double.class))
pstm.setDouble(i+1, (Double) values.get(i));
if (values.get(i).getClass().equals(Float.class))
pstm.setFloat(i+1, (Float) values.get(i));
}
pstm.execute();
谢谢,希望对您有所帮助!!