aspose file tools
The moose likes JDBC and the fly likes PreparedStatements Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "PreparedStatements" Watch "PreparedStatements" New topic
Author

PreparedStatements

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

Is is possible for me to use preparedstatements for selecting data from the db ? If yes, how ? I know how to do updates and inserts but not selects.


Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35253
    
    7
That's what the PreparedStatement.executeQuery method is for.


Android appsImageJ pluginsJava web charts
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Thanks. I need some help understanding bits in the followng code:

What is the point in the "?" in the select statement ?
And what is "pstmt.setString(1,"000010");" doing ?





.
[ December 21, 2008: Message edited by: shaf maff ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35253
    
    7
The question mark indicates that the statement has a parameter at that location (which is later supplied by the setString method).

setString (1, "XYZ") causes the first parameter of the statement to be replaced by the string "XYZ". Note that it also takes care of adding surrounding single quotes if necessary. That way you end up with
and not

which would cause an error.

It's possible to have several parameters (in other words, question marks) in a statement, and then fill them in using setString(1,...), setString(2,...) etc.

The Java Tutorial talks about this at length: http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Thanks! One more question, can you do commits/rollbacks with preparedstatements?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35253
    
    7
Yes, there's nothing special about them in this regard - they work like any other Statement.
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Ahha - So how would I implement them (a small example would be nice ) ?


.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35253
    
    7
The JDBC tutorial I linked to has a chapter on transactions; I'm sure it contains sample code.
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Ive read many tutorials, they either miss out what I need to import or dont explain the code in detail or they are like over bloated and it feels like I have to read a book just to learn one functionality.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26201
    
  66

Originally posted by shaf maff:
Ahha - So how would I implement them (a small example would be nice ) ?

Your second post in this thread is the example. It creates a prepared statement, sets the parameters and executes it. What part of it are you having difficulty with that you are looking for more explanation?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
His question was rather regarding committing and rollbacking.

Well, that's fairly obvious. If you have multiple queries on a single connection which depends on each other (a transaction), then you may need to rollback the complete transaction when executing a query fails. This is only useful if autocommit is set to false.

This is fairly important if you're using connection pooling, because the connection may be reused for other queries/transactions. If you're executing only a single query at a time, then you normally don't need to worry about rollbacking.
[ December 21, 2008: Message edited by: Bauke Scholtz ]

Code depot of a Java EE / JSF developer | JSF / Eclipse / Tomcat kickoff tutorial | DAO kickoff tutorial | I ♥ Unicode
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PreparedStatements
 
Similar Threads
How this code can be improved?
Sniffing out apostrophe's in SQL Server
Filtering Data to prevent SQL Injection
Is there a limit to # of Statements over a Connection object
Batch updates of preparedstatement