2017-08-30 65 views
1

我正在使用servlet和mysql連接器5.1.44來連接mysql數據庫。如何在使用servlet的mysql中保存表情符號

Connection conn = DriverManager.getConnection(Utils.DBURL+"/"+Utils.DBName+"?useUnicode=true&characterEncoding=utf8mb4&", Utils.DBUserName, Utils.DBPassword); 

這非常適用於多語言輸入,但試圖挽救表情符號時,它最終只節能「??」。 如何使用servlet來保存表情符號?

servlet代碼

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException{ 
response.setContentType("application/json"); 
request.setCharacterEncoding("UTF-8"); 
String content = request.getParameter("content"); 
Class.forName("com.mysql.jdbc.Driver"); 
     Connection conn = 
DriverManager.getConnection(Utils.DBURL+"/"+Utils.DBName+"? 
useUnicode=true&characterEncoding=utf8mb4&", Utils.DBUserName, 
Utils.DBPassword); 
PreparedStatement stmt = conn.prepareStatement("INSERT INTO " 
          + "`feed`(`content`)" 
          + " VALUES (?)"); 
stmt.setString(1, content); 
stmt.executeUpdate(); 
} 
+0

那裏的'characterEncoding'值是什麼值「utf8mb4」? – jingx

+0

在這裏看到https://stackoverflow.com/questions/30074492/what-is-the-difference-between-utf8mb4-and-utf8-charsets-in-mysql – user3136742

+0

'utf8mb4'作爲連接編碼嗎?我認爲這隻與存儲有關。 – tadman

回答

0

是在問題設置爲存儲這些Unicode值的表/數據庫?您在連接字符串中設置的字符集僅控制數據在客戶端和服務器之間傳輸的方式。如果該列無法存儲該數據,則最終會導致數據損壞。

+0

是列已設置。與utf8mb4_general_ci – user3136742

+0

然後一些更多的代碼顯示你如何插入和選擇數據到數據庫將幫助 – Lothar

+0

我可以使用phpmyadmin添加表情符號到數據庫,我也可以成功查詢它。唯一的問題是使用servlet進行插入時,它變成了? ,即時猜測什麼是與servlet – user3136742

相關問題