4 Replies Latest reply: Aug 20, 2012 12:38 PM by rp0428 RSS

    Statement,Prepared Statement and callable statement

    956663

      Hi,

      Please let me know in which scenario we are using Statement,Prepared Statement and callable statement.

      and which is efficient one among the above.

      Thanks in advance

        • 1. Re: Statement,Prepared Statement and callable statement
          rp0428
          Welcome to the forum!
          >
          Please let me know in which scenario we are using Statement,Prepared Statement and callable statement.
          >
          We don't know what scenario you are using those in or if you are using them at all. Are you asking what they are?

          For document related questions you should consult the documentation or use your favorite search engine to get information.

          See the Java Tutorial
          http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
          >
          The main feature of a PreparedStatement object is that, unlike a Statement object, it is given a SQL statement when it is created. The advantage to this is that in most cases, this SQL statement is sent to the DBMS right away, where it is compiled. As a result, the PreparedStatement object contains not just a SQL statement, but a SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement SQL statement without having to compile it first.
          >
          The Javadocs for your Java SDK have the API for each of those classes and a description of what they are. And the Oracle JDBC Developer Guide has extensive information on how to use them.
          http://docs.oracle.com/cd/B28359_01/java.111/b31224/toc.htm
          • 2. Re: Statement,Prepared Statement and callable statement
            956663
            Thanks for the quick response....

            Let say if i've below two scenarios

            1.To insert the login information to database(firstnamre,lastname and so on).

            2.To cheeck the username and password from database(valid/invalid) .

            In the above scenario please let me know in which case i need to use statement/prepared statement.

            Thanks again !
            • 3. Re: Statement,Prepared Statement and callable statement
              DrClap
              You need to use PreparedStatement.

              Short explanation: Always use PreparedStatement.

              Slightly longer explanation: Always use PreparedStatement unless it can't possibly be made to work because of very strange requirements, in which case use Statement.

              Reason: It's easier to write the code, it's easier to understand the code, and it removes the exposure to SQL injection attacks.
              • 4. Re: Statement,Prepared Statement and callable statement
                rp0428
                >
                In the above scenario please let me know in which case i need to use statement/prepared statement.
                >
                The quote I provided above answers that question for you.