1

As my question says,is it possible to save a hindi character directly to the database without encoding it.For example I have this word I type in my textfield in jsp page त५.I need to save the name in the mysql database.

The reason I ask this for is I tried encoding this using UTF-8 and even in the table,I created this

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

But it gives me त५ which when I retrieve back on a textarea is proper hindi character.This is ok. But the problem starts when I retrieve it from the db to a pdf I just get त५

Please could tell me what should I do? charset and encoding are bothh utf-8

UPDATE: COde that generated the 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>
Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52

3 Answers3

0

set charecter set headers to utf-8 while generating PDF (charset=utf-8). JSP download - application/octet-stream may be this link works your purpose.change accroding to your requirements.

Community
  • 1
  • 1
0

In your MySQL connection string you have to add some extra configurations like

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

I have created a connection class which can make a connection to MySQL. See the link which contain class http://uwudamith.wordpress.com/2011/09/02/how-to-insert-unicode-values-to-mysql-using-java/ . It is a swing project and you can get a small help from there

Try altering the table structure like below format

--
-- 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 ;
Damith
  • 1,982
  • 3
  • 28
  • 42
0

When you write

String text = "&#2361;";

In Java, that string has 7 characters. It's always like that. You probably meant to write

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

or

String text = "\u0939\u094B";

The ampersand form only works in HTML and XML, not in Java.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121