日期:2014-05-17  浏览次数:20712 次

jsp代码问题 开发小留言板
我想用form往mysql里输入数据 

mysql 数据库名字notepad 
表notepadtable 
字段 ueername email doc 都是text的 

乱码 

这是什么意思? 
我的源代码 

<%@ page import="java.sql.*"%> 
<%@ page contentType="text/html; charset=gbk" %> 
<%@ page language="java" %> 
<%@ page import="com.mysql.jdbc.Driver" %> 
<%@ page import="java.sql.*" %> 

<%! 
public 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){ } 
return “NULL”; 

%> 

<% 
request.setCharacterEncoding("gbk"); 
  Connection con=null; 
   String username=request.getParameter("username"); 
username=getStr(username); 
   String email=request.getParameter("email"); 
email=getStr(email); 
   String doc=request.getParameter("doc"); 
doc=getStr(doc); 
   
   try { 
     Class.forName("com.mysql.jdbc.Driver"); 
  Connection connection=DriverManager.getConnection( 
  "jdbc:mysql://localhost:3306/notepad,"root","1234";); 

     String str="insert into notepadtable(username,email,doc) values(?,?,?);"; 
     PreparedStatement pstmt=con.prepareStatement(str); 
     pstmt.setString(1,username); 
     pstmt.setString(2,email); 
     pstmt.setString(3,doc); 
     pstmt.executeUpdate(); 
     pstmt.close(); 
     connection.close(); 
    } 
   catch(Exception e) { 
     out.println(e.getMessage()); 
    } 
%> 


html 


<html> 
   <body> 
    <form method="post" action="notepadinsert.jsp"> 
     <br>姓名: 
     <input name=username size=55 value=""> 
     <br>邮箱: 
     <input name=email size=55 value=""> 
     <br>留言: 
     <br> 
     <textarea name=doc rows="10" cols="40"> 
     </textarea> 
     <br> 
     <input type=submit value="递交"> 
     <input type=reset value="重填"> 
    </form> 
   </bocy> 
   </html>

------解决方案--------------------
<%@ page contentType="text/html; charset=gbk" %> 
byte[] temp_t=temp_p.getBytes("ISO8859-1"); 

都换成utf-8试试。。