Insert accented word in MYSQL

Asked

Viewed 456 times

0

How can I correctly insert accented words into Mysql? Example: if I try to insert "John" is recording Joao.

Mysql is like Collation utf8-default collation

I take the input data "name" below:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
         <meta charset="UTF-8" />
          <meta http-equiv="content-type" content="text/html;charset=utf-8" />
            <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
            <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css">
        <title>Cadastro</title>
    </head>
.
.
.
form class="form-horizontal" action='Cadastro' method="POST">
<fieldset>

<!-- Form Name -->
<legend>Cadastro de Operador</legend>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="idNome">Nome</label>  
  <div class="col-md-5">
  <input id="idNome" name="idNome" type="text" placeholder="Nome do operador" class="form-control input-md" required="">

  </div>
</div>
.
.
.

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String nome = request.getParameter("idNome");
        String mail = request.getParameter("idDepto");
        int re = Integer.parseInt(request.getParameter("idUsuario"));
        String senha = request.getParameter("idSenha");
        int nivel = Integer.parseInt(request.getParameter("idAdmin"));//1=adm     2=user

        System.out.println(nome);
        Funcionario f = new Funcionario(re,nome);
        Login l = new Login(f,senha,mail,nivel);

        FuncionarioDAO fd = new FuncionarioDAO();

        LoginDAO ld = new LoginDAO();

        try {
            fd.create(f);
            ld.create(f, senha, mail, nivel);
        } catch (Exception ex) {
            Logger.getLogger(Cadastro.class.getName()).log(Level.SEVERE, null, ex);
        }


        processRequest(request, response);
    }

Staff DAO:

  public void create(Funcionario e)throws Exception{  

      //**********************
      try {
            java.sql.Connection conexao = getConexao();
            PreparedStatement pstm = (PreparedStatement) conexao.prepareStatement("INSERT INTO funcionario(re,nome) VALUES(?,?)");
            pstm.setInt(1,e.getRe());  
                        pstm.setString(2,e.getNome());
                        //pstm.setString(3,e.getEsc().getNome());


            pstm.execute();
            pstm.close();
            conexao.close();
        } catch (SQLException o) {
            o.printStackTrace();
        }


   }

1 answer

1


Try adding this other query before the query:

SET NAMES utf8;

If this works, your bank’s charset has not been set correctly. Check that the charset is actually set to utf8 (only "utf8").

However, if strange characters already appear before insertion, you need to set the request encoding:

request.setCharacterEncoding("UTF-8")
  • But if I print the word in there in Servlet with System.out.println(name); it already prints in the console with strange characters, then the problem would not be before Mysql?

  • Before entering the field is already with strange characters?

  • If yes, you need to set the request encoding: request.setCharacterEncoding("UTF-8")

  • Thank you very much! It was the request.setCharacterEncoding("UTF-8") that was missing even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.