getting invalid hindi string from mysql using jsp

2019-09-21 05:43发布

问题:

here are the settings

this time using odbc connection

![showing u the settings][1]


[1]: http://i.stack.imgur.com/4f2bD.jpg

and

![ i have mentioned utf8 here][1]


[1]: http://i.stack.imgur.com/C9fo5.jpg

here is my code


<%-- 
Document   : index.jsp
Created on : 30 Aug, 2011, 10:57:05 AM
Author     : Sahil
--%>
<%@page contentType="text/html" pageEncoding="UTF-8" import = "java.sql.*;"%>
<%@page import = "java.util.*"%>
<%@ page import = "java.io.*"%> 
<%@page import= "java.lang.RuntimePermission" %>
<!DOCTYPE html>
<html>
<head>
 <title>Punjabi-Hindi Dictionary | Output</title>
 <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
  <form accept-charset="UTF-8">  
    <%

    try
      { 
       if (request.getCharacterEncoding() == null) 
           request.setCharacterEncoding("UTF-8");
       Connection cn;

       Statement st;
       ResultSet rs;
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       cn=DriverManager.getConnection("jdbc:odbc:myodbc","root","");
       st=cn.createStatement();   
       rs=st.executeQuery("select * from stu_info");
       while(rs.next()){
       String Name= rs.getString("Name");

     %> 
     <%=Name%>
     <br>
      <%
                        }
     rs.close();
     st.close();
     cn.close();
     }
     catch(Exception ex)
     {
      out.println(ex.getMessage());
     }
      %>

    </form>    
    </body>
  </html>

  this time it gives me diffrent output

  साहिल
  साहिलसाहिल 

回答1:

Obviously your output is in a single page encoding (e.g. ISO-8859-1) and not in UTF-8, this can have several reasons:

  • Have you checked the data is correctly stored in your database? If not, you should check if the table you use for storing the data is UTF-8
  • Is your database connection properly set up to use UTF-8? I don't know how this is done for odbc, but for the mysql-jdbc Driver you might need to append this to your configuration url:

    characterEncoding=utf-8