PreparedStatement

Bharath Thippireddy
A free video tutorial from Bharath Thippireddy
IT Architect and Best Selling Instructor- 350000+ students
4.5 instructor rating • 28 courses • 378,076 students

Learn more from the full course

JDBC Servlets and JSP - Java Web Development Fundamentals

Create Java Web Applications from scratch in easy steps

08:37:58 of on-demand video • Updated October 2020

  • Develop Dynamic Web Applications
  • Learn the fundamentals of JDBC and using the different interfaces in the JDBC API
  • Learn , Create and Deploy Servlets
  • Initialize parameters dynamically
  • Learn how to manage sessions
  • Understand how JSPs can help to separate Web logic and functionality from page layout
  • Learn the meaning and importance of MVC
  • Explore how to make JSPs smaller and more powerful with JSTL, custom tags
  • Understand and Implement Connections Pooling
  • Secure you web application
  • Learn how to manage sessions
English This lecture you will learn about to prepare a statement which is the interface of the statement interfacing the database API. It is the pre-compiled version of a sequel statement. Do I understand that better. You need to understand how a statement works first. So if you use the state print interface to be execute select query or an update or insert statement like this the first thing that happens in the flow is that the Daily the client since these statements through the right one at a time through the driver to the database the database will first compile these statements. That is it takes the select statement. It will check if the statement is syntactically OK by passing through it. It says if this statement is following the Escorial specification and if there are any string values like in this case of the insert preexisting value should be within courts. So all those checks are done during compile time then it will execute that statement and send the appropriate results back to the client through the driver. So every time you do this for example now I'm inserting one part if I want to insert let's say 20 employees every day makes good this statement. It will be first compiled and then executed. So this compilation step happens every time which will degrade the performance of our application especially and the applications will be inserting Posen sold millions of records in the database for that same table. So he should not be compiling it every time. That is where the prepared statement comes in for the rescue. The prepared statement is it changed into a face off the statement interface. We create a prepared statement using connection Daut prepared statement mattered and we pass it to pad as well which we want to execute multiple times for example here it is the insert statement into their employee table which takes three values instead of hard coding the values here or baking them dynamically. We put them without question marks so we use the question marks to mark the placeholders in the prepared statement and that they send back a statement VB compiled right here. And then at a later point in time we will set the values for this question mark using the 6 x 6 6 methods available on a prepared statement. These x x x similar results that we have set in set string set float and so on for different data types available in Java. So if I am setting an ID I would lose set. If I am setting a string name I lose said string and so on. Once we bind the parameters this step offsetting the parameters is called binding parameters. Once we do that we can simply use the exact query on this prepared statement. If it is that select query or execute update if it is the insert update or delete. These methods are same as the statement except for they don't pick any parameters. In case of statement this is where we pass the queries to them within this table of course as a parameter. But since we are passing the query right when we create the prepared statement we need not pass it again and we execute. So right here these queries compiled and kept and we create the prepared statement and then we keep replacing the parameters. Every time we are adding a new employee only the parameters get replaced. This question marks and it is inserted into the database. There is no completion that happens again and again. So to summarize a prepared statement is it precompile an office sequel statement represented in an object oriented fashion. The benefit of it is that the compiler can step happens only once and from that point in time if we execute these statements they insert or select statements multiple times. We can replace or bind the values by having placeholders for them. Improving the performance of our application.