MySQL ResultSet JsonWriter:jsonWriter.value((java.sql.Date)resultSet.getObject(column));

时间:2017-03-08 06:25:32

标签: java mysql json

我想用

编写一个MySQL Date列值
jsonWriter.value((java.sql.Date) resultSet.getObject(column));

但似乎不可能那样。它表示long,Date,String都不适用于该类型。我怎样才能做到这一点?

完整代码:

private static void writeField(final ResultSet resultSet, final JsonWriter jsonWriter, final ResultSetMetaData metaData, final int column) throws SQLException, IOException {
final int columnType = metaData.getColumnType(column);

switch ( columnType ) {
    case Types.BIT:
    case Types.TINYINT:
    case Types.SMALLINT:
        throw new UnsupportedOperationException("TODO: " + JDBCType.valueOf(columnType));
    case Types.INTEGER:
        jsonWriter.value((Integer) resultSet.getObject(column));
        break;
    case Types.BIGINT:
    case Types.FLOAT:
    case Types.REAL:
    case Types.DOUBLE:
    case Types.NUMERIC:
    case Types.DECIMAL:
    case Types.CHAR:
        throw new UnsupportedOperationException("TODO: " + JDBCType.valueOf(columnType));
    case Types.VARCHAR:
        jsonWriter.value((String) resultSet.getObject(column));
        break;
    case Types.LONGVARCHAR:
        jsonWriter.value((String) resultSet.getObject(column));
        break;
    case Types.DATE:
// here's the problem. i dont know to what to case the column.
        jsonWriter.value((java.sql.Date) resultSet.getObject(column));
        break;
    case Types.TIME:
    case Types.TIMESTAMP:
        jsonWriter.value((long) resultSet.getObject(column));
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
    case Types.NULL:
    case Types.OTHER:
    case Types.JAVA_OBJECT:
    case Types.DISTINCT:
    case Types.STRUCT:
    case Types.ARRAY:
    case Types.BLOB:
    case Types.CLOB:
    case Types.REF:
    case Types.DATALINK:
    case Types.BOOLEAN:
    case Types.ROWID:
    case Types.NCHAR:
    case Types.NVARCHAR:
    case Types.LONGNVARCHAR:
    case Types.NCLOB:
    case Types.SQLXML:
    case Types.REF_CURSOR:
    case Types.TIME_WITH_TIMEZONE:
    case Types.TIMESTAMP_WITH_TIMEZONE:
        throw new UnsupportedOperationException("TODO: " + JDBCType.valueOf(columnType));
    default:
        throw new UnsupportedOperationException("Unknown type: " + columnType);
    }
}

我还需要稍后实现时间戳 - 以防它们相关。

非常感谢! :)

1 个答案:

答案 0 :(得分:0)

好的,我终于找到了怎么做(我希望,不是100%肯定,因为我无法在客户端显示数据 - 但它编译并运行。这是代码:

FileLoadException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

System.Net.Http.WinHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
AggregateException: Unhandled remote failure.

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler+<HandleRemoteCallbackAsync>d__6.MoveNext()

我希望我没有以同样的方式将其格式化两次? :)

这里还有时间戳的代码:

    case Types.DATE:
        java.sql.Date sqlDate = (java.sql.Date) resultSet.getObject(column);
        LocalDate localDate = sqlDate.toLocalDate();
        DateTimeFormatter formatDt = DateTimeFormatter.ISO_LOCAL_DATE;
        String formattedDate = localDate.format(formatDt);
        jsonWriter.value((String) formattedDate);
        break;