如何阅读阿拉伯字母在Java Servlet从sql sever 2005? [英] how to read Arabic letter in java Servlet from sql sever 2005?

查看:120
本文介绍了如何阅读阿拉伯字母在Java Servlet从sql sever 2005?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只得到结果为当我在eclipse中使用java Servlet



在eclipse中读取数据库时,结果英文



福特福克斯电击100 MPG当量
福特开始生产Focus Electric,声称它将是EV竞争对手效率最好的产品,而以丰田Prius为目标,其C-Max混合动力和插电式混合动力车将在下半年预计年。



正常工作。



但阿拉伯语不工作
'D39H / J)//这应该是阿拉伯语
'DEEDC)

 包网站; 

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Home扩展HttpServlet {

private static final long serialVersionUID = 8443024680664769771L;

public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException,IOException {
res.setContentType(text / html);
res.setCharacterEncoding(utf-8);
PrintWriter out = res.getWriter();
out.println(< html>);
out.println(< head>);
out.println(< title> info);
out.println(< / title>);
out.println(< / head>);
out.println(< body>);
out.println(< center>);
out.println(< table>);
out.println(< tr>);
out.println(< td>);
try {
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);

连接con = DriverManager.getConnection(jdbc:odbc:website);
语句stat = con.createStatement();
// ResultSet rs = stat.executeQuery(select * from res where word ='+ search +'或web ='+ search +');
ResultSet rs = stat.executeQuery(select * from news);
String hyper = null;
while(rs.next()){
String header = rs.getString(headline);
hyper = rs.getString(link);
String info = rs.getString(info);
out.println(< a href =+ hyper +>+ header +< / a>);
out.println(< p>+ info +< / p>);
}
} catch(SQLException e){
// TODO:handle exception
e.printStackTrace();
} catch(ClassNotFoundException e){
// TODO自动生成的catch块
e.printStackTrace();
}

out.println(< / td>);
out.println(< / tr>);
out.println(< / table>);
out.println(< / body>);
out.println(< / html>);

out.close();
}
}


解决方案


  • 经过大量的搜索,我发现一个非常好的解决方法是将阿拉伯语的列转换为varbinary,然后将其作为字节获取到您的java项目中,然后创建一个新的字符串该字节数组作为构造函数参数,它将使用阿拉伯数字Windows-1256映射阿拉伯字符的正确值。




    • here是代码的示例




SQL select语句:

 从[table_name]中选择cast([column_name] as varbinary(max))其中[条件] 

java代码:

  Statement stat = con。的createStatement(); 
ResultSet rs = stat.executeQuery(select cast([column_name] as varbinary(max))from [table_name] where [condition]);
while(rs.next()){
byte [] tmp = rs.getBytes(column_name);
String cloumn_value = new String(tmp,Windows-1256);
// cloumn_value阿拉伯值
}


i only get result as ????? when i read the database in eclipse using java Servlet

as result in English

Ford Focus Electric to hit over 100 MPG equivalent Ford begins production of the Focus Electric, claiming it will best EV competitors on efficiency, while it takes aim at the Toyota Prius with its C-Max hybrid and plug-in hybrids expected in the second half of next year.

is working fine.

but as Arabic not working 'D39H/J) // this should be Arabic 'DEEDC)

package website;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Home extends HttpServlet {

    private static final long serialVersionUID = 8443024680664769771L;

    public void doGet(HttpServletRequest req,
            HttpServletResponse res)
            throws ServletException, IOException {
        res.setContentType("text/html");
        res.setCharacterEncoding("utf-8");
        PrintWriter out = res.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>info");
        out.println("</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<center>");
        out.println("<table>");
        out.println("<tr>");
        out.println("<td>");
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            Connection con = DriverManager.getConnection("jdbc:odbc:website");
            Statement stat = con.createStatement();
            //ResultSet rs=stat.executeQuery("select * from res where word='"+search+"' or web='"+search+"'");
            ResultSet rs = stat.executeQuery("select * from news");
            String hyper = null;
            while (rs.next()) {
                String header = rs.getString("headline");
                hyper = rs.getString("link");
                String info = rs.getString("info");
                out.println("<a href=" + hyper + ">" + header + "</a>");
                out.println("<p>" + info + "</p>");
            }
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        out.println("</td>");
        out.println("</tr>");
        out.println("</table>");
        out.println("</body>");
        out.println("</html>");

        out.close();
    }
}

解决方案

  • After a lot of searching i found a very good workaround which is to cast the column that is in arabic to varbinary and then to get it in your java project as bytes then creating a new string that takes the byte array as a constructor parameter which will use the arabic encoding "Windows-1256" to map the correct values of the arabic characters

    • here is a sample of the code

SQL select statement :

select cast([column_name] as varbinary(max)) from [table_name] where [condition]

java code :

            Statement stat = con.createStatement();
            ResultSet rs = stat.executeQuery("select cast([column_name] as varbinary(max)) from [table_name] where [condition]");
            while (rs.next()) {
                byte[] tmp = rs.getBytes("column_name");
                String cloumn_value = new String(tmp, "Windows-1256");
                //cloumn_value arabic value
            }

这篇关于如何阅读阿拉伯字母在Java Servlet从sql sever 2005?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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