通过字符串添加,用getbyte操作。
public static String getStr(String str){
     try{
         String temp_p=str;
         byte[] temp_t=temp_p.getBytes("ISO8859-1");
         String temp=new String(temp_t);
         return temp;
     }
     catch(Exception e){ System.out.println("Error:"+e.getMessage());
          
     }
     return "NULL";
   }
调用的时候
publicTools.getStr(request.getParameter("id"))//接受表单提交的数据
你可以用String s = publicTools.getStr('中文');

解决方案 »

  1.   

    我是先在jsp中声明了方法getstr(),在用String s = publicTools.getStr('中文');好像不行!
      

  2.   

    请你看看你的库中的中文是不是乱码?如果不是乱码则不用转换。只要在jsp中加:<%@ page contentType="text/html;charset=gb2312"%>如果是乱码,则应在insert数据之前就要转换。这个问题说了不知多少次了。请你们在问此问题时在论坛里先搜索一下。
    拜托了。
      

  3.   

    我在库中可以读出中文,但用insert往数据库中写中文时却出现了乱吗,
    请问该如何转换sql语句中的中文参数?谢谢!
      

  4.   

    <%@ page contentType="text/html;charset=gb2312"%>
      

  5.   

    这是我的代码请问那里错了!谢谢!
    <%@ page import="java.sql.*"  contentType="text/html;charset=gb2312" %> 
    <%--mysql中的db_name库--%>
    <jsp:useBean id="dbaccess" class="db.DbAccess" scope="session"/>
    <%--查找search--%>
    <%=request.getParameter("id")%>
    <%
      String temp_0="中文";
      String abc;
      abc=new String(temp_0.getBytes("iso8859-1"),"gb2312");
      String sql_s="select * from list_name where name=\'"+abc+"\'";
      dbaccess.resultset = dbaccess.statement.executeQuery(sql_s);   
      while (dbaccess.resultset.next()){
          out.print(dbaccess.resultset.getString("name"));
      }
    %>
      

  6.   

    是的,中文问题我只用过<%@ page contentType="text/html;charset=gb2312"%>不知道你们怎么遇到那么多问题啊?还是resin好!
      

  7.   

    我用tomcat4+sqlserver,插入中文没有问题。
    多弄点错误调试信息看看。
    下面是我用的insert代码,你改改表名和参数,在试试:public String insertRecord(String title, String author, String content, int fatherId, String link, String email) 
    throws SQLException {
    String sql;
    byte[] temp = {1};
    int authorId = 3; sql = "insert into post (title, author, content, fatherid) values ('";
    sql += title + "', '" + author + "', '" + content + "', '" + fatherId + "')";try {
    temp = sql.getBytes("iso-8859-1");
    }catch (UnsupportedEncodingException e) {
    return ("Chinese handling failed: "+e.toString());
    }Statement s = null;
    String sqlCstr = new String(temp);
    int affectedRow = 0;
    try {
    if (conn == null) {
    DBConnect();
    }
    if (conn == null) {
    return ("could not establish conn");
    }else {
    s = conn.createStatement(scrollType, concurType);
    affectedRow = s.executeUpdate(sqlCstr);
    s.close();
    }
    }catch (SQLException e) {
    return ("access db error:"+e.toString() +", affectedRow = "+affectedRow+", sqlCstr="+sqlCstr+"");
    }
    return ("insert successfully, affectedRow = "+affectedRow+", sqlCstr="+sqlCstr);
    }