当前位置: 技术问答>java相关
痛苦啊,按照书上的方法,在将数据存入数据库前,将数据转为“iso-8859-1”字符集的字符,为什么存入后还是乱码
来源: 互联网 发布时间:2017-03-14
本文导语: 下面是源码,请大侠们分析分析。 registerProc.jsp User是一个类,主要是get和set方法,就不贴了。 下面是UserManager的addUser的源码。 public synchronized boolean addUser(User user) { if (!isUserExists(user.g...
下面是源码,请大侠们分析分析。
registerProc.jsp
User是一个类,主要是get和set方法,就不贴了。
下面是UserManager的addUser的源码。
public synchronized boolean addUser(User user)
{
if (!isUserExists(user.getNickname()))
{
Connection conn = null;
PreparedStatement preStatement = null;
try
{
String sSql = "INSERT INTO users "
+ "(nickname, password, question, "
+ "answer, email, description, "
+ "tag, forum_id) "
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
conn = ForumDb.getConn();
preStatement = conn.prepareStatement(sSql);
preStatement.setString(1, user.getNickname());
preStatement.setString(2, user.getPassword());
preStatement.setString(3, user.getQuestion());
preStatement.setString(4, user.getAnswer());
preStatement.setString(5, user.getEmail());
preStatement.setString(6, user.getDesc());
preStatement.setString(7, user.getTag());
preStatement.setInt(8, user.getForumId());
preStatement.execute();
return true;
}
catch (SQLException ex)
{
return false;
}
catch (ClassNotFoundException ex)
{
return false;
}
finally
{
try
{
preStatement.close();
conn.close();
}
catch (SQLException ex)
{
}
}
}
else
{
return false;
}
}
下面是CharSetConv的源码,主要实现字符集的转换,这里只贴出toISO()方法的源码。
/**
* 将源字符串转换为"iso-8859-1"字符集字符串
* @return "iso-8859-1"字符集字符串
*/
public String toISO()
{
try
{
byte[] sourbytes = sSource.getBytes();
return new String(sourbytes, "iso-8859-1");
}
catch (UnsupportedEncodingException ex)
{
return "";
}
}
/**
* 将源字符串转换为"iso-8859-1"字符集字符串
* @param source 要被转换的源字符串
* @return "iso-8859-1"字符集字符串
*/
public String toISO(String source)
{
setSource(source);
return toISO();
}
registerProc.jsp
User是一个类,主要是get和set方法,就不贴了。
下面是UserManager的addUser的源码。
public synchronized boolean addUser(User user)
{
if (!isUserExists(user.getNickname()))
{
Connection conn = null;
PreparedStatement preStatement = null;
try
{
String sSql = "INSERT INTO users "
+ "(nickname, password, question, "
+ "answer, email, description, "
+ "tag, forum_id) "
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
conn = ForumDb.getConn();
preStatement = conn.prepareStatement(sSql);
preStatement.setString(1, user.getNickname());
preStatement.setString(2, user.getPassword());
preStatement.setString(3, user.getQuestion());
preStatement.setString(4, user.getAnswer());
preStatement.setString(5, user.getEmail());
preStatement.setString(6, user.getDesc());
preStatement.setString(7, user.getTag());
preStatement.setInt(8, user.getForumId());
preStatement.execute();
return true;
}
catch (SQLException ex)
{
return false;
}
catch (ClassNotFoundException ex)
{
return false;
}
finally
{
try
{
preStatement.close();
conn.close();
}
catch (SQLException ex)
{
}
}
}
else
{
return false;
}
}
下面是CharSetConv的源码,主要实现字符集的转换,这里只贴出toISO()方法的源码。
/**
* 将源字符串转换为"iso-8859-1"字符集字符串
* @return "iso-8859-1"字符集字符串
*/
public String toISO()
{
try
{
byte[] sourbytes = sSource.getBytes();
return new String(sourbytes, "iso-8859-1");
}
catch (UnsupportedEncodingException ex)
{
return "";
}
}
/**
* 将源字符串转换为"iso-8859-1"字符集字符串
* @param source 要被转换的源字符串
* @return "iso-8859-1"字符集字符串
*/
public String toISO(String source)
{
setSource(source);
return toISO();
}
|
setSource函数怎么没有看到呢?