2013-07-04 33 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;        
    }