-
Prepared Statement/Statement comparison (7 messages)
- Posted by: T J
- Posted on: October 15 2002 13:47 EDT
Does anyone have a really good benchmark comparison between using a PreparedStatement and not? I'm trying to figure out how much a PreparedStatement is worth. ThanksThreaded Messages (7)
- Prepared Statement/Statement comparison by Dave Wolf on October 15 2002 20:13 EDT
- Prepared Statement/Statement comparison by Ulas Ergin on October 16 2002 12:18 EDT
- Prepared Statement/Statement comparison by Leonard Gurevich on October 16 2002 23:03 EDT
- Prepared Statements are more portable and easier to use by Dave C on October 17 2002 12:17 EDT
-
Prepared Statements are more portable and easier to use by Marco Ingco on October 17 2002 02:16 EDT
- Prepared Statements are more portable and easier to use by T J on October 23 2002 02:20 EDT
-
Prepared Statements are more portable and easier to use by Marco Ingco on October 17 2002 02:16 EDT
- Prepared Statements are more portable and easier to use by Dave C on October 17 2002 12:17 EDT
- Prepared Statement/Statement comparison by Leonard Gurevich on October 17 2002 11:34 EDT
-
Prepared Statement/Statement comparison[ Go to top ]
- Posted by: Dave Wolf
- Posted on: October 15 2002 20:13 EDT
- in response to T J
Tracy,
The things you need to think about are
1) Will you be using this same Statement over and over. An example would be using it within a loop.
2) You "can" cache PreparedStatements between connections (for instanbce as a member in a bean where each bean would pull a connection from the cache) but only if your Driver supports it. For instance the latest Oracle driver now supports caching PreparedStatements across connections from a DataSource pool. Not all drivers do.
If you are not
1) Using the same Statement across a loop or
2) Going to cache across Connections using a Driver which supports it
Then there is little to no gain from the PreparedStatement. If you are doing any of the above then the PreparedStatement will help out considerably.
Dave Wolf
Personified Technologies LLC -
Prepared Statement/Statement comparison[ Go to top ]
- Posted by: Ulas Ergin
- Posted on: October 16 2002 12:18 EDT
- in response to Dave Wolf
Prepared Stametent and Statement also has effects on the db size.
Statements are parsed at the db level every time but prepered statements are parsed once and bind variables are plugged and executed every time. -
Prepared Statement/Statement comparison[ Go to top ]
- Posted by: Leonard Gurevich
- Posted on: October 16 2002 23:03 EDT
- in response to T J
With Prepared Statement you don't need to worry about qoutation in String parameters. -
Prepared Statements are more portable and easier to use[ Go to top ]
- Posted by: Dave C
- Posted on: October 17 2002 12:17 EDT
- in response to Leonard Gurevich
Because you do not have to remember how to quote Strings (or even deal with it), nor any other type (like the dreaded Date). The driver handles creating the query for you. Seems to me there is no reason not to use PreparedStatement. -
Prepared Statements are more portable and easier to use[ Go to top ]
- Posted by: Marco Ingco
- Posted on: October 17 2002 14:16 EDT
- in response to Dave C
My general policy here is:
1) if i am going to use a plain SQL statement without any parameter and I'm gonna use it just once, I'll go for the Statement.
2) if i am going to use a plain SQL statement without any parameter and I'm gonna use it in a loop, I'll go for the PreparedStatement.
3) if i have parameters, there's no question about it, PreparedStatement.
There is just a bit of a problem with the PreparedStatement: if you pass a null object into some of its setXXX methods, e.g., pStmt.setObject(1, myObj) and myObj is null, you'll get SQLException errors (Invalid column, or something like that). You must use the setNull(...) method to overcome that.
Here's what we did to overcome that:
public void setPrepStmtObject(PreparedStatement pStmt, int col, Object data, int type){
if (data == null){
pStmt.setNull(col, type);
}
else {
pStmt.setObject(col, data);
}
}
We use this everywhere we know that a particular object could be null.
By the way, whatever you choose, be consistent.
Regards,
Marco -
Prepared Statements are more portable and easier to use[ Go to top ]
- Posted by: T J
- Posted on: October 23 2002 14:20 EDT
- in response to Marco Ingco
Thanks guys, that's what I understood as well. Esp. in a pooling environemnt where the conns stay around, you know darned good and well probably every sql staement you have will be executed thousands of times before the server is bounced. -
Prepared Statement/Statement comparison[ Go to top ]
- Posted by: Leonard Gurevich
- Posted on: October 17 2002 11:34 EDT
- in response to T J