2013-07-04 95 views
0

我的页面中有一些文本框,用户可以在其中编辑他的信息。这里是该文件的代码:数据库中的字符编码问题 - 土耳其语字符 - Java

<?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> 

我使用phpmyadmin和mysql。这里是我的表:

enter image description here

但是,当我输入一些字符,如O,C,I,G,U,S到文本框,并更新数据库,我得到奇怪的字符,如A A? İ的±A·A·Ã?çÃ?ö。我怎样才能解决这个问题?我尝试将utf8_bin和utf8turkish_ci作为整理,但不起作用。谁能帮忙?

感谢

编辑:这是我的新用户输入编辑我的数据库:

/* 
* 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(); 
    } 

} 
} 
+0

您还必须将接收用户数据的页面的字符编码设置为'UTF-8' – BackSlash

+0

您的意思是java类或托管bean? @BackSlash – yrazlik

+0

我的意思是,当你从服务器读取用户数据时,你必须告诉它使用'UTF-8'来读取。你可以发布更新MySQL表的新代码吗? – BackSlash

回答

-1

我居然同时允许UTF-8字符集用于用户名有类似的问题。

第一步骤中,在表单的代码添加:

accept-charset="utf-8" 

到接受=字符集形式属性

第二步是添加一个PHP头

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

现在,在处理php文件需要重新添加头文件

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

现在选择的数据库信息之前使用

mysql_set_charset('utf8'); 

应该从那里工作。这是UTF8,所以,如果你想将其更改为较低的一组像土耳其的人物,进入土耳其的编码..

编辑:这是在PHP中,HTML。所以希望它可以引导您朝着ASP.NET的正确方向发展。

-1

您应该尝试使用Base64进行字符转换。这真的很有效。 我有一个关于将文字写入文件的问题,一些符号消失或更改。 当我使用Base64转换器时,我解决了这个问题。检查Base64

转换的示例代码;

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;        
    }