如何从sql server 2005中读取java Servlet中的阿拉伯字母?

时间:2012-04-27 20:19:30

标签: java eclipse tomcat

我只得到结果?????当我使用java Servlet

在eclipse中读取数据库时

结果为英文

福特福克斯电动相当于达到100 MPG以上 福特开始生产福克斯电动车,声称它将成为效率最佳的竞争对手,同时瞄准丰田普锐斯的C-Max混合动力车和插电式混合动力车将在明年下半年上市。

工作正常。

但由于阿拉伯语不起作用 ' D39H / J)//这应该是阿拉伯语 ' 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();
    }
}

2 个答案:

答案 0 :(得分:0)

尝试

                String header=rs.getString("headline").getBytes("utf-8");
                hyper=rs.getString("link").getBytes("utf-8");
                String info=rs.getString("info").getBytes("utf-8");

答案 1 :(得分:0)

  • 经过大量搜索后,我发现了一个非常好的解决方法,即将阿拉伯语中的列转换为varbinary,然后将其作为字节在java项目中获取,然后创建一个接受字节数组的新字符串作为构造函数参数,将使用阿拉伯语编码&#34; Windows-1256&#34;映射阿拉伯字符的正确值

    • 这是代码示例

SQL select语句:

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

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 arabic value
            }