This problem is little bit odd. All UTF-8 requirement of MYSQL and JSP are fully justified in my code. I have two simple files input.jsp (for taking input) and NewFile.jsp (for retrieving input from database). The database QASKU.production is already created and loaded with UTF8 data and is working fine. The problem is with retrieved data through select statement but not always. When I use this statement
ResultSet rs = stmt.executeQuery("select * from QASKU.production");
All the data is retrieved and displayed perfectly.
but when I use these statements:
ResultSet rs = stmt.executeQuery("SELECT * FROM QASKU.production WHERE rhs LIKE '" + sent + "' ORDER BY prob DESC");
or
String query = "select * from QASKU.production WHERE rhs = ?";
PreparedStatement pstmt = con.prepareStatement( query );
pstmt.setString( 1, sent );
ResultSet rs = pstmt.executeQuery( );
The data is retrieved and displayed perfectly but it depends on the input which I gave to this file NewFile.jsp from file input.jsp.
The data in the database is looking like this:
ADJ|اسسٹنٹ|0.001222
ADJ|اسلامی|0.01956
ADJP|ADJ ADJ|0.098214
ADJP |ADJ ADJ.DEG|0.044643
So, when I gave ADJ as input value, the output displayed via NewFile.jsp is perfect.
Now, when I gave, for example, "اسسٹنٹ" as input value, the select statement did not fetch any result set from the database and it will remain empty which is a problem even the record for "اسسٹنٹ" exists in the database.
I don't think this is a problem with MySQL or JSP. I think the problem lies within the select statement, but I'm not sure.
My code file are here as below:
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>
and then the second file NewFile.jsp as below:
<%@ 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>