如何将俄语字符从 Java 传递到 Sql Server 数据库
When filling the database (SQL Server) via Java with Russian words, they are displayed in the database with characters like these: "& # 1085". And the words are truncated when there is no reason for it. That is, the input field is designed for 30 characters, I fill the database through Java, for example, the word "диван" and it truncates it, writes characters in the database or does not enter it at all. As I understand it, the truncation occurs precisely because its encoding is written instead of the word: "& # 1086; & # 1045; & # 1015; & # 1035; ..." and it comes out to more than 30 characters, so it truncates.
Everything is fine with the English language, it displays and works correctly.
I tried adding "useUnicode=true;characterEncoding=utf8" to the database connection string, but it didn't help.
And I'm also using nvarchar (30)
on the jsp page as well:
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
UPDATE:
method that adds to the database:
public static int insert(Product product) {
if (connection !=null){
String sql = "INSERT INTO products (name, price) Values (?, ?)";
try(PreparedStatement preparedStatement = connection.prepareStatement(sql)){
preparedStatement.setString(1, product.getName());
preparedStatement.setInt(2, product.getPrice());
return preparedStatement.executeUpdate();
}
catch(Exception ex){
System.out.println(ex);
}
}
return 0;
}
UPDATE 2:
class Product:
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private int price;
public Product(){ }
public Product(String name, int price){
this.name = name;
this.price = price;
}
public Product(int id, String name, int price){
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
UPDATE 3:
Index.jsp - this is where the data from the database is output:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Products</title>
</head>
<body>
<h2>Products List</h2>
<p><a href='<c:url value="/create" />'>Create new</a></p>
<table>
<tr><th>Name</th><th>Price</th><th></th></tr>
<c:forEach var="product" items="${products}">
<tr><td>${product.name}</td>
<td>${product.price}</td>
<td>
<a href='<c:url value="/edit?id=${product.id}" />'>Edit</a> |
<form method="post" action='<c:url value="/delete" />' style="display:inline;">
<input type="hidden" name="id" value="${product.id}">
<input type="submit" value="Delete">
</form>
</td></tr>
</c:forEach>
</table>
</body>
</html>
Create.jsp - this is where you enter the data to be added to the database:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Create product</title>
</head>
<body>
<h3>New product</h3>
<form method="post">
<label>Name</label><br>
<input name="name"/><br><br>
<label>Price</label><br>
<input name="price" type="number" min="100" /><br><br>
<input type="submit" value="Save" />
</form>
</body>
</html>
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2309
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权