Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe12.iad.POSTED!83aa503d!not-for-mail From: Daniel Pitts User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: JDBC, PreparedStatement and named parameters References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Lines: 40 Message-ID: X-Complaints-To: abuse@newsrazor.net NNTP-Posting-Date: Fri, 20 Jul 2012 17:22:55 UTC Date: Fri, 20 Jul 2012 10:22:54 -0700 X-Received-Bytes: 2465 Xref: csiph.com comp.lang.java.programmer:16158 On 7/20/12 2:39 AM, Andreas Leitgeb wrote: > I'm rather new to JDBC, and somewhat experienced with > database access in other languages, like with ProC or > with the sqlplus-shell. > > While in C/C++ eSql with ProC (not sure about correct > nomenclature) I can use named variables for the parameters, > jdbc seems like it would only allow positional parameters. > > With Google, I stumbled over this approach: > > http://www.javaworld.com/javaworld/jw-04-2007/jw-04-jdbc.html > > which boils down to pre-processing the sql-statement, replacing > the named variables by "?" while creating an array of the parameters > such, that e.g. a snippet like this: > "... :a :c :b :a ..." and setting a -> x1, b -> x2, c -> x3 > would turn into > "... ? ? ? ? ..." with an parameter array of { x1, x3, x2, x1 } > > That almost looks like what I was hoping for, but when I see > that the article is 5 years old, then I'd hope that something > similar might meanwhile have been turned into a standard... > > Does anyone here know of a way to use either named variables, > or at least something like these "foo {1} bar {0}" things > used in jsf4j and java.text.MessageFormat? > Depending on your needs, a JPA provider (such as Hibernate) may be a better approach. It moves you away from low-level SQL, and into more object oriented notation. It isn't always the best solution, and my experience with Hibernate has been mixed. It's worth looking into and learning about it. They tend to be useful if you do exactly what they were designed for, and then they get in your way when you need to do something different.