title: Urdu (UTF-8) data using MySQL and JSP
body: <p>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</p>
  <pre><code>ResultSet rs = stmt.executeQuery(&quot;select * from QASKU.production&quot;);
  </code></pre>
  <p>All the data is retrieved and displayed perfectly.</p>
  <p>but when I use these statements:</p>
  <pre><code>ResultSet rs = stmt.executeQuery(&quot;SELECT * FROM QASKU.production WHERE rhs LIKE '&quot; + sent + &quot;' ORDER BY prob DESC&quot;);
  </code></pre>
  <p>or</p>
  <pre><code>String query = &quot;select * from QASKU.production WHERE rhs = ?&quot;;
  PreparedStatement pstmt = con.prepareStatement( query );
  pstmt.setString( 1, sent );
  ResultSet rs = pstmt.executeQuery( );
  </code></pre>
  <p>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.</p>
  <p>The data in the database is looking like this:</p>
  <blockquote>
  <p>ADJ|اسسٹنٹ|0.001222</p>
  <p>ADJ|اسلامی|0.01956</p>
  <p>ADJP|ADJ ADJ|0.098214</p>
  <p>ADJP   |ADJ ADJ.DEG|0.044643</p>
  </blockquote>
  <p>So, when I gave ADJ as input value, the output displayed via NewFile.jsp is perfect.</p>
  <p>Now, when I gave, for example, &quot;اسسٹنٹ&quot; 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 &quot;اسسٹنٹ&quot; exists in the database.</p>
  <p>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.</p>
  <p>My code file are here as below:</p>
  <p>input.JSP</p>
  <pre><code> &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; &gt;
      &lt;title&gt;QASKU URDU PARSER&lt;/title&gt;
  
      &lt;script type=&quot;text/javascript&quot; &gt;
      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 = &quot;&quot;;
          }
            };
            document.getElementById(id).onblur = function(){
          if(this.value == &quot;&quot;){
            this.value = ids[id].val;
          }
        }
      }
  
      function checkSubmit(e)
      {
         if(e &amp;&amp; e.keyCode == 13)
         {
            document.forms[0].submit();
         }
      }
      &lt;/script&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
      &lt;form name=&quot;myform&quot; action=&quot;NewFile.jsp&quot; method=&quot;post&quot; enctype=&quot;application/x-www-form-    urlencoded&quot; &gt;
  
         &lt;div align=&quot;center&quot; onKeyPress=&quot;return checkSubmit(event)&quot;&gt;
  
      &lt;h4&gt;QASKU URDU PARSER&lt;/h4&gt;&lt;br&gt;
      &lt;h5&gt;Type sentence using Urdu/Arabic script only and then press the 'Parse' button below&lt;/h5&gt;&lt;br&gt;
  
      &lt;textarea cols=&quot;100&quot; rows=&quot;5&quot; style=&quot;text-align: right&quot; name=&quot;mytextarea&quot; id=&quot;message&quot; &gt;Type here&lt;/textarea&gt;
      &lt;script type=&quot;text/javascript&quot; &gt;
      blurfocus(&quot;message&quot;);
      &lt;/script&gt;
  
      &lt;br&gt;&lt;br&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Parse&quot; &gt;
  
      &lt;/div&gt;
  
      &lt;/form&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  <p>and then the second file NewFile.jsp as below:</p>
  <pre><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
  &lt;%@ page import=&quot;java.sql.*&quot; %&gt;
  &lt;%@ page import=&quot;java.io.*&quot; %&gt;
  &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;%
  try
  {
  String sent=request.getParameter(&quot;mytextarea&quot;);
   out.println(sent);
  
  Statement stmt;
  Connection con;
  String url = &quot;jdbc:mysql://localhost:3306/&quot;;
  Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
  con = DriverManager.getConnection(url, &quot;root&quot;, &quot;&quot;); 
  //stmt = con.createStatement();
  stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
  //out.println(con.getMetaData().getDatabaseProductVersion());
  //stmt.executeUpdate(&quot;DROP DATABASE QASKU&quot;);
  //out.println(&quot;Deleted&quot;);
  //stmt.executeUpdate(&quot;CREATE DATABASE QASKU CHARACTER SET utf8 COLLATE utf8_general_ci&quot;);
  //stmt.executeUpdate(&quot;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&quot;);
  //stmt.executeUpdate(&quot;LOAD DATA LOCAL INFILE '/QAS/JSP/myfirst/WebContent/PCFG.utf' INTO TABLE QASKU.production CHARACTER SET utf8  LINES TERMINATED BY '\r' &quot;);
  
  //ResultSet rs = stmt.executeQuery(&quot;SELECT USER(),CHARSET(USER()),COLLATION(USER())&quot;);
  //ResultSet rs = stmt.executeQuery(&quot;select * from QASKU.production&quot;);
  ResultSet rs = stmt.executeQuery(&quot;SELECT * FROM QASKU.production WHERE rhs LIKE '&quot; + sent + &quot;' ORDER BY prob DESC&quot;);
  
  //String query = &quot;select * from QASKU.production WHERE rhs = ?&quot;;
  //PreparedStatement pstmt = con.prepareStatement( query );
  //pstmt.setString( 1, sent );
  //ResultSet rs = pstmt.executeQuery( );
  
  if(rs != null)
  {
  %&gt;  
      &lt;table align=center border=&quot;1&quot; bgcolor=&quot;green&quot; width=&quot;75%&quot;&gt;
      &lt;col width=&quot;25&quot;&gt;
      &lt;col width=&quot;25&quot;&gt;
      &lt;col width=&quot;25&quot;&gt;
      &lt;tr&gt;
          &lt;th align=left&gt;LHS&lt;/th&gt;
          &lt;th align=left&gt;RHS&lt;/th&gt;
          &lt;th align=left&gt;PROBABILITIES&lt;/th&gt;
      &lt;/tr&gt;
  &lt;%
  
      while(rs.next())
      {
          out.println(&quot;&lt;tr&gt;&lt;td align=left&gt;&quot;+rs.getString(1)+&quot;&lt;/td&gt;&quot;);
          out.println(&quot;&lt;td align=left&gt;&quot;+rs.getString(2)+&quot;&lt;/td&gt;&quot;);
          out.println(&quot;&lt;td align=left&gt;&quot;+rs.getDouble(3)+&quot;&lt;/td&gt;&lt;/tr&gt;&quot;);    
      }
  }
  else
  {
      out.println(&quot;Result Set is Emptry&quot;);
  }
  
  %&gt;
      &lt;/table&gt;
  &lt;%
  
  con.close();
  }
  catch(Exception e)
  {
  out.println(e);
  }
  /*
  try
      {
          BufferedReader reader = new BufferedReader(new FileReader(&quot;/QAS/JSP/myfirst/WebContent/PCFG.utf&quot;));
          String text = &quot;&quot;;
          while ((text = reader.readLine()) != null) 
              {
                  out.println(text);
              }
      }
      catch(Exception e)
      {}
   */
  %&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>