简体   繁体   中英

Character encoding issue with database - Turkish characters - Java

I have some textboxes in my page where user can edit his information. Here is the code of that file:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h:form>
        <h1>Kişisel Bilgileri Değiştir</h1>
        <h:panelGrid columns="4" >
            <h:outputText value="Kullanıcı Adı: "></h:outputText>
            <h:inputText  style=" background-color: #DCDAD1; " id="tUserName" readonly="true" value="#{user.customer.username}" />
            <h:outputText style="color: red" value="*Kullanıcı adı değiştirilemez" ></h:outputText>
            <h:outputText></h:outputText>
            <h:outputText id="tPassword" value="Şifre: "></h:outputText>
            <h:inputText id="passwordInputText" required="true"
                         requiredMessage="*Şifre bilgisi zorunludur"
                         value="#{user.customer.password}" />
            <h:outputText></h:outputText><h:outputText></h:outputText>         
            <h:outputText id="tName" value="Ad: "></h:outputText>
            <h:inputText id="nameInputText" required="true"
                         requiredMessage="*İsim bilgisi zorunludur"
                         value="#{user.customer.name}" />
            <h:outputText></h:outputText>
            <h:outputText></h:outputText>
            <h:outputText id="tSurName" value="Soyad: "></h:outputText>
            <h:inputText id="surnameInputText" required="true"
                         requiredMessage="*Soyad bilgisi zorunludur"
                         value="#{user.customer.surname}" />
            <h:outputText></h:outputText><h:outputText></h:outputText>
            <h:outputText id="tPhone" value="Telefon: "></h:outputText>
            <h:inputText id="phoneInputText" required="true" 
                         requiredMessage="*Telefon bilgisi zorunludur"
                         value="#{user.customer.phone}" />
                     <!--    validatorMessage="*Örnek:+902123475671">
                         <f:validateRegex pattern=
                             "\+[0-9]{12}" /> 
            </h:inputText>-->
            <h:outputText></h:outputText>
            <h:outputText id="tAddress" value="Adres: "></h:outputText>
            <h:inputTextarea rows="5" id="addressInputText" value="#{user.customer.address}" />
            <h:outputText></h:outputText><h:outputText></h:outputText>
            <h:outputText id="tEmail" value="E-mail: "></h:outputText>
            <h:inputText id="emailInputText" required="true"
                         requiredMessage="*E-mail bilgisi zorunludur"
                         value="#{user.customer.email}"
                         validatorMessage="*E-mail formatı yanlış">
                         <f:validateRegex pattern=
                             "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />
            </h:inputText>
                <h:outputText></h:outputText><h:outputText></h:outputText>


                <h:commandButton value="Onayla" action="#{user.editInfo()}">
                </h:commandButton> 
        </h:panelGrid>
    </h:form>
</h:body>
</html>

I use phpmyadmin and mysql. Here is my table:

在此处输入图片说明

But when i enter some characters like Ö,Ç,İ,Ğ,Ü,Ş to the textboxes and update the database, i get weird characters such as Ã?Ä?İıÅ?Å?Ã?çÃ?ö. How can i fix this? I tried utf8_bin and utf8turkish_ci as collation but it does not work. Can anyone help?

Thanks

Edit: Here is where i edit my database with new user inputs:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author SUUSER
 */


@ManagedBean(name = "user")
@SessionScoped
public class User implements Serializable {

public static Customer customer;
private Connection con=null;
public void setCustomer(Customer customer) {
    User.customer = customer;
}

public Customer getCustomer() {
    return customer;
}

public User() {
}

public void editInfo() {
    String name = customer.getName(), surname = customer.getSurname(), password = customer.getPassword();
    String phone = customer.getPhone(), address = customer.getAddress(), email = customer.getEmail();


    try {
        con=DBConnect.connect();
        int result = -1;
        PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
                "UPDATE users set password=?,name=?,surname=?,phone=?,address=?,"
                + "email=? where username=?");
        checkDB.setString(7, customer.getUsername());
        checkDB.setString(1, password);
        checkDB.setString(2, name);
        checkDB.setString(3, surname);
        checkDB.setString(4, phone);
        checkDB.setString(5, address);
        checkDB.setString(6, email);
        result = checkDB.executeUpdate();
        con.close();
        FacesContext.getCurrentInstance().getExternalContext().redirect("editsuccesfull.xhtml");

    } catch (Exception e) {
        e.printStackTrace();
    }

}
}

I actually had a similar problem while allowing UTF-8 character sets to be used for usernames.

first step, in your form code add:

accept-charset="utf-8"

into the accept=charset form attribute

Second step is to add a php header

header("Content-type: text/html; charset=utf-8");

now, In your processing php file you need to add the header again

header("Content-type: text/html; charset=utf-8");

now before choosing the database information use

mysql_set_charset('utf8');

Should work from there.. This is UTF8, So if you want to change it to a lower set of characters like turkish, enter the turkish encoding..

EDIT: This is in PHP, HTML. So hopefully it guides you in the right direction for ASP.NET.

You should try Base64 for characters conversion. It is really effective. I had a problem about writing characters to file, some symbols disappeared or changed. When i used to Base64 converter,I solved this problem. Check Base64

Example code for convertion;

public String encode64(String resultText) {
        String encodeText= Base64.encodeBytes(resultText.getBytes());       //convert base64
        return encodeText;
    }


public byte[] decode64(String text) {
        byte[] decodeText= Base64.decode(text);          //deconvert base64
        return decodeText;                               
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM