如何配置JDBC以自动修剪来自数据类型定义为char(20)
的列的空格?
当列具有在Informix数据库的表中定义的char(xx)时,如果不使用,则每个保留字符将填充空格。
例如:类型为char(20)
的列名前缀,其值为“test”,最终将读作:
"test " <- 4 characters + 16 white-spaces
是否有配置文件可以处理来自这些类型的数据库字段的空格?
答案 0 :(得分:2)
在连接字符串中使用IFX_TRIMTRAILINGSPACES:
D:\Infx\work\Java>cat select.java
import java.sql.*;
import java.util.*;
public class select {
public static void main( String [] args ) {
Connection conn = null;
ResultSet dbRes = null;
Statement is = null;
try {
Class.forName("com.informix.jdbc.IfxDriver");
conn = DriverManager.getConnection("jdbc:informix-sqli://420ito:9088/stores7:INFORMIXSERVER=ids1210;user=informix;password=dummy;IFX_TRIMTRAILINGSPACES=0");
is = conn.createStatement();
is.executeUpdate("drop table t22;create table t22(c1 char(20))");
is.executeUpdate("insert into t22 values ('abc ')");
PreparedStatement pstmt=conn.prepareStatement("SELECT * from t22");
dbRes= pstmt.executeQuery();
while (dbRes.next()) {
System.out.format("--%s--,",dbRes.getString(1));
try {
} catch ( Exception ex ) {};
}
dbRes.close();
conn.close();
}
catch ( Exception e ) {
System.err.println(e);
e.printStackTrace();
}
}
}
D:\Infx\work\Java>javac select.java
D:\Infx\work\Java>java select
--abc --,
D:\Infx\work\Java>grep TRIM select.java
conn = DriverManager.getConnection("jdbc:informix-sqli://420ito:9088/stores7:INFORMIXSERVER=ids1210;user=informix;password=dummy;IFX_TRIMTRAILINGSPACES=1");
D:\Infx\work\Java>javac select.java
D:\Infx\work\Java>java select
--abc--,
D:\Infx\work\Java>
来自手册:
https://www.ibm.com/support/knowledgecenter/en/SSGU8G_11.70.0/com.ibm.jdbc_pg.doc/ids_jdbc_040.htm