1

i'm stuck. My problem is: When i send russian text data to the mysql, it save's like ????. I've tried a lot of diffrent solutions, none of them worked out for me. Here is my jsp file.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
    <%@page import="java.sql.*" %>
    <%@page import="com.mysql.jdbc.PreparedStatement" %>
    <%@page import="com.mysql.jdbc.Connection" %>
    <%@ page import="java.util.Enumeration" %>
    <%@ page import="java.io.PrintWriter" %>

<fmt:requestEncoding value="UTF-8" />
<%request.setCharacterEncoding("UTF-8");response.setContentType ("text/html; charset=UTF-8");   %>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi">
<head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP</title>
</head>
<body>
<%@ page contentType="text/html; charset=UTF-8" %>
<%
    try {
        response.setContentType ("text/html; charset=UTF-8");
        request.setCharacterEncoding ("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Connection con, conn;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        conn = (Connection) DriverManager.getConnection("jdbc:mysql:localhost:3306/schedule", "sch", "sch");
        PreparedStatement zxc = (PreparedStatement) conn.prepareStatement("SET NAMES utf8");

        //zx.setString(1, login);
        zxc.execute();
        zxc.close();
        String dat = request.getParameter("btn");
        String day_num = request.getParameter("day_num" + dat.substring(dat.length()-1, dat.length()));
        String first = request.getParameter("first" + dat.substring(dat.length()-1, dat.length()));
        String second = request.getParameter("second" + dat.substring(dat.length()-1, dat.length()));
        String third = request.getParameter("third" + dat.substring(dat.length()-1, dat.length()));
        String fourth = request.getParameter("fourth" + dat.substring(dat.length()-1, dat.length()));
        String fifth = request.getParameter("fifth" + dat.substring(dat.length()-1, dat.length()));
        String sixth = request.getParameter("sixth" + dat.substring(dat.length()-1, dat.length()));
        String seventh = request.getParameter("seventh" + dat.substring(dat.length()-1, dat.length()));
        String group = dat.substring(0, dat.length()-1);
        out.println(group);



        String bad_redirectURL = "login.jsp";
        String good_redirectURL = "tables.jsp?group=" + dat.substring(0, dat.length()-2);


        con = (Connection) DriverManager.getConnection("jdbc:mysql:localhost:3306/schedule", "user_creator", "user_creator");

        Statement rs = con.createStatement();
        rs.executeQuery("SET NAMES 'UTF8'");
        rs.close();


        String query = "UPDATE " + group +" SET first = ?, second = ?, third = ?, fourth = ?, fifth = ?, sixth = ?, seventh = ? WHERE day_num = ?;";




        PreparedStatement ps = (PreparedStatement) con.prepareStatement(query);




        ps.setString(1, first);
        ps.setString(2, second);
        ps.setString(3, third);
        ps.setString(4, fourth);
        ps.setString(5, fifth);
        ps.setString(6, sixth);
        ps.setString(7, seventh);
        ps.setString(8, day_num);
        out.println(ps.toString());



        ps.execute();

        response.sendRedirect(good_redirectURL);



    } catch (NullPointerException | SQLException e) {
        e.printStackTrace();
    }
%>
</body>
</html>

Aslo i've added this line to the server.xml

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>

Also i've added this to the web.xml

<jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
        </jsp-property-group>
    </jsp-config>
Jens
  • 67,715
  • 15
  • 98
  • 113
funafuna
  • 11
  • 2
  • How is your table fields encode defined? – Jorge Campos Jan 17 '17 at 12:54
  • My table is UTF-8 aswell. – funafuna Jan 17 '17 at 19:31
  • So, everything seems fine (I take that you really checked your database table, remember that besides the table being defined as UTF8 it should be `utf8mb4` which is utf8mb4_general_ci also, your varchar columns should be defined as the same). My other suggestion would be to you check the encode of your project files (.java, .jsp, web.xml, etc) if one of then is not UTF8 as well it could be your problem. I suggest you to use notepad++ to check your files. – Jorge Campos Jan 17 '17 at 20:03

1 Answers1

0

I think you are missing useUnicode=true.

Read about "question marks" in Trouble with utf8 characters; what I see is not what I stored It says that these are the likely causes:

  • The bytes to be stored are not encoded as utf8/utf8mb4. Fix this.
  • The column in the database is CHARACTER SET utf8 (or utf8mb4). Fix this.
  • Also, check that the connection during reading is UTF-8.

If you still have troubles, use the trouble shooting tips in that link and see if you get pairs of hex bytes Dxyy. In utf8, the first byte of Cyrillic is D0 through D4. If you don't see that, come back for further discussion.

Community
  • 1
  • 1
Rick James
  • 135,179
  • 13
  • 127
  • 222