通过字符串添加,用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('中文');
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('中文');
拜托了。
请问该如何转换sql语句中的中文参数?谢谢!
<%@ 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"));
}
%>
多弄点错误调试信息看看。
下面是我用的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);
}