Re: Jaw drop: JDBC will let you update rows with the wrong data type (within reason)

Niels Liisberg <nli-y1igGha7WhZnI0JKdf8LmQC/[email protected]> Thu, 7 Oct 2021 10:48:41 +0200
Newsgroups gmane.comp.lang.as400.java
Message-ID <CADAk_-jNm+VJzjh8ZwvcQcvQAkXk=yw9g_+6bhPCecWsba3JAQ@mail.gmail.com>
JPA - I am a big fan !! However sometimes you need to go below the beauty
of JPA - If your have a legacy ( QS36F ) database or you jys need to do
something more dynamic that realy dont maps into the entity manager ...

Now: auto-cast ... Yes it has improved a lot: now you can even use concat
on int, date , chars etc. happily without thinking about the SQL data types
... I think all the string operations actually now also work on numeric and
data/time/timestamp .. Have not tried it yet though ...

So you post Charles - "Jaw drop: JDBC" - is actually not JDBC .. it is "Jaw
drop: SQL" :)



On Thu, Oct 7, 2021 at 9:06 AM Colin Williams <[email protected]>
wrote:

> Do you use JPA? Just asking as eclipselink or hibernate will manage all
> this for you and you never have to write jdbc type code or sql again.
>
> On Wed, 6 Oct 2021, 20:58 David Gibbs via JAVA400-L, <
> [email protected]> wrote:
>
> > Folks:
> >
> > I just encountered a situation where JDBC allowed me to update rows in a
> > table where the values did not match the column definition.
> >
> > As long as you use the setObject method, you can set a numeric column to
> > a string value and a character column with a numeric value.
> >
> > The value contents have to match the column definition, but it doesn't
> > seem to care what the data type being passed in.
> >
> > For instance:
> >
> > You create a table as such ...
> >
> > create table testtab1 (
> >    decfield decimal(8,0),
> >    charfield char(10)
> > )
> >
> > ... and then run code like this ...
> >
> > try (
> >    Connection conn = DriverManager.getConnection(url, props);
> >    PreparedStatement stmt = conn.prepareStatement("insert into
> > dxglib/testtab1 (decfield,charfield) values(?,?)");
> > ) {
> >
> >    stmt.setObject(1, 1);
> >    stmt.setObject(2, "xyz");
> >    stmt.executeUpdate();
> >
> >    stmt.setObject(1, "2");
> >    stmt.setObject(2, 3);
> >    stmt.executeUpdate();
> >
> >    stmt.setObject(1, "A");
> >    stmt.setObject(2, 3);
> >    stmt.executeUpdate();
> >
> > } catch (Exception e) {
> >    e.printStackTrace();
> > }
> >
> > .. the first two updates will work fine, but the 3rd will fail.
> >
> > david
> > --
> > I'm riding in the American Diabetes Association's Tour de Cure to raise
> > money for diabetes research, education, advocacy, and awareness.  You
> > can make a tax-deductible donation to my ride by visiting
> > https://mideml.diabetessucks.net.
> >
> > You can see where my donations come from by visiting my interactive
> > donation map ... https://mideml.diabetessucks.net/map (it's a geeky
> > thing).
> > --
> > This is the Java Programming on and around the IBM i (JAVA400-L) mailing
> > list
> > To post a message email: [email protected]
> > To subscribe, unsubscribe, or change list options,
> > visit: https://lists.midrange.com/mailman/listinfo/java400-l
> > or email: JAVA400-L-request-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org
> > Before posting, please take a moment to review the archives
> > at https://archive.midrange.com/java400-l.
> >
> > Help support midrange.com by shopping at amazon.com with our affiliate
> > link: https://amazon.midrange.com
> >
> --
> This is the Java Programming on and around the IBM i (JAVA400-L) mailing
> list
> To post a message email: [email protected]
> To subscribe, unsubscribe, or change list options,
> visit: https://lists.midrange.com/mailman/listinfo/java400-l
> or email: JAVA400-L-request-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org
> Before posting, please take a moment to review the archives
> at https://archive.midrange.com/java400-l.
>
> Help support midrange.com by shopping at amazon.com with our affiliate
> link: https://amazon.midrange.com
>
-- 
This is the Java Programming on and around the IBM i (JAVA400-L) mailing list
To post a message email: [email protected]
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/java400-l
or email: JAVA400-L-request-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org
Before posting, please take a moment to review the archives
at https://archive.midrange.com/java400-l.

Help support midrange.com by shopping at amazon.com with our affiliate
link: https://amazon.midrange.com