这个问题有点奇怪。在我的代码中,对MYSQL和JSP的所有UTF-8要求都是完全正确的。我有两个简单的文件input.jsp (用于获取输入)和NewFile.jsp (用于从数据库检索输入)。数据库QASKU.production已经创建并加载了UTF8数据,并且运行良好。问题在于通过select语句检索到的数据,但并不总是这样。当我用这句话
ResultSet rs = stmt.executeQuery("select * from QASKU.production");所有数据都被检索并完美地显示出来。
但当我使用这些语句时:
ResultSet rs = stmt.executeQuery("SELECT * FROM QASKU.production WHERE rhs LIKE '" + sent + "' ORDER BY prob DESC");或
String query = "select * from QASKU.production WHERE rhs = ?";
PreparedStatement pstmt = con.prepareStatement( query );
pstmt.setString( 1, sent );
ResultSet rs = pstmt.executeQuery( );数据被很好地检索和显示,但是它取决于我从文件NewFile.jsp从input.jsp提供给这个文件的输入。
数据库中的数据如下所示:
ADJ\اسسٹنٹ\x{e76f}0.001222 ADJ\اسلامی\x{e76f}0.01956 ADJ=0.098214 ADJP /ADJ/ADJ ADJ 0.044643
因此,当我将ADJ作为输入值时,通过NewFile.jsp显示的输出是完美的。
现在,当我将"اسسٹنٹ“作为输入值时,select语句没有从数据库中获取任何结果集,它将保持为空--即使数据库中存在"اسسٹنٹ”记录也是一个问题。
我不认为这是MySQL或JSP的问题。我认为问题在select语句中,但我不确定。
我的代码文件如下所示:
input.JSP
<!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>QASKU URDU PARSER</title>
<script type="text/javascript" >
var ids = [];
var blurfocus = function(id){
document.getElementById(id).onfocus = function(){
if(!ids[id]){ ids[id] = { id : id, val : this.value, active : false }; }
if(this.value == ids[id].val){
this.value = "";
}
};
document.getElementById(id).onblur = function(){
if(this.value == ""){
this.value = ids[id].val;
}
}
}
function checkSubmit(e)
{
if(e && e.keyCode == 13)
{
document.forms[0].submit();
}
}
</script>
</head>
<body>
<form name="myform" action="NewFile.jsp" method="post" enctype="application/x-www-form- urlencoded" >
<div align="center" onKeyPress="return checkSubmit(event)">
<h4>QASKU URDU PARSER</h4><br>
<h5>Type sentence using Urdu/Arabic script only and then press the 'Parse' button below</h5><br>
<textarea cols="100" rows="5" style="text-align: right" name="mytextarea" id="message" >Type here</textarea>
<script type="text/javascript" >
blurfocus("message");
</script>
<br><br>
<input type="submit" value="Parse" >
</div>
</form>
</body>
</html>然后,第二个文件NewFile.jsp如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!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>
<%
try
{
String sent=request.getParameter("mytextarea");
out.println(sent);
Statement stmt;
Connection con;
String url = "jdbc:mysql://localhost:3306/";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, "root", "");
//stmt = con.createStatement();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
//out.println(con.getMetaData().getDatabaseProductVersion());
//stmt.executeUpdate("DROP DATABASE QASKU");
//out.println("Deleted");
//stmt.executeUpdate("CREATE DATABASE QASKU CHARACTER SET utf8 COLLATE utf8_general_ci");
//stmt.executeUpdate("CREATE TABLE QASKU.production(lhs varchar(50) NOT NULL, rhs varchar(200) NOT NULL, prob double NOT NULL) CHARACTER SET utf8 COLLATE utf8_general_ci");
//stmt.executeUpdate("LOAD DATA LOCAL INFILE '/QAS/JSP/myfirst/WebContent/PCFG.utf' INTO TABLE QASKU.production CHARACTER SET utf8 LINES TERMINATED BY '\r' ");
//ResultSet rs = stmt.executeQuery("SELECT USER(),CHARSET(USER()),COLLATION(USER())");
//ResultSet rs = stmt.executeQuery("select * from QASKU.production");
ResultSet rs = stmt.executeQuery("SELECT * FROM QASKU.production WHERE rhs LIKE '" + sent + "' ORDER BY prob DESC");
//String query = "select * from QASKU.production WHERE rhs = ?";
//PreparedStatement pstmt = con.prepareStatement( query );
//pstmt.setString( 1, sent );
//ResultSet rs = pstmt.executeQuery( );
if(rs != null)
{
%>
<table align=center border="1" bgcolor="green" width="75%">
<col width="25">
<col width="25">
<col width="25">
<tr>
<th align=left>LHS</th>
<th align=left>RHS</th>
<th align=left>PROBABILITIES</th>
</tr>
<%
while(rs.next())
{
out.println("<tr><td align=left>"+rs.getString(1)+"</td>");
out.println("<td align=left>"+rs.getString(2)+"</td>");
out.println("<td align=left>"+rs.getDouble(3)+"</td></tr>");
}
}
else
{
out.println("Result Set is Emptry");
}
%>
</table>
<%
con.close();
}
catch(Exception e)
{
out.println(e);
}
/*
try
{
BufferedReader reader = new BufferedReader(new FileReader("/QAS/JSP/myfirst/WebContent/PCFG.utf"));
String text = "";
while ((text = reader.readLine()) != null)
{
out.println(text);
}
}
catch(Exception e)
{}
*/
%>
</body>
</html>发布于 2013-03-21 14:02:25
完美的解决方案是在of /conf/server.xml文件中添加下面的字符串URIEncoding="UTF-8“,该文件位于eclipse项目的服务器目录中。然后,所有的编码/解码将自动。短而完美的解决方案
<Connector URIEncoding="UTF-8" ...........>这是最好的解决办法,现在为我祈祷。
发布于 2013-03-16 01:27:31
我不知道乌尔都语,但你应该在你的LIKE中加%。
就像这样:
ResultSet rs = stmt.executeQuery("SELECT * FROM QASKU.production WHERE rhs LIKE '%" + sent + "%' ORDER BY prob DESC");发布于 2013-03-16 22:24:09
最后,24小时后我解决了我的问题。这个问题与其他一些声明有关,具体如下:
String sent=request.getParameter("mytextarea");此语句通过post方法从input.jsp页面检索值。在jsp中,这种语句在默认情况下无疑是可用的,但是它的起源是Java。默认情况下,它从ASCII中的页面中获取值,取决于两个定义的方法'get‘和'post’。因此,在input.jsp中使用了这里的'post‘方法,因此在servlet中检索的值具有不同的格式。您可以在jsp教程中阅读。我解决了这个问题,将两个文件input.jsp和newfile.jsp嵌入到一个文件中,然后从下面的行中删除一些信息:
<form name="myform" action="NewFile.jsp" method="post" enctype="application/x-www-form-urlencoded" >并转化成这种简单的形式:
<form name="myform" method="get" >下面的语句是直接从同一个页面获取值,而不是向servlet发送数据:
String sent=request.getParameter("mytextarea");这不是一个很大的解决方案,但至少现在它对乌尔都语意味着utf8字符非常有效。因此,最后的结论是,从MySQL检索ASCII值和不从MySQL数据库检索utf8值的错误与此语句有关,而与其他语句无关。
https://stackoverflow.com/questions/15444012
复制相似问题