The Artima Developer Community
Sponsored Link

Java Answers Forum
Diff b/w prepare statement and prepare statement

5 replies on 1 page. Most recent reply: Mar 15, 2004 6:33 AM by Dhrubo

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
anamika

Posts: 11
Nickname: anamika
Registered: Feb, 2004

Diff b/w prepare statement and prepare statement Posted: Mar 14, 2004 8:17 AM
Reply to this message Reply
Advertisement
hi can you tell me the
Diff b/w a prepare statement and a statement ?


Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: Diff b/w prepare statement and prepare statement Posted: Mar 15, 2004 12:58 AM
Reply to this message Reply
Hi,

Definition:

The PreparedStatement is a slightly more powerful version of a Statement, and should always be at least as quick and easy to handle as a Statement.

Most relational databases handles a JDBC / SQL query in four steps:

1. Parse the incoming SQL query
2. Compile the SQL query
3. Plan/optimize the data acquisition path
4. Execute the optimized query / acquire and return data

A Statement will always proceed through the four steps above for each SQL query sent to the database. A PreparedStatement pre-executes steps (1) - (3) in the execution process above. Thus, when creating a PreparedStatement some pre-optimization is performed immediately. The effect is to lessen the load on the database engine at execution time.


Another advantage of the PreparedStatement class is the ability to create an incomplete query and supply parameter values at execution time. This type of query is well suited for filtering queries which may differ in parameter value only

Statement example
// Assume a database connection, conn.
Statement stmnt = null;
ResultSet rs = null;
try
{
   // Create the Statement
   stmnt = conn.createStatement();
 
   // Execute the query to obtain the ResultSet 
   rs = stmnt.executeQuery("select * from aTable");
}
catch(Exception ex)
{
 
   System.err.println("Database exception: " + ex);
}
[\java]
 
PreparedStatement example 
[java]
// Assume a database connection, conn.
PreparedStatement stmnt = null;
ResultSet rs = null;
try
{
   // Create the PreparedStatement
   stmnt = conn.prepareStatement("select * from aTable");
 
   // Execute the query to obtain the ResultSet 
   rs = stmnt.executeQuery();
}
catch(Exception ex)
{
   System.err.println("Database exception: " + ex);
}
 
[\java]
 
 
Viswa
-------

Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: Diff b/w prepare statement and prepare statement Posted: Mar 15, 2004 12:59 AM
Reply to this message Reply
Hi,

Definition:

The PreparedStatement is a slightly more powerful version of a Statement, and should always be at least as quick and easy to handle as a Statement.

Most relational databases handles a JDBC / SQL query in four steps:

1. Parse the incoming SQL query
2. Compile the SQL query
3. Plan/optimize the data acquisition path
4. Execute the optimized query / acquire and return data

A Statement will always proceed through the four steps above for each SQL query sent to the database. A PreparedStatement pre-executes steps (1) - (3) in the execution process above. Thus, when creating a PreparedStatement some pre-optimization is performed immediately. The effect is to lessen the load on the database engine at execution time.


Another advantage of the PreparedStatement class is the ability to create an incomplete query and supply parameter values at execution time. This type of query is well suited for filtering queries which may differ in parameter value only

Statement example
// Assume a database connection, conn.
Statement stmnt = null;
ResultSet rs = null;
try
{
   // Create the Statement
   stmnt = conn.createStatement();
 
   // Execute the query to obtain the ResultSet 
   rs = stmnt.executeQuery("select * from aTable");
}
catch(Exception ex)
{
 
   System.err.println("Database exception: " + ex);
}
 
PreparedStatement example 
 
// Assume a database connection, conn.
PreparedStatement stmnt = null;
ResultSet rs = null;
try
{
   // Create the PreparedStatement
   stmnt = conn.prepareStatement("select * from aTable");
 
   // Execute the query to obtain the ResultSet 
   rs = stmnt.executeQuery();
}
catch(Exception ex)
{
   System.err.println("Database exception: " + ex);
}
 
[\java]
 
 
Viswa
-------

Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: Diff b/w prepare statement and prepare statement Posted: Mar 15, 2004 12:59 AM
Reply to this message Reply
Hi,

Definition:

The PreparedStatement is a slightly more powerful version of a Statement, and should always be at least as quick and easy to handle as a Statement.

Most relational databases handles a JDBC / SQL query in four steps:

1. Parse the incoming SQL query
2. Compile the SQL query
3. Plan/optimize the data acquisition path
4. Execute the optimized query / acquire and return data

A Statement will always proceed through the four steps above for each SQL query sent to the database. A PreparedStatement pre-executes steps (1) - (3) in the execution process above. Thus, when creating a PreparedStatement some pre-optimization is performed immediately. The effect is to lessen the load on the database engine at execution time.


Another advantage of the PreparedStatement class is the ability to create an incomplete query and supply parameter values at execution time. This type of query is well suited for filtering queries which may differ in parameter value only

Dhrubo

Posts: 32
Nickname: dhrubo
Registered: Mar, 2003

Re: Diff b/w prepare statement and prepare statement Posted: Mar 15, 2004 6:32 AM
Reply to this message Reply
Hi,
I differ on this we can use bind variables with Statement sql string as well.
Now the preparedstatement will not be useful if we dont have connection pooling and subsequent pooling of prepared statement.

Dhrubo

Posts: 32
Nickname: dhrubo
Registered: Mar, 2003

Re: Diff b/w prepare statement and prepare statement Posted: Mar 15, 2004 6:33 AM
Reply to this message Reply
for the first time statement and prepared statement will be have same performance implications as all the 4 steps will be followed since we can cache preparedstatement objects this can be reused
dhrubo@indiainfo.com

Flat View: This topic has 5 replies on 1 page
Topic: Setting Private Variables Previous Topic   Next Topic Topic: Diff  b/w a API server and a Web server

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use