I import a file and read its content. I then save the content directly to database. The code sample is as follows.
def file = request.getFile('file')
if (file.empty) {
flash.message = "File cannot be empty"
return
}
String content = new String(file.getBytes())
Product product = new Product()
product.description = content
product.save(flush:true, failOnError:true)
The save fails throwing the following error.
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not insert: [Product]; uncategorized SQLException for SQL [...]; SQL state [HY000]; error code [1366]; Incorrect string value: '\xEF\xBB\xBFNan' for column 'product_description' at row 1; nested exception is java.sql.SQLException: Incorrect string value: '\xEF\xBB\xBFNan' for column 'product_description' at row 1
I am guessing the problem is related to encoding. I wonder if there is something that needs to be done to content imported from file before saving the content to database.
I appreciate any help. Thanks!
Please look below the error screen
UPDATE:
Here is the actual code
def uploadRegistrations() {
def file = request.getFile('file')
if (file.empty) {
flash.message = "File cannot be empty"
return
}
String content = new String(file.getInputStream().getText('UTF-8'))
def id = params['id']
def event = CompositeEvent.get(id.toLong())
def reg = new RaceRegistration(race: event.races[0], compositeEvent: event, raceParticipant: new EmbeddedRaceParticipant(
firstName: content.split(',')[0],
lastName: "none",
gender: Gender.MALE
),
waiver: Waiver.getInstance(event),
status: EntityStatus.ACTIVE
)
reg.save(flush: true, failOnError: true)
The important part is the content is used in firstname of RaceRegistration domain.