You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by ca...@calidris.f9.co.uk on 2003/02/21 12:55:17 UTC

[BeanUtils] DynaBean and SQL problem

I'm trying to use DynaBeans to copy the results of a SQL select into my bean and it doesn't seem to work.

This code *does* work:
----
PreparedStatement lStatement = lConnection.prepareStatement(
                                           "select agr_agreement_number as agreementNumber from agreements WHERE agr_agreement_number = ?");
      lStatement.setString(1, pAgreementNumber);

      ResultSet lResults = lStatement.executeQuery();
      lResults.next();
      
     lAgreement.setAgreementNumber(lResults.getString("agreementNumber"));
----
If I migrate this code to use the Dynabean (I eventually want to select more columns and auto copy them using copyProperties) I get the following code:
----
      PreparedStatement lStatement = lConnection.prepareStatement(
                                           "select agr_agreement_number as agreementNumber from agreements WHERE agr_agreement_number = ?");
      lStatement.setString(1, pAgreementNumber);

      ResultSet lResults = lStatement.executeQuery();
     
      ResultSetDynaClass lResultsSet = new ResultSetDynaClass(lResults);
      Iterator lRows = lResultsSet.iterator();

      if (lRows.hasNext())
      {
        DynaBean lRow = (DynaBean) lRows.next();

        //this.copyProperties(lAgreement, lRow);
        lAgreement.setAgreementNumber((String) lRow.get("agreementNumber"));
      }
----
This does not work, giving me the error:

 java.lang.IllegalArgumentException: agreementNumber

which seems to be from the getting of the property agreementNumber from the DynaBean.

I'm using BeanUtils 1.6.1, Sun java 1.4.0 on Windows NT, jboss 3.0.4, and the AgreementNumber is a String.

Is this a bug or am I doing something wrong?

Cheers,

Tim.


Re: [BeanUtils] DynaBean and SQL problem

Posted by Tim Sawyer <ti...@calidris.force9.co.uk>.
I've sorted it out.

The problem was that I was using an Oracle database, and expecting
"agr_agreement_number as agreementNumber" to make the value available
using get("agreementNumber").  

It doesn't, because the default in BeanUtils is to convert the text of
the column name to lower case.  If this is turned off (by passing false
as the second parameter of the constructor) there is still a problem. 
Oracle returns metadata in all upper case, so the column name is
returned as AGREEMENTNUMBER.  This can be fixed by putting quotes around
the column name.  

So, this works:

"select agr_agreement number as \"agreementNumber\" from agreements"

and the value can then be gathered using:

lRow.get("agreementNumber");

Tim.

On Fri, 2003-02-21 at 15:00, Stefan Smeets wrote:
> could you post the code of the Entity ... declaration and accessor methods ?
> 
> grtz
> 
> stef
> 
> 
> 
> ----- Original Message -----
> From: <ca...@calidris.f9.co.uk>
> To: <co...@jakarta.apache.org>
> Sent: Friday, February 21, 2003 12:55 PM
> Subject: [BeanUtils] DynaBean and SQL problem
> 
> 
> >
> > I'm trying to use DynaBeans to copy the results of a SQL select into my
> bean and it doesn't seem to work.
> >
> > This code *does* work:
> > ----
> > PreparedStatement lStatement = lConnection.prepareStatement(
> >                                            "select agr_agreement_number as
> agreementNumber from agreements WHERE agr_agreement_number = ?");
> >       lStatement.setString(1, pAgreementNumber);
> >
> >       ResultSet lResults = lStatement.executeQuery();
> >       lResults.next();
> >
> >      lAgreement.setAgreementNumber(lResults.getString("agreementNumber"));
> > ----
> > If I migrate this code to use the Dynabean (I eventually want to select
> more columns and auto copy them using copyProperties) I get the following
> code:
> > ----
> >       PreparedStatement lStatement = lConnection.prepareStatement(
> >                                            "select agr_agreement_number as
> agreementNumber from agreements WHERE agr_agreement_number = ?");
> >       lStatement.setString(1, pAgreementNumber);
> >
> >       ResultSet lResults = lStatement.executeQuery();
> >
> >       ResultSetDynaClass lResultsSet = new ResultSetDynaClass(lResults);
> >       Iterator lRows = lResultsSet.iterator();
> >
> >       if (lRows.hasNext())
> >       {
> >         DynaBean lRow = (DynaBean) lRows.next();
> >
> >         //this.copyProperties(lAgreement, lRow);
> >         lAgreement.setAgreementNumber((String)
> lRow.get("agreementNumber"));
> >       }
> > ----
> > This does not work, giving me the error:
> >
> >  java.lang.IllegalArgumentException: agreementNumber
> >
> > which seems to be from the getting of the property agreementNumber from
> the DynaBean.
> >
> > I'm using BeanUtils 1.6.1, Sun java 1.4.0 on Windows NT, jboss 3.0.4, and
> the AgreementNumber is a String.
> >
> > Is this a bug or am I doing something wrong?
> >
> > Cheers,
> >
> > Tim.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 


Re: [BeanUtils] DynaBean and SQL problem

Posted by Stefan Smeets <st...@staf.pi.be>.
could you post the code of the Entity ... declaration and accessor methods ?

grtz

stef



----- Original Message -----
From: <ca...@calidris.f9.co.uk>
To: <co...@jakarta.apache.org>
Sent: Friday, February 21, 2003 12:55 PM
Subject: [BeanUtils] DynaBean and SQL problem


>
> I'm trying to use DynaBeans to copy the results of a SQL select into my
bean and it doesn't seem to work.
>
> This code *does* work:
> ----
> PreparedStatement lStatement = lConnection.prepareStatement(
>                                            "select agr_agreement_number as
agreementNumber from agreements WHERE agr_agreement_number = ?");
>       lStatement.setString(1, pAgreementNumber);
>
>       ResultSet lResults = lStatement.executeQuery();
>       lResults.next();
>
>      lAgreement.setAgreementNumber(lResults.getString("agreementNumber"));
> ----
> If I migrate this code to use the Dynabean (I eventually want to select
more columns and auto copy them using copyProperties) I get the following
code:
> ----
>       PreparedStatement lStatement = lConnection.prepareStatement(
>                                            "select agr_agreement_number as
agreementNumber from agreements WHERE agr_agreement_number = ?");
>       lStatement.setString(1, pAgreementNumber);
>
>       ResultSet lResults = lStatement.executeQuery();
>
>       ResultSetDynaClass lResultsSet = new ResultSetDynaClass(lResults);
>       Iterator lRows = lResultsSet.iterator();
>
>       if (lRows.hasNext())
>       {
>         DynaBean lRow = (DynaBean) lRows.next();
>
>         //this.copyProperties(lAgreement, lRow);
>         lAgreement.setAgreementNumber((String)
lRow.get("agreementNumber"));
>       }
> ----
> This does not work, giving me the error:
>
>  java.lang.IllegalArgumentException: agreementNumber
>
> which seems to be from the getting of the property agreementNumber from
the DynaBean.
>
> I'm using BeanUtils 1.6.1, Sun java 1.4.0 on Windows NT, jboss 3.0.4, and
the AgreementNumber is a String.
>
> Is this a bug or am I doing something wrong?
>
> Cheers,
>
> Tim.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>