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) ;
- JavaME和MySQL中文问题
- JavaME中文编码
- JavaME中文编码
- mysql和admin中文问题
- mysql 和jsp 的中文乱码问题
- java和mysql中文乱码问题
- 解决PHP和MYSQL中文乱码问题
- 使用Hibernate和mysql中文乱码问题
- MySQL和JDBC中文乱码问题
- javaME
- JavaMe中的jad文件问题
- Mysql不能插入中文和中文显示乱码问题
- Eclipse 提取MySql中文乱码问题和向mysql数据库插入中文乱码问题
- mysql中文问题
- 使用MySQL中文问题
- mySql的中文问题
- mysql jsp中文问题
- MySQL的中文问题
- 控制面板命令大全
- C++ 多个头文件的编译
- 操作系统经典书籍推荐
- C/C++编码规范
- 嵌入式系统Linux内核开发工程师必须掌握的三十道题
- JavaME和MySQL中文问题
- 最喜欢的音乐——海滨之歌
- 输出菱形 --第七题
- 五险一金参考
- 2009年规划
- FC10 上网
- 测试程序(非自身)是否处于忙碌状态,
- 哈哈~~~~
- 恩信ERP7.10安装、定制和二次开发