简体   繁体   English

更改Jackcess的数据类型定义

[英]Changing datatype definition for Jackcess

I have a program built that takes my access DB and exports to a CSV. 我建立了一个程序,该程序可以将访问数据库导出到CSV。 Everything works great but the program that takes in the exported CSV has some hardcoded regex and cannot handle the different format of certain datatypes. 一切正常,但是采用导出的CSV的程序具有一些硬编码的正则表达式,并且无法处理某些数据类型的不同格式。

Date 日期
IS: Fri May 01 00:00:00 EDT 2015 IS:2015年5月1日星期五美国夏令时00:00:00
NEEDS: 5/1/2015 00:00:00 需求:5/1/2015 00:00:00

Boolean? 布尔值?
Not sure if these field are a boolean but 不确定这些字段是否为布尔值,但
IS: TRUE or FALSE IS:正确或错误
NEEDS: 0 or 1 需求:0或1

Currency 货币
IS: 0 IS:0
NEEDS: $0.00 需:$ 0.00

Strings 弦乐
IS: string IS:字符串
NEEDS: "string" 需求:“字符串”

After reading through the docs this line jumped out at me " the row values are strongly typed Java objects. In Jackcess, the column types are represented by a Java enum named DataType. " Any help is greatly appreciated. 在阅读了文档之后,这一行对我跳了出来:“ 行值是强类型的Java对象。在Jackcess中,列类型由名为DataType的Java枚举表示。 ”非常感谢您的帮助。

Use uniVocity-parsers for that: 为此,请使用uniVocity-parsers

    Writer output = new StringWriter(); // use a FileWriter for your case

    CsvWriterSettings writerSettings = new CsvWriterSettings(); //many options here, check the tutorial

    ObjectRowWriterProcessor writerProcessor = new ObjectRowWriterProcessor(); // handles rows of objects and conversions to String.
    writerSettings.setRowWriterProcessor(writerProcessor);
    writerSettings.setHeaders("A", "B", "C", "D", "E", "F", "G", "H");

    CsvWriter writer = new CsvWriter(output, writerSettings);

    writerProcessor.convertFields(Conversions.toBoolean("0", "1")).add("C", "H"); // will write "0" and "1" instead of "true" and "false" on column "C" and "H"
    writerProcessor.convertFields(Conversions.toDate("M/d/YYYY HH:mm:ss")).add("A", "E");
    writerProcessor.convertFields(Conversions.formatToBigDecimal("$#0.00")).add("B", "D");

    writer.writeHeaders();
    writer.processRecord(new Date(), BigDecimal.TEN, true, new BigDecimal("999.99"), new Date(), "some text here", null, false);
    writer.processRecord(new Date(), BigDecimal.ZERO, false, null, null, null, "more text here", null);
    writer.close();

    System.out.println(output.toString()); //and here is the result

The output will be: 输出将是:

A,B,C,D,E,F,G,H
7/8/2015 07:09:58,$10.00,0,$999.99,7/8/2015 07:09:58,some text here,,1
7/8/2015 07:09:58,$0.00,1,,,,more text here,

Disclosure: I am the author of this library. 披露:我是这个图书馆的作者。 It's open-source and free (Apache V2.0 license). 它是开源且免费的(Apache V2.0许可证)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM