JavaME和MySQL中文问题

来源:互联网 发布:点云数据处理软件 v8 编辑:程序博客网 时间:2024/05/22 09:01

      java 内部使用 unicode ,而 mysql 缺省使用 ISO-8859-1,所以 jdbc driver 把查询字符串传给 mysql server 时,会做 unicode->ISO-8859-1的转换,从 mysql server 接受结果时,会做 ISO-8859-1->unicode 的转换。(在屏幕上显示结果时会 unicode->GBK,不过不关这里的事。)
这就有问题了,我在命令行下插入数据库的中文字符串是 GBK (这是简体中文 windows 的默认),所以 jdbc driver 接受查询结果时,应该做 GBK->unicode 的转换才对。 类似,写入数据库的时候,我们期待 jdbc driver 会把 unicode->gbk ,结果却是 unicode->ISO-8859-1,当然是乱码了。

 

   因此我在服务器的客户端的代码如下:

  import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import db.* ;

public class MIDletUserDataUpdateServlet extends HttpServlet
{
 
 protected void doGet(HttpServletRequest request,HttpServletResponse response)
        throws IOException, ServletException
 {
               
  request.setCharacterEncoding("MS950") ;
  String id = request.getParameter("id");
  response.setContentType("text/plain;charset=UTF-8");
  DataOutputStream dos = new DataOutputStream(response.getOutputStream());
  
  Connection conn = null ;
  ResultSet rs = null ;
  Statement stmt = null ;
  try
  {
   DatabaseBean da = new DatabaseBean("piginfo") ;
   conn = da.getConnection() ;

   stmt = conn.createStatement() ;
  
   String command = "select * from piginfo where id ="+id;
   rs = stmt.executeQuery(command) ;
 //  while(rs.next())
         if(rs.next())
   {
    StringBuffer s = new StringBuffer("") ;
    s.append(rs.getString("id")) ;
    s.append(",") ;
    s.append(rs.getString("type")) ;
    s.append(",") ;
    s.append(rs.getString("sex")) ;
                                s.append(",") ;
    s.append(rs.getString("birthday")) ;
    s.append(";") ;
    dos.writeUTF(toGB(s.toString()));
    //dos.writeUTF(s.toString()) ;
    dos.flush() ;
    
   }
   dos.close() ;
   rs.close() ;
   stmt.close() ;
   conn.close() ; 
  }catch(Exception e)
  {
  }
  
   }
        public  String toGB(String iso){
       String gb;
       try{
       if(iso.equals("")|| iso == null){
       return "";
       }
       else{
       iso = iso.trim();
       gb = new String(iso.getBytes("ISO-8859-1"),"GB2312");
       return gb;
       }
       }
       catch(Exception e){
       System.err.print("编码转换错误:"+e.getMessage());
       return "";
       }
       }
     

 

 

这样对从MySQL读取的中文进行编码转换。     gb = new String(iso.getBytes("ISO-8859-1"),"GB2312");

 

 

在客户端的部分代码如下:

boolean error = false ;
    HttpConnection c = null;
          InputStream is = null;
    OutputStream os = null;
    DataInputStream dos = null ;
    line ="";
    try
    {
           c = (HttpConnection)Connector.open(url);
          System.out.println("回传码:" + c.getResponseCode()) ;
          is = c.openInputStream();
         dos = new DataInputStream(is) ;
          line = dos.readUTF() ;   
      
          }catch (EOFException e)
     {
      System.out.println("接收到的响应:" + line) ;
     }catch (Exception e)
           {
           }finally
           {
            try
      {
       if (is != null)
                     is.close();
                 if (os != null)
                     os.close();
                 if (c != null)
                     c.close();
      }catch(Exception e){}
                
           }
           if(!error)
     {
            System.out.println(line);
               int i = line.indexOf(",") ;
            String id = line.substring(0,i) ;
            line = line.substring(i+1) ;
            i = line.indexOf(",") ;
            String type = line.substring(0,i) ;
            line = line.substring(i+1) ;
            i = line.indexOf(",") ;
            String sex = line.substring(0,i) ;
            line = line.substring(i+1) ;
            i = line.indexOf(";") ;
            String birthday = line.substring(0,i) ;
             String result ="猪耳号:   "+id+"/r/n种类:   "+type+"/r/n性别:    "+sex+"/r/n出生日期:   "+birthday;
            Alert al = new Alert("猪的基本信息",result,null,AlertType.CONFIRMATION) ;
            al.setTimeout(Alert.FOREVER) ;
            display.setCurrent(al) ;