Thursday, June 14, 2007

Connecting database in JAVA through PeopleCode API

I wrote one utility method in java, through which we can query from database using peoplecode API. This code will work only if java code is deployed in people soft application.

Usage : query(record-name,where-condition ,bind-parametrs , select-column-list );

It will return the results in as list of Object arrays. Here each object array represents one row.
Objects are in the same order as mentioned in select-column-list.


public static List query(String tableName, String whereCondition, List bindVars, Object[] columns)
{
List results = new ArrayList();
Object row[] = new Object[columns.length];

// Creates the PeopleCode Query, table name also goes as a bind parameter
// So adding the table bind parameter
int nextBindVar = bindVars.size() + 1;
String peopleQuery = "%SelectAll(:" + nextBindVar + ") WHERE " + whereCondition;

Record menuRec = Func.CreateRecord(new Name("RECORD", tableName));
bindVars.add(menuRec);
SQL menuSQL = Func.CreateSQL(peopleQuery, bindVars.toArray());

//Fetch the rows
while (menuSQL.Fetch(new Object[]{menuRec}))
{ //Fetch the columns
for(int i = 0; i < columns.length; ++i)
{
Field field = menuRec.GetField(new Name("FIELD", columns[i].toString()));
row[i] = field.getValue();
}
results.add(row);
}
return results;
}

No comments: