简体   繁体   English

JAVA - 更改字符串格式

[英]JAVA - Change String Format

JAVA - I have a file like the 1st one below, but I need it to look like the 2nd one to be inserted into a database later. JAVA - 我有一个类似下面第一个的文件,但我需要它看起来像第二个,以便稍后插入数据库。 Can someone help me figure out how to make it happen?有人可以帮我弄清楚如何实现它吗? I have tried using an array to read the file, but I can't get it to change the quotations correctly.我尝试使用数组读取文件,但无法正确更改引号。 The double to single quotations are key to be able to insert.双引号到单引号是能够插入的关键。 Below is minus the asterisks.下面是减去星号。

1st: Red,Blue,Green,Yellow,"11,12,13,14,15,16",Orange第一个:红色,蓝色,绿色,黄色,“11,12,13,14,15,16”,橙色
2nd: ,('Red','Blue','Green','Yellow','11,12,13,14,15,16','Orange','Date')第二个: ,('红色','蓝色','绿色','黄色','11,12,13,14,15,16','橙色','日期')

tl;dr tl;博士

Read, and write, as Comma Separated Values (CSV) data.逗号分隔值 (CSV)数据的形式读取和写入。

Tip: Use a CSV-processing library such as Apache Commons CSV .提示:使用 CSV 处理库,例如Apache Commons CSV

CSV file CSV 文件

Your text is in CSV format.您的文本采用CSV格式。

Use a library such as Apache Commons CSV to read and parse into Java objects.使用诸如Apache Commons CSV 之类的库来读取并解析为 Java 对象。 This has been addressed many many times on Stack Overflow, so search for more info and examples including entire example apps' source code.这已在 Stack Overflow 上多次解决,因此请搜索更多信息和示例,包括整个示例应用程序的源代码。

Then write out those objects as text wrapped with the single quote marks.然后将这些对象写为用单引号括起来的文本。 The CSV library may help there too, if it has options to force quote-enclosing on every element, and specify the kind of quote mark. CSV 库也可能对此有所帮助,如果它具有在每个元素上强制使用引号括起来的选项,并指定引号的类型。

String input = "Red,Blue,Green,Yellow,\"11,12,13,14,15,16\",Orange";

try (
        Reader reader = new java.io.StringReader( input ) ;
)
{
    Iterable < CSVRecord > records = CSVFormat.RFC4180.parse( reader );
    for ( CSVRecord record : records )
    {
        // Dump to console.
        record.iterator().forEachRemaining( string -> System.out.println( string ) );
        // Write now data.
        StringBuilder builder = new StringBuilder( "(" );
        record.iterator().forEachRemaining( string -> builder.append( "'" ).append( string ).append( "'" ).append( "," ) ); // Leaves an extra comma at end.
        builder.deleteCharAt( builder.lastIndexOf( "," ) ); // Delete that extra comma at end.
        builder.append( ")" );
        System.out.println( "builder.toString() = " + builder.toString() );
    }
}
catch ( IOException e )
{
    e.printStackTrace();
}

When run:运行时:

Red
Blue
Green
Yellow
11,12,13,14,15,16
Orange
builder.toString() = ('Red','Blue','Green','Yellow','11,12,13,14,15,16','Orange')

But I suspect your greater goal of inserting into database is better served by properly using JDBC.但我怀疑正确使用 JDBC 可以更好地实现插入数据库的更大目标。 Read on for details.请继续阅读以了解详细信息。

PreparedStatement::set…

You appear to be taking the wrong approach to working with JDBC.您似乎采用了错误的方法来使用 JDBC。

Do not bother manipulating your text into being a SQL-compliant string.不要费心将您的文本操作为符合 SQL 的字符串。 Do not pass your data arguments to SQL as a block of text.不要将您的数据 arguments 作为文本块传递给 SQL。

Use a prepared statement to guard against SQL-injection attacks.使用准备好的语句来防范 SQL 注入攻击。 Pass each data argument with a call to a set… command.通过调用set…命令传递每个数据参数。

myPreparedStatement.setString( 1 , "Red" ) ; 

Of course you would be passing that Red text in a variable rather than a literal.当然,您将在变量而不是文字中传递Red文本。

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

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