Reputation: 5094
I want to insert arabic information to the database but always get caracters like this : ابو نص. I use the UTF-8 encoding in my pages and i set my database to utf8_general_ci.
I read many questions similar to this question but I don't find a solution for my case.
this is a solution but with php and i don't know how to do the same thing in java.
The code of insert (by JdbcTemplate)
final String move_insert = "insert into r_movement (PPR,cd_fonc,nom_etabl,ville,delegation,date_debut,date_fin,nbjour,nbmois,nbannees,cina,cinn) "
+ "values (?,?,?,?,?,?,?,?,?,?,?,?)";
getJdbcTemplate()
.update(move_insert, new Object[] {move.getPpr(),move.getFonction(),move.getNom_etabl(),move.getVille(),move.getDelegation(),move.getDate_debut(),move.getDate_fin(),c.getNbjours(),c.getNbmois(),c.getNbyears(),move.getCina(),move.getCinn()});
This is my table :
CREATE TABLE `r_movement` (
`id_move` int(11) NOT NULL AUTO_INCREMENT,
`PPR` int(11) NOT NULL,
`cd_fonc` varchar(255) CHARACTER SET utf8 NOT NULL,
`nom_etabl` varchar(255) CHARACTER SET utf8 NOT NULL,
`ville` varchar(255) CHARACTER SET utf8 NOT NULL,
`delegation` varchar(255) CHARACTER SET utf8 NOT NULL,
`date_debut` date NOT NULL,
`date_fin` date NOT NULL,
`nbjour` int(255) NOT NULL,
`nbmois` int(255) NOT NULL,
`nbannees` int(255) NOT NULL,
`CINA` varchar(255) CHARACTER SET utf8 NOT NULL,
`CINN` varchar(255) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id_move`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8
Upvotes: 2
Views: 3443
Reputation: 142298
This Answer (belatedly) discusses how to recover the mojibaked text.
ابو نص
represents ابو نص
. Hex: D8A7D8A8D98820D986D8B5
. That's 5 Arabic characters (Dxxx
), plus a space (20
).
What caused the problem:
latin1
, probably by default. (It should have been utf8
.)CHARACTER SET latin1
. (Or possibly it was inherited from the table/database.) (It should have been utf8
.)The fix for the data is a "2-step ALTER".
ALTER TABLE Tbl MODIFY COLUMN col VARBINARY(...) ...;
ALTER TABLE Tbl MODIFY COLUMN col VARCHAR(...) ... CHARACTER SET utf8 ...;
where the lengths are big enough and the other "..." have whatever else (NOT NULL
, etc) was already on the column.
By the way, utf8_general_ci
is a "collation"; only the "character set", utf8
, is relevant to this problem.
Upvotes: 0
Reputation: 5094
I solved Finnaly The problem the configuration in the file web.xml was missed !
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
I can now insert arabic data to database safely! Thanks
Upvotes: 3
Reputation: 18614
Try setting character encoding in connection string as explained in docs. e.g.
jdbc:mysql://localhost/some_db?useUnicode=yes&characterEncoding=UTF-8
You also can set that as a server configuration. Look at the doc.
Upvotes: 2