JDBC Prepared Statement

Let’s learn JDBC Prepared statement in Java. PreparedStatement interface is a subinterface of Statement. PreparedStatement is used to execute parameterized query.

It Improves the system performance because if you are using PreparedStatement interface because the query is compiled only once.

 

We will discuss this and other JDBC details in JDBC Tutorials. For now, Let’s see the example how can we use PrepatedStatement to execute the query using Java Application.

 

In our example, Let’s use the select statement to execute the SQL using Java application.

  1. Login your my SQL database.
  2. Run command ‘show databases;’, it will show the list of all available database.
  3. Run command ‘use sakila;’ ‘sakila’ is a sample database in our DB. use command is to use this database;
  4. Run command ‘show tables;’.  It will display all tables available in the ‘sakila’ DB.

Here, We can see many tables. Here we have table ‘actor’, use desc actor; command to see the structure of ‘actor’ table.

Here we have 4 columns in this table. Now our task is to identify all records whose the first name starts with the character ‘A’.

 

Do you know the SQL query of this?

select * from actor where first_name like ‘a%’;

(You can refer our SQL tutorials to get command over SQL)

Now we are going to do this using Java application.

 

PreparedStatement example in Java

 

You can see, how can query from the database. This is the way how can we validate the data against the data base. In coming tutorials, we will validate the database against the inserted data as well. But understand the concept of preparedStatement was necessary to understand the process of the query from a database using java application.

 

In the coming time, we will discuss the complete JDBC in detail in Java JDBC tutorials.

0 Comment

Leave a Reply