Exposed Kotlin. Problem with cyrillic encoding
Asked Answered
R

1

5

I tryed to fix a problem with encodings. So, I sent from 'Postman', from web browser request to server, where I search data in database by keys in request. Request can be like this:

http://localhost:8080/books.getBooksByGenre/Документальное/0/10

(in browser). Server receive string, like

http://localhost:8080/books.getBooksByGenre/%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D0%BB%D1%8C%D0%BD%D0%BE%D0%B5/0/10

then, takes params from url: genreName: 'Документальное' start: 0 count: 10. Then, this data sends to dao:

override fun findGenreByName(genreName: String): DatabaseGenre {
    return transaction(db) { getGenreByName(genreName) }
}

private fun getGenreByName(genreName: String): DatabaseGenre {
    return try {
        val foundGenre = GenreEntity.find { Genres.genre eq genreName }.single()
        DatabaseGenre(foundGenre.id.value, foundGenre.genre, foundGenre.link)
    } catch (e: Exception) {
        throw NothingFoundInDatabaseException("no one genre found by '$genreName'")
    } catch (e: NoSuchElementException) {
        val m = "Duplicates of genre with name '$genreName'"
        throw DuplicatedDataInDatabaseException(m)
    }
}

In log I see, that sql-query for finding genres is correct, but I receive an exception:

java.util.NoSuchElementException: Collection is empty.

The sql-query, as I said, is correct:

SELECT genres.id, genres.genre, genres.link FROM genres WHERE genres.genre = 'Документальное'

Structure of genres table:

genres id: int(10) genre: varchar(100) link: varchar(100

I tryied, to select all genres, and this query executed almost correctly. So, I decided, to check this query with english word, and this query correctly executed:

SELECT genres.id, genres.genre, genres.link FROM genres WHERE genres.genre = 'simpleGenre'

I have not exceptions with this query.

So, what I've done wrong and how to fix problem with collations?

UPD: As I said at github (issue), I've tryied this query it mysql cli and I receive correct answer.

Also, I've tryed to decode url params (with java URLDecoder class). It doesn't helps too.

Radman answered 18/11, 2018 at 11:0 Comment(5)
Well, yeah. That's called URL encoding. If it doesn't automatically decode it, you'll need to do so manually.Vascular
@Zoe, thanks for your answer. But, why I saw in logs sql-query with cyrillic letters (in "normal" form)? It was without encoded symbols, just normal cyrillic letters. p.s. I'll try your advice and write here resultsRadman
@Zoe, I've tryied your advice. It doesn't hepls. I updated my question description with my attempts to fix this problem and linkto github issue, where I wrote that from mysql cli all works fineRadman
Do you have characterEncoding=utf8 parameter in your MySQL connection URL? Like jdbc:mysql://localhost:3306/db?characterEncoding=utf8. Another helpful parameter is useUnicode=true.Toxophilite
@madhead, fuu** ghm, it's really works. Thank you very mutch. Spent to this a week. Don't know, how to mark this as correct answerRadman
R
8

Thanks, @madhead. I tryied an advance of @madhead, and it works. So, from this time my DB connection URL looks like this:

val connect = Database.connect(
        url = "jdbc:mysql://localhost:3306/my_database_name?characterEncoding=utf8&useUnicode=true",
        driver = "com.mysql.jdbc.Driver",
        user = user_name,
        password = password
)
Radman answered 22/11, 2018 at 21:51 Comment(1)
For me only works with characterEncoding=UTF-8Pleader

© 2022 - 2024 — McMap. All rights reserved.