aspose file tools*
The moose likes JDBC and the fly likes the real concept behind statement object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "the real concept behind statement object" Watch "the real concept behind statement object" New topic
Author

the real concept behind statement object

Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
I would like to know everything I can do with a Statement object. I mean:
How many times can I call execute method?
Can I call executeUpdate on a Statement object and then call executeQuery???
do I need to create a statement object to every query I need to execute???
thanks in advance!!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


I would like to know everything I can do with a Statement object.

See the javadoc.

How many times can I call execute method?

Any number of times.

Can I call executeUpdate on a Statement object and then call executeQuery???

Yes, you could, but it generally wouldn't make sense; executeUpdate is for queries that don't return a result (like UPDATE) and executeQuery is for those that do. Usually only one or the other makes sense for a given query.

do I need to create a statement object to every query I need to execute???

Yep, you sure do!


[Jess in Action][AskingGoodQuestions]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
No you don't -- you can reuse the same Statement object for multiple queries.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
SJ Adnams
Ranch Hand

Joined: Sep 28, 2001
Posts: 925
No you don't -- you can reuse the same Statement object for multiple queries.

(As an aside) remember to add stmt.close() when you do this..
PreparedStatement stmt = connection.prepareStatement(..);
... do stuff
stmt.close(); // important
stmt = connection.prepareStatement(..something new);
.. more stuff
(hehe yep, that one bit me)
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But I'm talking about Statements, not PreparedStatements.
In your example, you are in fact creating a new PreparedStatement for the next query.
[ August 14, 2003: Message edited by: Ron Newman ]
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
I agree with Ron Newman,
I would like to know what can I do with a statement object not what I can do with the place holder for objects (variables)...
for example:
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("selec ...");
stmt.executeUpdate("insert into...");
stmt.executeUpdate("update x set ..");
rst=stmt.executeQuery("select ...");
//end of job so close everything...
rst.close();
stmt.close();
con.close();
Can I do this???
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
That should work, though you may want to protect some of that code with try..finally so that things get closed even if an exception occurs.
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
Great!!! Just what I wanted to read. Just one more thing dos it work with every db system??? I'm asking this because I had to create many connections to create many statements because I had the following problem:
table user:
name|id|age|
a | 1| 13|
b | 2| 78|
c | 3| 87|
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from user");
rst.next();
rst.getString("id");
rst.getString("name");//exception here.

To avoid this exception I had to get each column in the same order they are in db or create another statement object to create another resultset...
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
What exception did you get? I don't see why it would be necessary to retrieve the columns in order.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: the real concept behind statement object