以下是一个非常简单的JDBC插入文字SQL字符串,导致无效数据。
这是用于插入Oracle DB的表。
create table test_nls (Field1 varchar2(100));
这是Java代码。
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCBatchUpdateExample {
private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
// private static final String DB_CONNECTION = "jdbc:oracle:thin:@192.168.190.1:1521/orcl";
private static final String DB_CONNECTION = "jdbc:oracle:oci:scott/tiger@orcl";
private static Properties props = new Properties();
public static void main(String[] argv) {
try {
batchInsertRecordsIntoTable();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void batchInsertRecordsIntoTable() throws SQLException
{
props.setProperty("oracle.jdbc.convertNcharLiterals", "true");
Connection dbConnection = null;
Statement statement = null;
String insertTableSQL1 = "INSERT INTO TEST_NLS (Field1) values ('Hello World')";
String insertTableSQL2 = "INSERT INTO TEST_NLS (Field1) values ('3£333££')";
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
dbConnection.setAutoCommit(false);
statement.addBatch(insertTableSQL1);
statement.addBatch(insertTableSQL2);
statement.executeBatch();
dbConnection.commit();
System.out.println("Records are inserted");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
statement.close();
dbConnection.close();
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(DB_CONNECTION, props);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}
试图查看以下呼叫是否有帮助,但事实并非如此。
props.setProperty("oracle.jdbc.convertNcharLiterals", "true");
实际文件正在提供给我,因此我无法编辑数据以转换为使用我已阅读过的其他解决方法。
插入的数据结果如下:
Hello World
3Ā£333Ā£Ā£
我是否可以进行一些JDBC调用以使Insert不转换数据?
答案 0 :(得分:0)
似乎每个非ASCII字符都被转换为2个单独的字符,这就是为什么Oracle中数据的最终大小更大。
经过大量研究后,我发现通过使用以下代码作为UTF8读取文件,数据现在正按预期填充。
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(fileDir), "UTF8"));