I am working on spring batch example that reads from db an then have some treatment in processor and then write to a file.
this is my reader :
public JdbcCursorItemReader<FichierEclate> readerDB(){
JdbcCursorItemReader<FichierEclate> reader = new JdbcCursorItemReader<FichierEclate>();
reader.setDataSource(ds);
reader.setSql(query(ClassApp));
reader.setRowMapper(new FichierEclateRowMapper());
return reader;
}
in " reader.setSql(query(ClassApp))" i called a function "query" of type string that make changes to the sql query that i should put in reader.setSql. Here is the function :
public String query(String ClassApp) {
if (ClassApp != null && !ClassApp.trim().equals(""))
{ String[] token = ClassApp.split(".");
String tableName="TF01_TRANSFERT_"+Country;
String QUERY_FIND_FILES =
"SELECT TF01TFID, TF01APPCLS, TF01APPLI, TF01NCPTE, TF01NLOT, FROM" +tableName+ "WHERE TF01APPCLS='"+ token[0] +"' AND TF01APPLI='"+ token[1] +"' AND TF01STID=3 ";
return QUERY_FIND_FILES; }
else return result;
}
This function takes a string variable as input and makes a split on it to use its parts in the sql query as shown in the code.
MY PROBLEM : when the variables "ClassApp" and "country" are declared statically, mean , when i give them values just for testing the batch job , every thing work fine and i got the result i want. Once i change them to be Environment Variables , i got this error
java.lang.arrayindexoutofboundsexception 0
These variable should be env variables and i made this like shown in the screenshot enter image description here
And then i call them using :
private String ClassApp=System.getenv("ClassApp");
private String Country=System.getenv("Country");
SO CAN ENYONE HELP ME HOW TO SOLVE THIS? EVERYTHING WORK FINE AT FIRST WHEN THESE VARIABLES ARE GETTING VALUES STATICALLY? BUT WHEN CHANGED TO ENV.VAR I GOT THIS ERROR java.lang.arrayindexoutofboundsexception ????
To solve this problem i simply need to create regex which will represents dot. To do so we need to escape that .. There are few ways to do it, but simplest is probably by using \ (which in String needs to be written as "\" because \ is also special there and requires another \ to be escaped).
So solution to my problem was :
String[] token = ClassApp.split("\\.");