如何在数据库中通过Spring Hibernate在TextBox中插入古吉拉特语? [英] How To insert Gujarati in TextBox via Spring Hibernate in database?
问题描述
我试图使用Java Spring Hibernate Project将gujarati存储在postgreSQL数据库中但是它存储了类似于这样的内容
$ b
મૠàª|à«àª¨àª¾àªàª¯-પરાàªàª¯àªªàª¾àªàª³àªàªμà«àª¯àªà«àª¤àª¿àª¨à«àªàªà«àªà«àª
,而不是
મોદીનાજય-પરાજયપાછળઆવ્યક્તિનુંભેજું
在我的数据库编码为UTF-8的情况下,如果直接在 postgreSQL
中复制粘贴,
以下是我的 hibernate.hbm中的code> html
表单,它不能正确存储。
<?xml version ='1.0'encoding ='utf-8' ?>
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-configuration-3.0 .dtd>
< hibernate-configuration>
< session-factory>
<! - 数据库连接设置 - >
< property name =connection.driver_class>
org.postgresql.Driver
< / property>
< property name =connection.url>
JDBC:在PostgreSQL://192.168.6.51:5432 / JayHind autoReconnect的=真放大器;放大器;了useUnicode =真放大器;放大器; createDatabaseIfNotExist =真放大器;放大器;的characterEncoding = UTF-8
将/性>
< property name =connection.username> postgres< / property>
< property name =connection.password> pshiv< / property>
<! - JDBC连接池(使用内置) - >
< property name =connection.pool_size> 10< / property>
<! - - SQL方言 - >
< property name =dialect>
org.hibernate.dialect.PostgreSQLDialect
< / property>
<! - 启用Hibernate的自动会话上下文管理 - >
< property name =current_session_context_class>线程< / property>
<! - 禁用二级缓存 - >
< property name =cache.provider_class>
org.hibernate.cache.NoCacheProvider
< / property>
<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>
<! - 在启动时删除并重新创建数据库模式 - >
< property name =hbm2ddl.auto>更新< / property>
< property name =hibernate.connection.CharSet> utf8< / property>
< property name =hibernate.connection.characterEncoding> utf8< / property>
< property name =hibernate.connection.useUnicode> true< / property>
< mapping class =com.models.Role/>
< mapping class =com.models.UserAttempts/>
< mapping class =com.models.UserLogin/>
< mapping class =com.models.UserRole/>
< mapping class =com.models.Program/>
< mapping class =com.models.NationalProgram/>
< mapping class =com.models.StateProgram/>
< mapping class =com.models.OtherProgram/>
< mapping class =com.models.Video/>
< mapping class =com.models.Contact/>
< mapping class =com.models.Heading/>
< / session-factory>
< / hibernate-configuration>
我也用过
%@ page language =javacontentType =text / html; charset = UTF-8pageEncoding =UTF-8%>
在jsp页面中,这使得古吉拉特语显示正常,但在表单提交中仍然存在问题。
在模型类中
package com.models;
import static javax.persistence.GenerationType.IDENTITY;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
导入org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;
import com.sun.istack.internal.NotNull;
@Entity
@Table(name = tbl_heading_info,uniqueConstraints = {
@UniqueConstraint(COLUMNNAMES = ID)})
公共类标题{
private int id;
@NotNull
@NotBlank
私人字符串消息;
@Id
@GeneratedValue(策略= IDENTITY)
@Column(name = ID,独特= TRUE,可为空= FALSE)
公众诠释的getId( ){
return id;
}
public void setId(int id){
this.id = id;
@Column(name =message,nullable = false)
public String getMessage(){
return message;
}
public void setMessage(String message){
this.message = message;
/ * byte ptext [] = null;
尝试{
ptext = message.getBytes(ISO_8859_1);
} catch(UnsupportedEncodingException e1){
// TODO自动生成的catch块
e1.printStackTrace();
}
尝试{
字符串值=新字符串(ptext,UTF-8);
this.message = value;
} catch(UnsupportedEncodingException e){
// TODO自动生成的catch块
e.printStackTrace();
} * /
}
}
我也在web.xml中添加了过滤器
< filter>
< filter-name> SetCharacterEncodingFilter< / filter-name>
< filter-class> org.springframework.web.filter.CharacterEncodingFilter< / filter-class>
< init-param>
< / param-name>编码< / param-name>
< param-value> UTF8< /参数值>
< / init-param>
< init-param>
< param-name> forceEncoding< / param-name>
< param-value> true< /参数值>
< / init-param>
< / filter>
< filter-mapping>
< filter-name> SetCharacterEncodingFilter< / filter-name>
< url-pattern> *< / url-pattern>
< / filter-mapping>
预期输出
当前输出
上周我不得不面对这个问题,试试这个。
使用'definitivo'有textBox.getText()并转换为UTF-8
lockquote
byte ptext [] = definitivo。的getBytes( ISO_8859_1);
字符串值=新字符串(ptext,UTF-8);
I am trying to store gujarati in postgreSQL database using Java Spring Hibernate Project But it is storing something like this
મà«àª¦à«àª¨àª¾ àªàª¯-પરાàªàª¯ પાàªàª³ ઠવà«àª¯àªà«àª¤àª¿àª¨à«àª àªà«àªà«àª
instead of
મોદીના જય-પરાજય પાછળ આ વ્યક્તિનું ભેજું
In my database encoding is UTF-8, if I copy paste directly in postgreSQL
it is storing properly but from html
form in web application it is not storing properly.
Following is my hibernate.hbm.cfg
file
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
org.postgresql.Driver
</property>
<property name="connection.url">
jdbc:postgresql://192.168.6.51:5432/JayHind?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf-8
</property>
<property name="connection.username">postgres</property>
<property name="connection.password">pshiv</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<mapping class="com.models.Role" />
<mapping class="com.models.UserAttempts" />
<mapping class="com.models.UserLogin" />
<mapping class="com.models.UserRole" />
<mapping class="com.models.Program" />
<mapping class="com.models.NationalProgram" />
<mapping class="com.models.StateProgram" />
<mapping class="com.models.OtherProgram" />
<mapping class="com.models.Video" />
<mapping class="com.models.Contact" />
<mapping class="com.models.Heading" />
</session-factory>
</hibernate-configuration>
I have also used
% @ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" % >
in jsp pages this make gujarati display properly but in form submission there is still problem.
In model class
package com.models;
import static javax.persistence.GenerationType.IDENTITY;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;
import com.sun.istack.internal.NotNull;
@Entity
@Table(name = "tbl_heading_info", uniqueConstraints = {
@UniqueConstraint(columnNames = "id")})
public class Heading {
private int id;
@NotNull
@NotBlank
private String message;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "message", nullable = false)
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
/*byte ptext[] = null;
try {
ptext = message.getBytes("ISO_8859_1");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String value = new String(ptext, "UTF-8");
this.message=value;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
}
}
I have also put filter in web.xml
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
Expected Output
Current Output
I had to face this the last week, try this. Use 'definitivo' has the textBox.getText() and convert to UTF-8
byte ptext[] = definitivo.getBytes("ISO_8859_1");
String value = new String(ptext, "UTF-8");
这篇关于如何在数据库中通过Spring Hibernate在TextBox中插入古吉拉特语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!