簡體   English   中英

Java:將數據從UTF-8文件保存到Sql Server 2008中

[英]Java : save data from UTF-8 file into Sql Server 2008

我無法將帶有重音符號的數據從utf8文件保存到我的SQL Server 2008表中-SQL排序規則= SQL_Latin1_General_CP1_CI_AS-(當我對插入語句進行System.out.print時:重音即可。)

這是我正在執行的步驟:

1)將文件轉換為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)執行插入:

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

謝謝你的幫助

要將UTF-8字符數據轉碼為UTF-16字符串,請為String構造函數提供正確的編碼。

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

無論使用哪種排序規則,在unicode數據字符串之前使用“ N”都是很重要的。

喜歡:

insert into table(somecolumn) values(N'unicode string')

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

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM