我正在尝试使用java程序在我的数据库中保存hindi中的一些文本,它是一个简单的文本在印地文中并存储在数据库中,如下所示:
ज़िनà¥à¤¦à¤—ी में अगर कà...
以下是我的PHP代码
//Normal connection stuff
....
$content_hindi = $_POST['content_hindi'];
$QueryString = "INSERT INTO contents(content_hindi) VALUES ('$content_hindi')";
....
//running query stuff
以下是我的java代码
private void sendData() throws IOException{
URL url = new URL(Constants.URL_ADDDATA);
Map<String,Object> params = new LinkedHashMap<>();
params.put(Constants.COL_HINDI, getString(app.text_hindi));
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;){
sb.append((char)c);
}
app.log(sb.toString());
}
我的php或java代码有问题吗? 我试着输入英文字符串,它工作正常。
修改:col设置为utf8_unicode_ci
答案 0 :(得分:0)
我使用:mysqli_set_charset
$con=mysqli_connect($servername, $username, $password, $dbname);
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($con,"utf8");
$content_hindi = $_POST['content_hindi'];