为什么用jsp连接mysql的时候,向表中插入中文数据时.表中得到的是乱码.而读取mysql数据库里表的内容使用
<%request.setCharacterEncoding("gb2312");%>读取得到的是乱码.而使用new String(rs.getString(1).getBytes("ISO8859-1"));就可以得到中文..怎么解决呢?我晕啊...乱码好烦人啊...求前辈们帮帮忙啊:
代码如:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%request.setCharacterEncoding("gb2312");%>
<%
String DRIVER="com.mysql.jdbc.Driver";
String DBURL="jdbc:mysql://localhost/bdifn?user=root&password=1234";
Connection conn =null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
out.println("class not found!!");
}
try
{
conn=DriverManager.getConnection(DBURL);
stmt=conn.createStatement();
stmt.executeUpdate("insert into note values(null,'你好啊','好啊','乱七八糟的啊~')");
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
%>
<%request.setCharacterEncoding("gb2312");%>读取得到的是乱码.而使用new String(rs.getString(1).getBytes("ISO8859-1"));就可以得到中文..怎么解决呢?我晕啊...乱码好烦人啊...求前辈们帮帮忙啊:
代码如:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%request.setCharacterEncoding("gb2312");%>
<%
String DRIVER="com.mysql.jdbc.Driver";
String DBURL="jdbc:mysql://localhost/bdifn?user=root&password=1234";
Connection conn =null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
out.println("class not found!!");
}
try
{
conn=DriverManager.getConnection(DBURL);
stmt=conn.createStatement();
stmt.executeUpdate("insert into note values(null,'你好啊','好啊','乱七八糟的啊~')");
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
%>