• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Oracle JDBC: Statements is better ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Use the Statement object for time-critical or dynamic SQL statements

When it comes to executing a SQL statement, you have two choices: you can use a PreparedStatement object or a Statement object. A PreparedStatement parses and compiles a SQL statement once, no matter how many times you reuse it. When you use a Statement, each time a SQL statement is executed, it is again parsed and compiled. This might lead you to think that using a PreparedStatement would be faster than using a Statement; however, my tests show this is not the case on the client side. So, for time-critical SQL operations, unless you are going to batch SQL statements, you should consider using Statements.


http://www.onjava.com/pub/a/onjava/2001/12/19/oraclejdbc.html

Anybody has your voices for/against the points above ? is this statement testable?

Thanks.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh its testible, just create a preparedstatement that does not insert any values and compare it with the same excution as a statement.

Since a preparedstatement is a more complex data structure than a statement I am not surprised that it takes longer to run, but most of the time for code maintainability I would pick a prepared statement over a statement even if I was not sending it any parameters.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to guard against SQL injection attacks, I would use a PreparedStatement instead of a Statement.

(Also note that the article in question was published in 2001. In my opinion, 5-year-old benchmarks are of little value.)
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Oracle if you re-use the SQL in a statement with literal values substituted then each is regarded as a unique piece of SQL by the query optimiser, which will hard parse it to generate the query plan.

With a PreparedStatement with Bind variables, after the first use and hard parse, subsequent executions will re-use the already generated query plan.

Hard coded literals and hard parsing will not only use more resources in the database, but will also serialize access to shared memory structures used by the database. The application will not scale.

Any performance gain on the client side from using a Statement object will be lost on the DB side if it is re-used.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually here's the real question, could one take a preparedstatement and write an implementation that uses a statement and therefore is always as fast? The answer is probably yes. Sometimes the java implementation might be such that more advanced structures are slower but the hope is that by using the advanced data structure, the implementation will eventually improve such that the users could take advantage of the data structure without any performance tradeoffs.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic