2012-04-27 85 views
1

我只得到結果爲?????當我在英語如何從sql sever 2005中讀取java Servlet中的阿拉伯文字母?

使用的Java servlet

的結果中讀取Eclipse中的數據庫

福特福克斯電動打了100 MPG當量 福特開始生產福克斯電動的,聲稱對效率,將最EV競爭對手,而它瞄準豐田普銳斯的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(); 
    } 
} 

回答

0

嘗試

   String header=rs.getString("headline").getBytes("utf-8"); 
       hyper=rs.getString("link").getBytes("utf-8"); 
       String info=rs.getString("info").getBytes("utf-8"); 
0
  • 大量搜索,我發現後一個非常好的解決方法是將以阿拉伯語寫入varbinary的列,然後將其作爲字節獲取到您的java項目中,然後創建一個將字節數組作爲公司的新字符串它將使用阿拉伯語編碼的「Windows-1256」映射阿拉伯字符

    • 這裏的正確的價值觀nstructor參數是代碼

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 
      }