欢迎加入站点基地设计开发交流群:30720409
详细内容
jsp连接mysql传输中文
发布日期:2010-03-25     点击:2604     字体:[ ]

<%

Class.forName("com.mysql.jdbc.Driver").newInstance();    
String   dsn="jdbc:mysql://localhost:3306/test_db?user=root&password=sa";    
   
Connection   conn=DriverManager.getConnection(dsn);    
   
Statement   cmd=conn.createStatement();    
   
//新增数据
String sql = "select max(id) as num from employee";
ResultSet numRs = cmd.executeQuery(sql);
numRs.next();

int id = numRs.getInt(1) + 1;
request.setCharacterEncoding("GB2312");
String last_name = request.getParameter("last_name");
String first_name = request.getParameter("first_name");
out.println(last_name + "--" + first_name);

sql = "insert into employee(id,last_name,first_name,birth,sex,email) values(?,?,?,?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1,id);
stmt.setString(2,last_name);
stmt.setString(3,first_name);
stmt.setString(4,"1982-11-16");
stmt.setString(5,"M");
stmt.setString(6,"liucy@qq.com.cn");
stmt.executeUpdate();
//cmd.executeUpdate(sql);

//查询数据
ResultSet   rs=cmd.executeQuery("select *   from   employee");    
  
while(rs.next())
{
   out.println(rs.getString(1) + "--" + rs.getString(2) + "--" + rs.getString(3)+ "--" + rs.getString(4)+ "--" + rs.getString(5)+ "--" + rs.getString(6));
   out.println("<br/>");
}
    cmd.close();    
rs.close();
conn.close();


%>
连接不加参数&useUnicode=True&characterEncoding=GB2312,数据库写入中文,加入参数写入乱码。

用户评论
昵称 
内容  *
验证码   
   
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号-1 可人软件设计