这个问题有点奇怪。 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( );
数据被检索并完美显示,但这取决于我从文件 input.jsp 给文件 NewFile.jsp 的输入。
数据库中的数据是这样的:
ADJ|اسسٹنٹ|0.001222
ADJ|اسلامی|0.01956
ADJP|ADJ ADJ|0.098214
ADJP |ADJ ADJ.DEG|0.044643
因此,当我将 ADJ 作为输入值时,通过 NewFile.jsp 显示的输出是完美的。
现在,例如,当我给出“اسسٹنٹ”作为输入值时,select 语句没有从数据库中获取任何结果集,它将保持为空,即使数据库。
我认为这不是 MySQL 或 JSP 的问题。我认为问题出在 select 语句中,但我不确定。
我的代码文件如下:
输入.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>
最佳答案
我不懂乌尔都语,但您可能应该在 LIKE
中添加 %%。
像这样:
ResultSet rs = stmt.executeQuery("SELECT * FROM QASKU.production WHERE rhs LIKE '%" + sent + "%' ORDER BY prob DESC");
关于java - 使用 MySQL 和 JSP 的乌尔都语 (UTF-8) 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15444012/