使用Java的数据库中的非英文字符 [英] Non english characters in database using Java

查看:85
本文介绍了使用Java的数据库中的非英文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用Java代码在MySql中保存非英语(特殊字符),当我尝试这样做时,数据将另存为??????

I have to save non-english (special character) in MySql using Java code , When i am trying to do so data is getting saved as ??????

String dataStr  = "κωνσταντίνα";
            System.out.println("Before " + dataStr);
             String dataStr1 = new String(dataStr.getBytes("ISO-8859-1"),"UTF-8"); 
             System.out.println("after "+dataStr1);
            String st = URLDecoder.decode("κωνσταντίνα", "UTF-8");
            cd.setTransactionDescription(dataStr1);

推荐答案

您真的应该尝试将所有UTF-8都制作成点对点.

You really should try making everything UTF-8 from point to point.

对数据库和表使用适当的可识别Unicode的排序规则,即使已提供db default,我也会始终按表提供.这个答案有很多mysql + java和servlet问题,但是它们应该回答开发Unicode感知的Java应用程序时我们需要知道的大多数问题.

Use appropriate unicode aware collation for database and table, I always give per table even if db default was already given. This answer has a lot of mysql+java and also servlet issues but they should answer most issues we need to know when developing unicode aware java applications.

CREATE DATABASE mydb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_swedish_ci;

CREATE TABLE tMyTable (
  id int(11) NOT NULL auto_increment,
  code VARCHAR(20) NOT NULL,
  name VARCHAR(20) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_swedish_ci;

使用jdbc连接字符串进行unicode转换.

Use jdbc connection string to have unicode translation.

<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
  maxActive="10" maxIdle="2" maxWait="10000"
  username="myuid" password="mypwd"
  driverClassName="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8"
  validationQuery="SELECT 1"
/>

强制Tomcat对GET和POST参数字符串使用内容类型字符集,因此对http和https连接器(tomcat/conf/server.xml文件)应用useBodyEncodingForURI属性.

Force Tomcat to use content-type charset for both GET and POST parameter strings, so apply useBodyEncodingForURI attribute for http and https connectors (tomcat/conf/server.xml file).

<Connector port="8080"
           maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
           enableLookups="false" redirectPort="8443" acceptCount="100"
           debug="0" connectionTimeout="20000"
           disableUploadTimeout="true"
           useBodyEncodingForURI="true"
/>

在每个servlet页面的开始处,请确保Tomcat解析器将参数要求为utf-8.您需要在读取参数之前调用setCharacterEncoding,否则为时已​​晚.大多数Web浏览器都不发送content-type charset属性,因此servlet引擎可能会猜错.

At the start of each servlet page make sure Tomcat parsers request parameters as utf-8. You need to call setCharacterEncoding before reading parameters or it's too late. Most web browsers don't send content-type charset attribute so servlet engines may guess it wrong.

public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException { doPost(req, res); }

public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException {
   if (req.getCharacterEncoding() == null)
      req.setCharacterEncoding("UTF-8");

   String value = request.getParameter("fieldName");
   ...
}

请谨慎使用.jsp页面,不要插入空的前导白字符,否则调用setCharacterEncoding可能为时已晚,请参阅如何在每行的末尾放置标记标记以避免任何白字符,以及html元素如何从第一行开始线. Jsp标记contentType进入http响应,而pageEncoding表示文件在磁盘中的存储方式.如果仅具有ISO-8859-15文本编辑器,并且没有在jsp页面中对i18n字母进行硬编码,则可以选择适当的iso * pageEncoding.

Be careful with .jsp page do not insert an empty leading whitechars or it may be too late calling setCharacterEncoding, see how I put tag markers at the end of each row to avoid any whitechars, also how html elements start from the first line. Jsp tag contentType goes to http response and pageEncoding means how file is stored in a disk. If you have ISO-8859-15 text editor only and do not hardcode i18n letters in a jsp page you may choose proper iso* pageEncoding.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ 
    taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"  %><%@ 
    page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
    import="java.util.*,
             java.io.*
    "
%><%
   if (req.getCharacterEncoding() == null)
      request.setCharacterEncoding("UTF-8");
   String param1 = request.getParameter("fieldName");
%><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Page Title</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="keywords" content="some,fine,keywords" />
</head>
<body>
your html content goes here.... <%= param1 %>
</body>
</html>

在jsp页面中创建xml文档时,您需要编写xml标头,而不能使用空白字符或换行符.查看scriptlet endtag和xml标头如何在同一行中.这是嵌入式jsp代码必须始终考虑的内容,无辜的领先whitechar可能会破坏格式正确的答复.

Creating xml document in jsp page you need to write xml header without leading whitechars or newlines. See how scriptlet endtag and xml header is in a same line. This is what embedded jsp code must always take into consideration, innocent leading whitechar may ruin well formatted replys.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ 
    page contentType="text/xml; charset=UTF-8" pageEncoding="ISO-8859-1"
    import="java.util.*, 
             java.io.*
    "
%><%
  // MyBean has getId() and getName() getters
  List<MyBean> items = new ArrayList<MyBean>();
  items.add( new MyBean(1, "first") );
  items.add( new MyBean(2, "second") );
  items.add( new MyBean(3, "third") );

  pageContext.setAttribute("items", items);
%><?xml version="1.0" encoding="UTF-8"?>
<mydoc>
<c:forEach var="item" items="${items}">
  <item>
    <id>${item.id}</id>
    <name>${item.name}</name>
  </item>
</c:forEach>
</mydoc>

这篇关于使用Java的数据库中的非英文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆