Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Statement vs PreparedStatements which is better in which situation

843859Oct 22 2008 — edited Oct 23 2008
Hi i read in a few forums and a few articles that PreparedStatement object performs better than the Statement object in situations where a high number of the same sqls are used because it precompiles the sql statement. such as inserting a 1000 employees into a table.

i did the following test and found that the gain for using PreparedStatements is sometimes not a gain but a loss.

What the test basically does is that it inserts an item into the table a 2000 times using both PreparedStatements and Statement objects. And it switches between which object it uses to insert item into the table. So in the end 1000 items would have been inserted using PreparedStatements and 1000 using Statement objects.

Am i doing somthing wrong with my test or is the so called gain by using PreparedStatements a fluke. Please advise.
import java.sql.*;

public class TEST {

    static int Prepcount = 1;
    static long PreptotalTime = 0;
    static long PrepstartTime = 0;
    static long PrependTime = 0;
    static long Prepavarage = 0;

    static int Stmtcount = 1;
    static long StmttotalTime = 0;
    static long StmtstartTime = 0;
    static long StmtendTime = 0;
    static long Stmtavarage = 0;

    static PreparedStatement pst;
    static Statement stmt;
    static ResultSet rs;

    static Connection conn;

    public static void usePrep() {
        try {

            pst = conn.prepareStatement("insert into Dhanu values (?,?)");
            PrepstartTime = System.currentTimeMillis();
            
            pst.setInt(1, Prepcount);
            pst.setInt(2, Prepcount);
            pst.executeQuery();
            PrependTime = System.currentTimeMillis();

            PreptotalTime = PreptotalTime + (PrependTime - PrepstartTime);
            Prepavarage = PreptotalTime / Prepcount;
            Prepcount++;

            pst.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void useStatement() {
        try {
            StmtstartTime = System.currentTimeMillis();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("insert into Dhanu values ("+Stmtcount+","+Stmtcount+")");
            StmtendTime = System.currentTimeMillis();
            StmttotalTime = StmttotalTime + (StmtendTime - StmtstartTime);
            Stmtavarage = StmttotalTime / Stmtcount;
            
            Stmtcount++;

            rs.close();
            stmt.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        try {

            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            conn = DriverManager.getConnection("jdbc:oracle:thin:@XXX.XXX.XXX.XXX:1521:XXX", "XXX", "XXX");
            System.out.println("Connected to DB");
            conn.setAutoCommit(true);
            
            for(int x=1;x<=2000;x++){
                
                if(x%100==0){
                   System.out.println("Count is "+x); 
                }
                
                if(x%2==0){
                    usePrep();
                }else
                    useStatement();
            }

            System.out.println("Prepcount " + Prepcount + " Prepavarage " + Prepavarage + " CurrentExecution " +(PrependTime - PrepstartTime)+ " Totaltime "+PreptotalTime);
            System.out.println("Stmtcount " + Stmtcount + " Stmtavarage " + Stmtavarage+ " CurrentExecution " +(StmtendTime - StmtstartTime)+ " Totaltime "+StmttotalTime);
            
            System.err.println("Statement time - Prep Time " + (StmttotalTime - PreptotalTime ));

        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }


    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 20 2008
Added on Oct 22 2008
9 comments
647 views