I failed to save data with accents from utf8 file into my SQL Server 2008 table - SQL collation = SQL_Latin1_General_CP1_CI_AS - (when I do a System.out.print of my insert statement : the accents are OK).
Here's the steps I'm doing :
1) Convert file to String :
File f = new File(file);
byte[] buffer = new byte[(int) f.length()];
in = new DataInputStream(new FileInputStream(f));
in.readFully(buffer);
result = new String(buffer);
2) Execute insert :
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Properties properties = new Properties();
properties.put("charSet", "ISO-8859-1");
properties.put("user", user);
properties.put("password", password);
connection = DriverManager.getConnection("jdbc:jtds:sqlserver://" + serverName + ":1433;DatabaseName=" + dbName + "", properties);
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
statement.executeUpdate(sqlInsert, Statement.RETURN_GENERATED_KEYS);
Thanks for your help
要将UTF-8字符数据转码为UTF-16字符串,请为String构造函数提供正确的编码。
new String(bytes, Charset.forName("UTF-8"));
No matter which collation you use, it is important to use "N" before unicode data strings.
Like:
insert into table(somecolumn) values(N'unicode string')
Instead of setting charset to ISO-8859-1, set charset to UTF-8. ISO-8859-1 encoding does not accepts all the characters.
Thanks.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.