• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

the real concept behind statement object

 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you don't -- you can reuse the same Statement object for multiple queries.
 
SJ Adnams
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exception did you get? I don't see why it would be necessary to retrieve the columns in order.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic