ResultSet.getString(Date)因驱动程序而异 [英] ResultSet.getString(Date) differs based on driver
问题描述
我正在使用"Oracle Database 11g企业版11.2.0.2.0版-具有分区,OLAP,数据挖掘和实际应用程序测试选项的64位生产"
I am using "Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options"
我有一个表,其表为
COLUMN_NAME DATA_TYPE DATA_TYPE_MOD DATA_TYPE_OWNER DATA_LENGTH
CITY VARCHAR2 (null) (null) 30
COUNTRY VARCHAR2 (null) (null) 30
DATE_TYPE DATE (null) (null) 7
PARTNO NUMBER (null) (null) 22
STATE VARCHAR2 (null) (null) 30
ZIP VARCHAR2 (null) (null) 30
我已经编写了一个简单的Java客户端来获取DATE_TYPE
.
I have written a simple java client to fetch the DATE_TYPE
.
public class DateIssue {
private void testDateOutput() {
Connection con = null;
Statement psmt = null;
try {
con = getConnection();
con.setAutoCommit(true);
psmt = con.createStatement();
String sql = "SELECT DATE_TYPE FROM EMP";
ResultSet rs = psmt.executeQuery(sql);
while (rs.next()) {
String dateString = rs.getString(1);
System.out.println("As String :"+ dateString);
}
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
private static Connection getConnection() {
Connection connection = null;
try {
Class.forName("oracle.jdbc.pool.OracleDataSource");
java.util.Properties info = new java.util.Properties();
info.put("user", "myuser");
info.put("password", "mypass");
info.put("oracle.jdbc.mapDateToTimestamp", "false");
connection = DriverManager.getConnection("jdbc:oracle:thin:@myserver:1521:myservicename", info);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void main(String rgs[]) throws Exception {
DateIssue di = new DateIssue();
di.testDateOutput();
System.out.println("----------------------------------------------------");
}
对于ojdbc6.jar(12.1.0.1.0),输出为:As String :2013-11-12
使用ojdbc6.jar(11.2.0.2.0)时,输出为:As String :2013-11-12 11:10:09
With ojdbc6.jar(12.1.0.1.0) the output is : As String :2013-11-12
With ojdbc6.jar(11.2.0.2.0) the output is : As String :2013-11-12 11:10:09
Java版本:1.7
Java version : 1.7
为什么ojdbc6.jar(12.1.0.1.0)中的行为发生了变化?
如果我需要使用ojdbc6.jar(12.1.0.1.0)格式为2013-11-12 11:10:09
的输出,该怎么办?
Why the behavior changes in ojdbc6.jar(12.1.0.1.0) ?
If I need the output in the format 2013-11-12 11:10:09
using ojdbc6.jar(12.1.0.1.0) what should I do?
推荐答案
您不应该从不依靠驱动程序将日期隐式转换为任何特定的字符串格式-该格式是司机.您应该自己处理转换.
You should never rely on the driver to implicitly convert a date to any particular string format - the format is an implementation detail of the driver. You should handle the conversion yourself.
这可以在Java级别完成:
This can either be done in the Java level:
/* executing the statement, etc. - snipped for clarity */
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while (rs.next()) {
Date date = rs.getTimestamp(1);
System.out.println("As String :"+ formatter.format(date));
}
或通过查询本身:
/* Setting up the connection, etc. - snipped for clarity */
String sql = "SELECT TO_CHAR(date_type, 'yyyy-mm-dd hh24:mi:ss') FROM emp";
ResultSet rs = psmt.executeQuery(sql);
while (rs.next()) {
String dateString = rs.getString(1);
System.out.println("As String :" + dateString);
}
这篇关于ResultSet.getString(Date)因驱动程序而异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!