且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Spring JPA将阿拉伯语文本保存到mysql

更新时间:2022-11-15 07:38:25

在互联网上搜索一下后,我添加了几行到我的application.properties以便使用UTF-8编码,它终于奏效了。这里是文件。不要忘记更改网址和密码等。

 #DataSource设置:在这里设置您自己的$ b $配置b##数据库
#连接。在这个例子中,我们有netgloo_blog作为数据库名称#和
#root作为用户名和密码。
spring.datasource.url = jdbc:mysql:// localhost:3306 / newDB?useUnicode = yes& characterEncoding = UTF-8& characterSetResults = UTF-8
spring.datasource.username = root
spring.datasource.password = somePassword

#保持连接长时间闲置(需要在#production中)
spring.datasource.testWhileIdle = true
spring .datasource.validationQuery = SELECT 1

#显示或不记录每个sql查询
spring.jpa.show-sql = true

#Hibernate ddl auto(创建,删除,更新)
spring.jpa.hibernate.ddl-auto =更新
server.tomcat.uri-encoding = UTF-8

#HTTP编码HttpEncodingProperties)
spring.http.encoding.charset = UTF-8
spring.http.encoding.enabled = true
spring.http.encoding.force = true

#命名策略
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

#使用sp ring.jpa.properties。*用于Hibernate本地属性(#prefix是在将
#添加到实体管理器之前剥离的)

#SQL方言使得Hibernate为选定的对象生成更好的SQL #database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.connection.characterEncoding = utf-8
spring.jpa。 properties.hibernate.connection.CharSet = utf-8
spring.jpa.properties.hibernate.connection.useUnicode = true

server.port = 8080


everyone, my app based on Spring MVC and i was trying to persist Arabic text in MySQL (i use JPA) but it's save ???? instead of Arabic characters. I tried to use this:

spring.datasource.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8

but i got error when i tried to save text in Ar***:

java.sql.SQLException: Incorrect string value: '\xD8\xAA\xD8\xB1\xD8\xAD...' for column 'last_name' at row 1

Thanks in advance.

Hi after a bit of search in the internet I added a couple of lines to my application.properties in order to use UTF-8 encoding and it finally worked. here is the file. don't forget to change the url and password etc.

   # DataSource settings: set here your own configurations for the   
# #database 
    # connection. In this example we have "netgloo_blog" as database name #and 
    # "root" as username and password.
    spring.datasource.url = jdbc:mysql://localhost:3306/newDB?useUnicode=yes&characterEncoding=UTF-8&characterSetResults=UTF-8
    spring.datasource.username = root
    spring.datasource.password = somePassword

    # Keep the connection alive if idle for a long time (needed in #production)
    spring.datasource.testWhileIdle = true
    spring.datasource.validationQuery = SELECT 1

    # Show or not log for each sql query
    spring.jpa.show-sql = true

    # Hibernate ddl auto (create, create-drop, update)
    spring.jpa.hibernate.ddl-auto = update
    server.tomcat.uri-encoding=UTF-8

    # HTTP encoding (HttpEncodingProperties)
    spring.http.encoding.charset=UTF-8
    spring.http.encoding.enabled=true
    spring.http.encoding.force=true

    # Naming strategy
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

    # Use spring.jpa.properties.* for Hibernate native properties (the #prefix is
    # stripped before adding them to the entity manager)

    # The SQL dialect makes Hibernate generate better SQL for the chosen #database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
    spring.jpa.properties.hibernate.connection.CharSet=utf-8
    spring.jpa.properties.hibernate.connection.useUnicode=true

    server.port=8080