I use spring-data-jpa and mysql database. My tables character set is utf-8. Also I added ?useUnicode=yes&characterEncoding=utf8 to mysql url in application.properties file. Problem when I pass characters like "ąčęėį" to controller to save it in mysql. In mysql I got ??? marks. But when I use mysql console example update projects_data set data="ąęąčę" where id = 1; every works well.
application.properties:
# "root" as username and password.
spring.datasource.url = jdbc:mysql://localhost:3306/gehive?useUnicode=yes&characterEncoding=utf8
spring.datasource.username = gehive
spring.datasource.password = pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 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
# 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
tables:
+---------------+--------------------+
| TABLE_NAME | character_set_name |
+---------------+--------------------+
| customer | utf8 |
| projects | utf8 |
| projects_data | utf8 |
+---------------+--------------------+
Try
spring.datasource.url = jdbc:mysql://localhost:3306/gehive?useUnicode=yes&characterEncoding=UTF-8
It seems issue is due to missing "-".
Reference:-
https://forum.hibernate.org/viewtopic.php?f=1&t=1037497&view=next
I had the same issues, and I solved it by adding this line to my application.properties file:
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
Note: The following didn't work:
spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;
For anyone using the HikariCP connection pool who prefers not to append the parameters directly to the JDBC URL:
The spring.datasource.connectionProperties property was removed some time ago. Since then, you need to use connection pool specific properties as shown in #SebTheGreat's answer:
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
Hikari doesn't have a connection-properties property, but this works:
spring.datasource.hikari.data-source-properties.useUnicode=true
spring.datasource.hikari.data-source-properties.characterEncoding=UTF-8
Remember to escape any special characters, as below:
spring.datasource.url=jdbc\:mysql\://localhost\:3306/${SERVER_DB_NAME}\?useUnicode=true\&characterEncoding=utf\-8\&characterSetResults=utf\-8
In my case that's solve my issue
https://mathiasbynens.be/notes/mysql-utf8mb4
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
It works for me if using mysql java connector version 5.1.44 (before I used connector 8.0.17 it doesn't work)
Another possible solution depending your configuration is using #SqlConfig annotation
#Sql(config = #SqlConfig(encoding = "utf-8"))
Related
Hei guys, im pretty new to SpringBoot and i have 1 problem.
When i start the app the spring read the schema.sql and make the tabels but the data.sql is not read, so i dont have no data on my db.
this is my app.prop
spring.datasource.url = jdbc:mysql://localhost:3306/garagesaledb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultComponentSafeNamingStrategy
spring.jpa.hibernate.naming.physical-strategy= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.sql.init.mode=always
spring.datasource.schema = classpath:db/schema.sql
spring.datasource.data = classpath:db/data.sql
spring.jpa.hibernate.ddl-auto=update
spring.liquibase.drop-first=true
spring.jpa.defer-datasource-initialization=true
schema.sql
create table asset
(
id bigint not null auto_increment,
category varchar(255),
price decimal(10,2),
quantity integer,
purchaseOrder_id bigint,
primary key(id),
);
data.sql
INSERT INTO garagesaledb.asset(category, price, quantity,purchaseOrder_id) VALUES
('MOUSE', 10.0, 1, null);
It's just a simple app and query but it doesnt read it.
Either you put the file somewhere outside the resources (external file) or provided the location wrongly, - please check the logs
To confirm that, change below line
spring.datasource.data = classpath:db/data.sql
to :
spring.datasource.data = file:<full-path>db/data.sql
OR
in some spring-boot versions,
spring.sql.init.data-locations=classpath:db/data.sql
Add this property to app.prop spring.datasource.initialization-mode=always
Refer Link : https://docs.spring.io/spring-boot/docs/2.1.0.M1/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc
Spring Boot automatically creates the schema of an embedded DataSource. This behaviour can be customized by using the spring.datasource.initialization-mode property. For instance, if you want to always initialize the DataSource regardless of its type:
spring.datasource.initialization-mode=always
I hope this helps.
I need to know the simple steps to Access the Repository and Entity Classes from Multiple Databases.
Example: I have User table in DB1 and Email in DB2 have to access from a single Service Request.
DB 1
spring.datasource.url = jdbc:mysql://localhost:3306/dbName1?useSSL=false
spring.datasource.username = user
spring.datasource.password = password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
DB 2
spring.datasource.url = jdbc:mysql://localhost:3306/dbName2?useSSL=false
spring.datasource.username = user
spring.datasource.password = password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
OTHER PROPERTIES
spring.jpa.properties.hibernate.jdbc.batch_size=20
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
Solution 1:
You could create a user that has access to both databases and then use fully qualified table names when querying for the external table.
MySQL supports the dbname.tablename syntax to access tables outside the current database scope.
This requires that the currently connected user has the appropriate rights to read from the requested table in another physical db.
Solution 2:
You can configure two datasources as described here:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-two-datasources
(Not a direct answer.)
If you will be using text other than plain English, consider these settings:
Hibernate XML:
<property name="hibernate.connection.CharSet">utf8mb4</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.useUnicode">true</property>
Connection url:
db.url=jdbc:mysql://localhost:3306/db_name?useUnicode=true&character_set_server=utf8mb4
#Table(name="tablename", catalog="db2")
worked for me
I using hibernate and oracle when is execute this hql
my oracle dialect is
org.hibernate.dialect.Oracle10gDialect
and postgres dialect is
org.hibernate.dialect.PostgreSQLDialect
select u.name,substring(u.name,0,2) changedname
from User u
--result is
name | changedname
------------------------------
azizkhani | azi
But when i migrate dialect from oracle to postgres
my result is diff
name | changedname
-------------------------------
azizkhani | az
After google that i found postgres position is not zero base
i dont know why have this problem .
Is hibernate dialect dose not create for solve this problem ?
For Both DBMS, the index 0 is incorrect/invalid in the substr() function.
For both DBMS, the first character is at index 1 not 0.
The difference is that Oracle silently treats 0 as 1 as documented in the manual
Postgres however seems to treat the 0 differently (unfortunately I can't find anything for that in the manual).
If you use the correct index in your HQL query this should work correctly in both DBMS.
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 Arabian:
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
I want to read data from a csv file and then write to MySql. The data contains foreign Languages.
I got this error when I tried to insert a record, which contains Japanese Characters, into MySql.
"1366Incorrect string value: '\xE6\xB0\xB4\xE7\x9D\x80...' for column 'name' at row 1"
The SQL sentence looks like this:
INSERT INTO `MerchandiseMaster` (id,name) VALUES ('20000101','JANIE AND JACK水着 鶯茶系 大胆花柄')
My csv file uses UTF-8 Encoding and the charset of MySql database schema is utf8_gerneral_ci.
I have put these parameters when I connect to database through JDBC(mysql-connector-java-5.1.34-bin.jar):
connect = DriverManager.getConnection("jdbc:mysql://localhost/mydata?"
+ "useUnicode=yes&characterEncoding=UTF-8&user=user123&password=user123.");
My question is:
Is there anything else that I am missing to deal with foreign characters correctly?
I found this on a website, so caveat emptor, but apparently MySQL's UTF-8 support is incomplete. In 2010 they added new support, utf8mb4 that supports the entire UTF-8 encoding scheme.
Add to your MySQL configuration file:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
Here's a link to the full article. I haven't tried this out, so test everything carefully first, and make a back-up of your database before doing anything.