Jump to content
Changes to the Jaspersoft community edition download ×

Use column number instead of column name


ktrinad

Recommended Posts

By: Carles Bellonch - cbellonch

Use column number instead of column name

2004-12-14 06:37

First of all, thanks to the JasperReports team for this nice software!

 

I'm using JasperReports 0.6.1 and I would like to improve JRResultSetDataSource.getFieldValue(JRField field), so it could be possible to refer columns by number "COLUMN_X" (where X = {1..N)). I known that it's possible to use JRTableModelDataSource, but it slows down the performance and needs more code...

 

I've seen the getFieldValue method in JRResultSetDataSource class, and here is a proposal of a modified code using a column number (new variable "column"):

 

/**

*

*/

public Object getFieldValue(JRField field) throws JRException

{

Object objValue = null;

 

if (field != null && resultSet != null)

{

Class clazz = field.getValueClass();

 

int column = 0;

if (field.getName().startsWith("COLUMN_") && field.getName().length() > 7)

column = new Integer(field.getName().substring(7)).intValue();

 

try

{

if (clazz.equals(java.lang.Object.class))

{

if (column == 0) objValue = resultSet.getObject(field.getName());

else objValue = resultSet.getObject(column);

}

else if (clazz.equals(java.lang.Boolean.class))

{

if (column == 0) objValue = resultSet.getBoolean(field.getName()) ? Boolean.TRUE : Boolean.FALSE;

else objValue = resultSet.getBoolean(column) ? Boolean.TRUE : Boolean.FALSE;

}

else if (clazz.equals(java.lang.Byte.class))

{

if (column == 0) objValue = new Byte(resultSet.getByte(field.getName()));

else objValue = new Byte(resultSet.getByte(column));

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.util.Date.class))

{

if (column == 0) objValue = resultSet.getDate(field.getName());

else objValue = resultSet.getDate(column);

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.sql.Timestamp.class))

{

if (column == 0) objValue = resultSet.getTimestamp(field.getName());

else objValue = resultSet.getTimestamp(column);

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.sql.Time.class))

{

if (column == 0) objValue = resultSet.getTime(field.getName());

else objValue = resultSet.getTime(column);

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.lang.Double.class))

{

if (column == 0) objValue = new Double(resultSet.getDouble(field.getName()));

else objValue = new Double(resultSet.getDouble(column));

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.lang.Float.class))

{

if (column == 0) objValue = new Float(resultSet.getFloat(field.getName()));

else objValue = new Float(resultSet.getFloat(column));

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.lang.Integer.class))

{

if (column == 0) objValue = new Integer(resultSet.getInt(field.getName()));

else objValue = new Integer(resultSet.getInt(column));

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.io.InputStream.class))

{

if (column == 0) objValue = resultSet.getBinaryStream(field.getName());

else objValue = resultSet.getBinaryStream(column);

if(resultSet.wasNull())

{

objValue = null;

}

else

{

InputStream is = (InputStream)objValue;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] bytes = new byte[1000];

int ln = 0;

try

{

while ((ln = is.read(bytes)) > 0)

{

baos.write(bytes, 0, ln);

}

baos.flush();

}

finally

{

try

{

baos.close();

}

catch(IOException e)

{

}

}

objValue = new ByteArrayInputStream(baos.toByteArray());

}

}

else if (clazz.equals(java.lang.Long.class))

{

if (column == 0) objValue = new Long(resultSet.getLong(field.getName()));

else objValue = new Long(resultSet.getLong(column));

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.lang.Short.class))

{

if (column == 0) objValue = new Short(resultSet.getShort(field.getName()));

else objValue = new Short(resultSet.getShort(column));

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.math.BigDecimal.class))

{

if (column == 0) objValue = resultSet.getBigDecimal(field.getName());

else objValue = resultSet.getBigDecimal(column);

if(resultSet.wasNull())

{

objValue = null;

}

}

else if (clazz.equals(java.lang.String.class))

{

if (column == 0) objValue = resultSet.getString(field.getName());

else objValue = resultSet.getString(column);

if(resultSet.wasNull())

{

objValue = null;

}

}

}

catch (Exception e)

{

throw new JRException("Unable to get value for field '" + field.getName() + "' of class '" + clazz.getName() + "'", e);

}

}

 

return objValue;

}

 

Could it be possible to include this? It would be useful...

Thanks a lot!!!

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...