Java Oracle Mysql中的时间戳插入问题
我试图将时间戳值从oracle插入mysql时间戳列。 oracle值为2017-09-01 11:35:22.495000000,但从结果集中获取值时,其给出的值为2017-09-01 11:35:22.495 它使用oracle.sql.timestamp存储在oracle中,我无法在mysql中插入该值。因此,从oracle.sql.timestamp API获取stringvalue或timestamp值 但是mysql存储的值是2017-09-01 11:35:22.000495,数据类型定义为时间戳(6),我不知道为什么要这样插入值Java Oracle Mysql中的时间戳插入问题,java,Java,我试图将时间戳值从oracle插入mysql时间戳列。 oracle值为2017-09-01 11:35:22.495000000,但从结果集中获取值时,其给出的值为2017-09-01 11:35:22.495 它使用oracle.sql.timestamp存储在oracle中,我无法在mysql中插入该值。因此,从oracle.sql.timestamp API获取stringvalue或timestamp值 但是mysql存储的值是2017-09-01 11:35:22.000495,数据
如何在mysql中存储类似于oracle的值?使用JDBC,您应该能够直接将时间戳从一个数据库复制到另一个数据库,这样做:
try(Connection oracleConnection = getOracleConnection();
Connection mysqlConnection = getMySQLConnection();
PreparedStatement oracleStmt = oracleConnection.prepareStatement("SELECT my_time FROM oracle_table");
PreparedStatement mysqlStmt = mysqlConnection.prepareStatement("INSERT INTO mysql_table VALUES (?)");
ResultSet rs = oracleStmt.executeQuery()) {
while(rs.next) {
mysqlStmt.setTimestamp(1, rs.getTimestamp("my_time"));
mysqlStmt.execute();
}
}
时间戳本质上是数字数据类型。不同的DBMS可以有不同的精度和处理时区的不同方式,但在大多数情况下,您不需要任何特定于数据库的API来与它们交互
如果需要格式化时间戳,可以使用从getTimestamp()
返回的内容格式化字符串