File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Passing a Statement instead of a Connection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Passing a Statement instead of a Connection?" Watch "Passing a Statement instead of a Connection?" New topic

Passing a Statement instead of a Connection?

Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429

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,
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1082

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.


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
For advanced transaction processing, better see JTA.
[ May 04, 2005: Message edited by: Adeel Ansari ]
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
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,
I agree. Here's the link:
subject: Passing a Statement instead of a Connection?
It's not a secret anymore!