เป็นไปได้ไหมที่จะส่งอักขระภาษาฮินดีไปยัง mysql db โดยไม่ต้องแปลงเป็น Unicode

ตามที่คำถามของฉันบอกว่า เป็นไปได้หรือไม่ที่จะบันทึกอักขระภาษาฮินดีลงในฐานข้อมูลโดยตรงโดยไม่ต้องเข้ารหัส ตัวอย่างเช่น ฉันมีคำนี้ ฉันพิมพ์ลงในฟิลด์ข้อความในหน้า jsp त५ ฉันต้องบันทึกชื่อในฐานข้อมูล mysql

เหตุผลที่ฉันถามสิ่งนี้คือฉันพยายามเข้ารหัสโดยใช้ UTF-8 และแม้แต่ในตารางฉันก็สร้างสิ่งนี้ขึ้นมา

CREATE TABLE `hindi` (
    `data` varchar(200) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

แต่มันทำให้ฉัน त५ ซึ่งเมื่อฉันดึงข้อมูลกลับมาบนพื้นที่ข้อความนั้นเป็นอักขระภาษาฮินดีที่เหมาะสม ไม่เป็นไร แต่ปัญหาเริ่มต้นเมื่อฉันดึงข้อมูลจาก db ไปเป็น pdf ฉันเพิ่งได้รับ त५

โปรดบอกฉันว่าฉันควรทำอย่างไร? ชุดอักขระและการเข้ารหัสเป็นทั้ง utf-8

อัปเดต: รหัสที่สร้างไฟล์ PDF

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@ page trimDirectiveWhitespaces="true" %>
   <%@ page import="javax.servlet.http.*,javax.servlet.*,com.lowagie.text.Document,com.lowagie.text.DocumentException,com.lowagie.text.Paragraph" %>
   <%@page import="java.io.*,java.text.SimpleDateFormat,com.lowagie.text.pdf.BaseFont,com.lowagie.text.pdf.PdfContentByte,com.lowagie.text.pdf.PdfTemplate"%>
   <%@page import="java.sql.*,java.nio.charset.Charset,com.lowagie.text.pdf.PdfWriter,java.awt.Graphics2D"%>
   <%@ page import="java.util.List,java.util.Arrays,java.util.Collections,java.util.*,com.itextpdf.text.pdf.*,com.itextpdf.tool.xml.ElementList,com.itextpdf.text.Rectangle,com.itextpdf.text.Element,com.itextpdf.text.*,com.itextpdf.text.Font,java.awt.Color,com.itextpdf.text.Font.FontFamily,java.util.Date,java.text.*,com.itextpdf.tool.xml.XMLWorkerHelper" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% 

List arrlist = new ArrayList();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/a", "root", "root");
Statement st=con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs;
st.executeQuery("SET NAMES UTF8");
rs=st.executeQuery("SELECT * FROM hindi");

while(rs.next()){
arrlist.add(rs.getString("data"));
}  
System.out.println(arrlist);
// step 1: creation of a document-object
Document document = new Document();
        try {
            // step 2:
            // we create a writer
            PdfWriter writer = PdfWriter.getInstance(
            // that listens to the document
                    document,
                    // and directs a PDF-stream to a file
                    new FileOutputStream("C:/Users/hindi.pdf"));
            // step 3: we open the document
            document.open();
            // step 4:
            String text = "&#2361;&#2379;";
            //String arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
            //String x=new String(,Charset.forName("UTF-8"));
            BaseFont bf = BaseFont.createFont("c:/windows/fonts/arialuni.ttf",
                    BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            for(int i=0;i<2;i++){
                  String str =(String) arrlist.get(i);
            document.add(new Paragraph(str,
                    new com.lowagie.text.Font(bf, 12)));
            }
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(100, 50);
            cb.addTemplate(tp, 36, 750);
        } catch (DocumentException de) {
            System.err.println(de.getMessage());
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }

        // step 5: we close the document
        document.close();


%>
</body>
</html>

person Santino 'Sonny' Corleone    schedule 09.01.2014    source แหล่งที่มา


คำตอบ (3)


ในสตริงการเชื่อมต่อ MySQL คุณต้องเพิ่มการกำหนดค่าพิเศษบางอย่างเช่น

jdbc:mysql://localhost/unicode?useUnicode=true&characterEncoding=UTF-8

ฉันได้สร้างคลาสการเชื่อมต่อที่สามารถเชื่อมต่อกับ MySQL ได้ ดูลิงค์ที่มีคลาส http://uwudalith.wordpress.com/2011/09/02/how-to-insert-unicode-values-to-mysql-using-java/ . มันเป็นโปรเจ็กต์แบบแกว่ง และคุณสามารถขอความช่วยเหลือเล็กๆ น้อยๆ จากที่นั่นได้

ลองเปลี่ยนโครงสร้างตารางตามรูปแบบด้านล่าง

--
-- Database: `unicode`
--
CREATE DATABASE `unicode` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `unicode`;

-- --------------------------------------------------------

-- --------------------------------------------------------

--
-- Table structure for table `unicode`
--

CREATE TABLE IF NOT EXISTS `unicode` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `job` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;
person Damith    schedule 09.01.2014
comment
ใช่. แก้ไขฐานข้อมูลแล้วลอง หากคุณได้รับการแสดงหน้าเว็บที่ถูกต้อง อาจเกิดปัญหาอื่นเมื่อคุณสร้างไฟล์ PDF - person Damith; 09.01.2014
comment
ตรวจสอบให้แน่ใจว่าได้รับการสำรองข้อมูลก่อนทำการเปลี่ยนแปลง - person Damith; 09.01.2014
comment
ฉันสร้าง db ใหม่ แต่ยังคงเก็บ &#2340;&#2381;&#2332; ไว้ในตารางยูนิโค้ด .. ฉันกำลังเข้าสู่ฐานข้อมูลจากหน้า jsp ของฉัน - person Santino 'Sonny' Corleone; 09.01.2014
comment
มีวิธีจัดเก็บการเข้ารหัสฟอนต์ภาษาฮินดีเป็น //u2192 เข้ารหัสแทน ஬ หรือไม่ - person Santino 'Sonny' Corleone; 09.01.2014

ตั้งค่า charecter ตั้งค่าส่วนหัวเป็น utf-8 ขณะสร้าง PDF (charset=utf-8) การดาวน์โหลด JSP - application/octet-stream อาจเป็นได้ว่าลิงก์นี้ใช้งานได้ตามวัตถุประสงค์ของคุณ การเปลี่ยนแปลงที่สอดคล้องกับ ตามความต้องการของคุณ

person Aravind Kishore    schedule 09.01.2014
comment
มันถูกตั้งค่าเป็น charset=utf-8 - person Santino 'Sonny' Corleone; 09.01.2014
comment
พยายามถอดรหัสผู้เช่าเหมาลำ utf-8 และส่งเป็น PDF โดยใช้ฟังก์ชันถอดรหัสบางอย่างใน jsp - person Aravind Kishore; 09.01.2014
comment
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>. pageEncoding คืออะไร ??. ฉันไม่คิดว่ามันจำเป็น - person Aravind Kishore; 09.01.2014
comment
contentType="text/html; charset=UTF-8" เปลี่ยนเป็น contentType="application/pdf; charset=UTF-8" - person Aravind Kishore; 09.01.2014
comment
ที่จะแสดงบนหน้าเว็บ ฉันต้องการดาวน์โหลดไฟล์ - person Santino 'Sonny' Corleone; 09.01.2014
comment
องค์ประกอบ html ทั้งหมด? - person Santino 'Sonny' Corleone; 09.01.2014
comment
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body>และในตอนท้าย </body></html> - person Aravind Kishore; 09.01.2014
comment
คุณได้ตั้งค่า 'Content-disposition: attachment; filename=filename.pdf' ในส่วนหัวแล้ว - person Aravind Kishore; 09.01.2014
comment
มีวิธีจัดเก็บการเข้ารหัสฟอนต์ภาษาฮินดีเป็น //u2192 เข้ารหัสแทน ஬ หรือไม่ - person Santino 'Sonny' Corleone; 09.01.2014

เมื่อคุณเขียน

String text = "&#2361;";

ใน Java สตริงนั้นมีอักขระ 7 ตัว มันก็เป็นเช่นนั้นเสมอ คุณคงตั้งใจจะเขียน

String text = String.valueOf((char)2361) + String.valueOf((char)2379);

or

String text = "\u0939\u094B";

รูปแบบเครื่องหมายและใช้ได้เฉพาะใน HTML และ XML ไม่ใช่ใน Java

person Roland Illig    schedule 09.01.2014