2011-09-14 27 views
1

我無法將數據從utf8文件中保存到我的SQL Server 2008表中 - SQL排序規則= SQL_Latin1_General_CP1_CI_AS - (當我執行我的System.out.print插入語句:重音是確定的)。Java:將UTF-8文件中的數據保存到Sql Server 2008中

這裏是我做的步驟:

1)將文件轉換爲字符串:

 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)執行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); 

感謝您的幫助

回答

0

將charset設置爲UTF-8,而不是將charset設置爲ISO-8859-1。 ISO-8859-1編碼不接受所有字符。

謝謝。

1

要將UTF-8字符數據轉碼爲UTF-16字符串,請向String constructor提供正確的編碼。

new String(bytes, Charset.forName("UTF-8")); 
+0

非常感謝您!有效。 – Remy

0

無論哪個歸類使用, 它的unicode字符串數據之前,使用「N」是很重要的。

像:

insert into table(somecolumn) values(N'unicode string') 
相關問題