Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing a Statement instead of a Connection?

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

If I have a sequential action, spread over multiple methods, that makes very many SQL Queries, does it make sense to pass the Statement object to the different methods instead of passing the Connection and calling createStatement() every time?

Thank you,
Yuriy
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you provide more details , like what kind of sequencial operation you are doing in your code, are all those operation are insert or mixed operation of insert/update. whether they are on same table or multiple tables ?

in my view spanning one conection /statement to multiple places makes maintainance difficult in long run. It also make debugging process long.

I would rather prefer collect all queries at one place and execute them.if possible I would use addBatch() of prepared statement if operation is on same table.

Shailesh
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For advanced transaction processing, better see JTA.
[ May 04, 2005: Message edited by: Adeel Ansari ]
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replies!

All of the actions are read-only queries, and at the moment at least it is assumed that none of the information being queried will be changed while the queries are in progress, so transactions are being ignored. I cannot do a batch because subsequent queries depend on the results of the prior queries.

What I am actually doing is building an XML file (using JDom) based on the contents of the database whose schema that models a tree-type hierarchy. A single table contains all of the parent-child relationships for every tree in the database. The

Element createElement(Connection conn, String id, String tableName)

method creates an Element (JDom's version of Node) with attributes populated by "SELECT * FROM <tableName> WHERE ID = <id>". Then recursively calls createElement() with the identifiers and table names that the relationship table lists for this element's children, and adds those elements to the content of the current one.

So I'm wondering if it maybe be more efficient to pass the Statement instance, instead of the Connection instance.

Thank you,
Yuriy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic