Search code examples
jdbccharwhitespaceinformixsqldatatypes

JDBC - Configure white-spaces from 'char' datatype


How can JDBC be configured to automatically trim white-spaces coming from a column where the data type is defined as a char(20)?

When a column has a char(xx) defined in a table of an Informix database, each of those reserved characters will be filled with white-spaces if not used.

Ex.: Column name prefix of type char(20) with a value of "test" will ultimately be read as:

"test                " <- 4 characters + 16 white-spaces

Is there a configuration file which can handle white-spaces from these types of database fields?


Solution

  • use IFX_TRIMTRAILINGSPACES in your connection string:

    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>
    

    from the manual:

    https://www.ibm.com/support/knowledgecenter/en/SSGU8G_11.70.0/com.ibm.jdbc_pg.doc/ids_jdbc_040.htm