Parsing CSV and exporting to the Mysql database in Grails

I am new to Groovy and Grails. I want to send a parsing CSS file and export it to several MySQL database tables. I looked at some kind of encoding, but it baffled me as a beginner. So can someone help me in understanding a simple csv file. Parsing and exporting to a MySQL database.

Thanks Sleep

+2
source share
2 answers

Grails is a boot process that starts whenever your application starts. His graceful; You can configure it for different actions in different environments.

One approach is to do the following in bootstrap:

1) csv, , .
2) , , doDomainObject.save()

.

, -

new File(filePath).splitEachLine(',') {fields ->
    def domainObject = new YouDomainObject(
        id: fields[0].trim(),
        name: fields[1].trim()
    )

    if (domainObject.hasErrors() || domainObject.save(flush: true) == null) {
        log.error("Could not import domainObject  ${domainObject.errors}")
    }

    log.debug("Importing domainObject  ${domainObject.toString()}")
}
+11

Groovy Java, Java opencsv, CSV, .

0

Source: https://habr.com/ru/post/1523860/


All Articles