1

我只得到结果??????当我使用 java Servlet 在 Eclipse 中读取数据库时

结果是英语

福特福克斯电动汽车达到 100 英里/加仑以上 福特开始生产福克斯电动汽车,声称它将在效率方面超越电动汽车竞争对手,同时它的目标是丰田普锐斯,其 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();
    }
}
4

2 回答 2

0

尝试

                String header=rs.getString("headline").getBytes("utf-8");
                hyper=rs.getString("link").getBytes("utf-8");
                String info=rs.getString("info").getBytes("utf-8");
于 2012-04-27T21:05:37.927 回答
0
  • 经过大量搜索后,我发现了一个非常好的解决方法,即将阿拉伯语的列转换为 varbinary,然后将其作为字节放入您的 java 项目中,然后创建一个新字符串,该字符串将字节数组作为构造函数参数,这将使用阿拉伯编码“Windows-1256”来映射阿拉伯字符的正确值

    • 这是代码示例

SQL 选择语句:

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

爪哇代码:

            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
            }
于 2015-02-08T17:15:03.243 回答