日期:2014-05-20  浏览次数:20830 次

sql 语句乱码问题
<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8" import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="index2.jsp" method="post">
<select name="keyname">
<option value="city">城市</option>
<option value="">/option>
<option value=""></option>
</select>
<input type="text" name="keyvalue" value="">
<input name="submit" type="submit" value="确定"/>


</form>
<% 
try{

  Class.forName("com.mysql.jdbc.Driver").newInstance(); 
  Connection connOrac = DriverManager.getConnection("jdbc:mysql://localhost/ip?user=root&password=1314521&useUnicode=true&characterEncoding=gbk");
  Statement st=connOrac.createStatement();
  System.out.println("ok1");
   
 String kn = "",kv="",type="all";
String swhere = "";
request.setCharacterEncoding("utf-8");




kn =request.getParameter("keyname");

  System.out.println(kn);
kv = request.getParameter("keyvalue");
System.out.println(kv);

if (kn == null){

kn = "";
}
if (kv==null){
kv = "";
}
if(!kn.equals("")&&!kv.equals("")){

swhere = "where" + kn + "like '" + kv +"%'";

}else{
swhere="";
}

System.out.println(swhere);



  String sql = "select ip3 from bbsgood_ip" + swhere;
   
  ResultSet rs = st.executeQuery(sql);
   
  System.out.println(sql);
  if(rs !=null){
   
  while(rs.next()){
  System.out.println(rs.getString("ip3"));
   
   
  }
   
  rs.close();
  }  
  }  

catch(Exception e){

System.out.print(e.getMessage());

}
%>






</body>
</html>

------解决方案--------------------
Java code
Connection connOrac = DriverManager.getConnection("jdbc:mysql://localhost/ip?user=root&password=1314521&useUnicode=true&characterEncoding=utf8");