1

JavaでUnicodeを使用してヒンディー語の文字を表示しようとしているときに問題に直面しています。

例: 文字列 s="\u2345\u2346\u2347"; // 文字を完全に表示する

しかし、これらのコードを db から取得して表示しようとすると、これらは単にコード自体として表示され、ヒンディー語の文字では表示されません。

これが私のコードです:

package nayak;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

import org.omg.CORBA.portable.ValueOutputStream;
/*          DB      : hexadecimalcode
 *          Table   : en_hin
 * +---------------+---------------+--------------+---------------+
 * | char_en       | char_hi       | html_code    | hex_code      |
 * +---------------+---------------+--------------+---------------+
 * | A             | ?             | अ      | &#x0905       |
 * +---------------+---------------+--------------+---------------+
 * | AA            | ?             | आ      | &#x0906       |
 * +---------------+---------------+--------------+---------------+
 */


public class UnicodeToHexcode extends javax.swing.JFrame {
    private JLabel lblUnicode;
    private JTextField jtextf_unicode;
    private JTextField jtextf_hexcode;
    private JButton jcb_getcode;
    private JLabel lblHexcode;

    Connection con = null;
    private JButton jcb_clear;
    private JScrollPane jscrollpane_result;
    private JTextArea jtextarea_result;
    String driverString="net.sourceforge.jtds.jdbc.Driver";
    String sqlString = "jdbc:jtds:sqlserver://mitu:1433/hexadecimalcode";
    String sqlUsername="hms";
    String sqlPassword="hms";
    Statement st=null; 
    ResultSet rs=null;

    {
        //Set Look & Feel
        try {
            javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


    /**
    * Auto-generated main method to display this JFrame
    */
    public static void main(String[] args) {
        UnicodeToHexcode inst = new UnicodeToHexcode();
        inst.setVisible(true);
    }

    public UnicodeToHexcode() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
            this.setTitle("Unicode to Hexadecinal code Converter by Nayak");
            getContentPane().setBackground(new java.awt.Color(255,255,255));
            {
                lblUnicode = new JLabel();
                getContentPane().add(lblUnicode);
                lblUnicode.setText("Unicode");
                lblUnicode.setBounds(10, 23, 63, 21);
                lblUnicode.setFont(new java.awt.Font("Verdana",0,12));
            }
            {
                jtextf_unicode = new JTextField();
                getContentPane().add(jtextf_unicode);
                jtextf_unicode.setBounds(10, 42, 462, 28);
                jtextf_unicode.setFont(new java.awt.Font("Verdana",0,12));
                jtextf_unicode.setBackground(new java.awt.Color(255,231,193));
                jtextf_unicode.addKeyListener(new KeyAdapter() {
                    public void keyPressed(KeyEvent evt) {
                        jtextf_unicodeKeyPressed(evt);
                    }
                });
            }
            {
                lblHexcode = new JLabel();
                getContentPane().add(lblHexcode);
                lblHexcode.setText("Hexcode");
                lblHexcode.setBounds(10, 191, 63, 19);
                lblHexcode.setFont(new java.awt.Font("Verdana",0,12));
            }
            {
                jtextf_hexcode = new JTextField();
                getContentPane().add(jtextf_hexcode);
                jtextf_hexcode.setBounds(10, 210, 462, 28);
                jtextf_hexcode.setFont(new java.awt.Font("Verdana",0,12));
                jtextf_hexcode.setBackground(new java.awt.Color(255,231,193));
            }
            {
                jcb_getcode = new JButton();
                getContentPane().add(jcb_getcode);
                jcb_getcode.setText("Get Code");
                jcb_getcode.setBounds(10, 76, 155, 49);
                jcb_getcode.setFont(new java.awt.Font("Arial Unicode MS",1,16));
                jcb_getcode.setBackground(new java.awt.Color(0,0,255));
                jcb_getcode.setBorder(BorderFactory.createCompoundBorder(
                        new LineBorder(new java.awt.Color(128,128,128), 1, false), 
                        new LineBorder(new java.awt.Color(192,192,192), 1, false)));
                jcb_getcode.setForeground(new java.awt.Color(255,255,255));
                jcb_getcode.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        jcb_getcodeActionPerformed(evt);
                    }
                });
            }
            {
                jscrollpane_result = new JScrollPane();
                getContentPane().add(jscrollpane_result);
                jscrollpane_result.setBounds(179, 81, 293, 104);
                jscrollpane_result.setAutoscrolls(true);
                {
                    jtextarea_result = new JTextArea();
                    jscrollpane_result.setViewportView(jtextarea_result);
                    jtextarea_result.setBounds(10, 202, 72, 23);
                    jtextarea_result.setFont(new java.awt.Font("Dialog",0,20));
                    jtextarea_result.setEditable(false);
                    jtextarea_result.setRows(3);
                    jtextarea_result.setPreferredSize(new java.awt.Dimension(442, 36));
                }
            }
            {
                jcb_clear = new JButton();
                getContentPane().add(jcb_clear);
                jcb_clear.setText("Clear");
                jcb_clear.setBounds(10, 136, 155, 49);
                jcb_clear.setFont(new java.awt.Font("Arial Unicode MS",1,16));
                jcb_clear.setBorder(BorderFactory.createCompoundBorder(
                        new LineBorder(new java.awt.Color(128,128,128), 1, false), 
                        new LineBorder(new java.awt.Color(192,192,192), 1, false)));
                jcb_clear.setBackground(new java.awt.Color(0,0,255));
                jcb_clear.setForeground(new java.awt.Color(255,255,255));
                jcb_clear.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        jcb_clearActionPerformed(evt);
                    }
                });
            }
            pack();
            this.setSize(490, 278);
            postInitGUI();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void postInitGUI(){
        stayCentered();
    }
    public void stayCentered(){
        double s_height,s_width,f_height,f_width,f_x,f_y;
        f_height=this.getHeight();
        f_width=this.getWidth();
        Dimension screenSize = this.getToolkit().getScreenSize();
        s_height=screenSize.getHeight();
        s_width=screenSize.getWidth();

        f_x=(s_width/2)-(f_width/2);
        f_y=(s_height/2)-(f_height/2);

        Double d= new Double(f_x);
        int fr_x=d.intValue();
        d= new Double(f_y);
        int fr_y=d.intValue();
        this.setLocation(fr_x, fr_y);
    }
    public boolean isNumber(String input){
        boolean res=false;
        int input_no=0;
        try {
            input_no=Integer.parseInt(input);
            System.out.println("Input_no: "+input_no);
            if(input_no>0 && input_no<9999)
                res= true;
            else
                res= false;
        } catch (Exception e) {
            res= false;
        }
        return res;
    }
    public boolean checkTextfield_unicode(String input_text){
        boolean res=false;

        if(input_text.equalsIgnoreCase("")){
            System.out.println("jtextf_unicode: "+input_text);
            JOptionPane.showMessageDialog(this, "Enter Unicode !!!");
            clear();
            res=false;
        }else{
            StringTokenizer input_text_splitted = new StringTokenizer(input_text,";");
            while (input_text_splitted.hasMoreTokens()) {
                String txt=input_text_splitted.nextToken();
                 if(!(txt.substring(0,2).equalsIgnoreCase("&#"))){
                    System.out.println("jtextf_unicode: "+txt.substring(0,2));
                    JOptionPane.showMessageDialog(this, "Enter Correct Unicode !!!\nCode format: &#<4-digit no.>;");
                    clear();
                    res=false;
                }else if(txt.substring(2).length()<4){
                    System.out.println("jtextf_unicode: "+txt.substring(2));
                    JOptionPane.showMessageDialog(this, "Enter Correct Unicode !!!\nCode format: &#<4-digit no.>;\n");
                    clear();
                    res=false;
                }else if(!(isNumber(txt.substring(2)))){
                    System.out.println("jtextf_unicode: No Number");
                    JOptionPane.showMessageDialog(this, "Enter Correct Unicode !!!\nCode format: &#<4-digit no.>;\n");
                    clear();
                    res=false;
                }else{
                    res=true;
                }
            }
        }
        return res;
    }
    public void clear(){
        jtextf_unicode.setText("");
        jtextf_hexcode.setText("");
        jtextarea_result.setText("");

        jtextf_unicode.requestFocus();
    }
    public Connection getConnection(){
        // --- connect to DB ---
        try{
            // --- Step 1: Set Driver url ---
            Class.forName(driverString);  
            // --- Step 2: Set Sql Server url ---
            con = DriverManager.getConnection(sqlString,sqlUsername,sqlPassword);  
            System.out.println("...... DB Connection Successful ......");
            // --- Step 3: Fetch Data ---

        }catch (Exception e) {
            System.out.println("...... DB Connection Failed ......");
            e.printStackTrace();
        }
        return con;
    }
    public String getData(String query){
        String output="";
        con=getConnection();
        try {
            st = con.createStatement();
            rs = st.executeQuery(query);  
            while (rs.next()) {                         
                String htm=rs.getString(1);
                String hex=rs.getString(2);
                System.out.println("--> "+htm+" - "+hex);
                String hex_1=hex.substring(3,7);
                System.out.println("--> hex_1: "+hex_1);
                hex_1="\\u"+hex_1;
                System.out.println("--> hex_1: "+hex_1);
                output+=hex_1.trim();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }  
        return output;
    }
    private void jtextf_unicodeKeyPressed(KeyEvent evt) {
        char c=evt.getKeyChar();
        String txt=jtextf_unicode.getText().toString().trim();

        if(c==KeyEvent.VK_ENTER){
            if(checkTextfield_unicode(txt)){
                jcb_getcode.requestFocus();
            }

        }
    }
    private void jcb_getcodeActionPerformed(ActionEvent evt) {
        String unicd=jtextf_unicode.getText().trim();
        System.out.println("@@@unicode: "+unicd);
        String hex_all="";
        if(checkTextfield_unicode(unicd)){
            StringTokenizer cod_all = new StringTokenizer(unicd,";");
            while (cod_all.hasMoreTokens()) {
                String cod_single=cod_all.nextToken();
                System.out.println("***cod_single: "+cod_single+";");
                cod_single+=";";
                String query="select html_code,hex_code " +
                        " from en_hin " +
                        " where html_code='"+cod_single.trim()+"'";
                hex_all+=getData(query);
                System.out.println("@@@Hex code: "+hex_all);
                jtextf_hexcode.setText(hex_all.trim());
                //hex_all="\u0917\u0941\u0923\u0935\u0924"; //<--- if u uncomment it shows Hindi texts
                jtextarea_result.setText(hex_all);          //<--- but here same code from db doesn't show, why?
            }
            jcb_clear.requestFocus();
        }
    }
    private void jcb_clearActionPerformed(ActionEvent evt) {
        clear();
    }

}

誰がどこに障害があるのか​​ わかりますか?

4

1 に答える 1