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();
}
}
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?
– Jovani
Before entering the field is already with strange characters?
– Túlio Calazans
If yes, you need to set the request encoding: request.setCharacterEncoding("UTF-8")
– Túlio Calazans
Thank you very much! It was the request.setCharacterEncoding("UTF-8") that was missing even.
– Jovani