因此,这是在 Oracle 10g/UTF-8 数据库中创建表的代码:
CREATE TABLE TEST_SEMANTIC
(
SEMANTIC_COLBYTE char(2 byte) ,
SEMANTIC_COLCHAR char(2 char)
);
意思是,我对字节和字符这两列使用了两种不同类型的语义。
然后我在数据库中插入这些相应的数据:
insert into test_semantic(SEMANTIC_COLBYTE,SEMANTIC_COLCHAR)
values('é','é');
所以当我在 java 程序中使用 JDBC 驱动程序查询数据库并显示结果时,我希望得到这样的输出:
Byte>é<
Char>é<
而我明白了:
Byte>é<
Char>é <
当我这样查询数据库时:
select dump(semantic_colbyte,16),dump(semantic_colchar,16) from test_semantic;
我明白了:
Typ=96 Len=2: c3,a9 Typ=96 Len=3: c3,a9,20
这是Java代码:
public static void main(String[] args){
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
try {
Properties props = new Properties();
props.put("user", "XXX");
props.put("password", "XXX");
con = DriverManager.getConnection("jdbc:oracle:thin:@xxx:1521:xxx", props);
Statement stmt = (Statement) con.createStatement();
stmt.execute("SELECT SEMANTIC_COLBYTE,SEMANTIC_COLCHAR FROM TEST_SEMANTIC");
ResultSet result = stmt.getResultSet();
result.next();
String output_byte = result.getString(1);
String output_char = result.getString(2);
System.out.println("Byte>"+output_byte+"<");
System.out.println("Char>"+output_char+"<");
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
最佳答案
The CHAR datatype specifies a fixed-length character string. Oracle ensures that all values stored in a CHAR column have the length specified by size. If you insert a value that is shorter than the column length, then Oracle blank-pads the value to column length.
关于java - 为什么 JDBC 驱动程序会在 Oracle 数据库中填充一些空白字符而不是查询字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1913498/