How can you use PreparedStatement?

This special type of statement is derived from the more general class, Statement. If you want to execute a Statement object many times, it will normally reduce execution time to use a PreparedStatement object instead. The advantage to this is that in most cases, this SQL statement will be sent to the DBMS right away, where it will be compiled. As a result, the PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement 's SQL statement without having to compile it first.E.g.PreparedStatement updateSales = con.prepareStatement("UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?");

Showing Answers 1 - 16 of 16 Answers

guptach

  • Sep 23rd, 2005
 

A PreparedStatement is a precompiled statement which takes the values dynamically at runtime. When we want to execute a statement number of times, it is better to use PreparedStatement ratherthan using Statement object as this will compile everytime when it is being executed. But the PreparedStatement will stored in the Database when it is executed for the first time and that compiled Statement is used from the next time. Obviously this will reduce the execution time as well as the response time in giving results. What all we need to do is to provide the inputs to the PreparedStatement at runtime.

  Was this answer useful?  Yes

Jiansen Niu

  • Jan 18th, 2007
 

Use PreparedStatement for:

1. You want to execute the same SQL statement multiple times

2. You dont like escape "/" etc. characters when the data you want to insert, update .. has such characters (example date), you can use setDate (), setString ().... method to take the advantage of not escape such characters that are widly used in Date and String.

3. You want to avoid SQL injection.

  Was this answer useful?  Yes

rameshvdnv

  • Apr 27th, 2007
 

Let us take a simple scenario,consider a jdbc application which has to execute about 20 insert statements if we use statement object what it does is first it will parse those 20 statements and then executes those statements which results more network traffic or more amount of data is transferred between the client and the server.
  If u use preparedstatement instead of statement object first of all the DBServer parses the statement and sends the values provided by the jdbcdriver asks the server to execute the statement that it has parsed earlier.Everytime the DBServer will not parse those statements but it will send the values by the driver asks it to execute the earlier statement it has parsed earlier.In this way we can reduce the network traffic between the client and the server and preparedstatement is more efficient than statment 0bject.

  Was this answer useful?  Yes

kirankpv

  • Feb 9th, 2008
 

Prepared statement can be used to repeatedly execute the same sql statement with different set of values prepared statement reducess the number of parses and improves the performance of the application

  Was this answer useful?  Yes

sampra

  • Feb 11th, 2008
 

If we have to use palce holder mechnaism and pass compiled query in the sql engine the we use prepares stament.For one prepared stmt we can use only one query

  Was this answer useful?  Yes

kalyan789

  • Apr 23rd, 2009
 

Following above answer, we can set input values by specifying column index (index starts from 1) and value using corresponding Java set methods like setInt/String/Float etc.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions