Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
JDBC
3.
Types of Statements 
3.1.
Create statement
3.1.1.
Syntax
3.1.2.
Implementation
3.1.3.
Example
3.2.
Prepared statement
3.2.1.
Syntax
3.2.2.
Implementation
3.2.3.
Example
3.3.
Callable statement
3.3.1.
Syntax
3.3.2.
Implementation
3.3.3.
Example
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Types of Statements in JDBC

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Java Database Connectivity basically is an application programming interface(API) for Java. This helps in connecting and executing a query with the database. Thus, it helps us in defining how a client accesses the database. This can be classified as a Java-based data access technology for connecting to a Database

When using JDBC, we will come across several other statements that basically define how we will be accessing the database and up to which level of access. In this blog, we will learn about the different types of statements in JDBC and how we can use them as an API to the database.

JDBC

JDBC is used to create SQL statements in Java by providing methods to execute queries with the database. Thus, JDBC works as an API for Java-Based Programs. JDBC also provides universal data access from the Java programming language. These interfaces also define the methods that are generally known to assist the bridge data type differences that exist between Java and SQL data types basically used in a database.

Also see, Duck Number in Java and Hashcode Method in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of Statements 

Now that we know about JDBC, let's learn about the different types of statements that constitute the JDBC queries and enable them to connect to the database and use it.

There are basically three types of statements in JDBC: Create statements, Prepared statements, Callable statements.

Create statement

This is usually used when we want a general-purpose access to the database and can be very helpful when running static SQL statements in the runtime.

Syntax

Statement statement = connection.createStatement();

Implementation

Once an object is created it can be executed using three ways:

  • boolean execute(String SQL): This is used to execute SQL DDL statements or for dynamic SQL statements.
  • int executeUpdate(String SQL): This is used to execute INSERT, DELETE, UPDATE i.e. DML statements.
  • ResultSet executeQuery(String SQL): This is used to execute SELECT statements in SQL.

Example

Statement statement = con.createStatement();
String sql = "select * from orders";
ResultSet result = statement.executeQuery(sql);

Prepared statement

These types of statements basically represent recompiled SQL statements that can be executed many times. Thus, instead of having actual parameters in the SQL Queries it has “?” in place of the parameters to be passed dynamically by using the methods of prepared statements at the run time.

Syntax

String query = "INSERT INTO ORDERS(id, product) VALUES(?,?)";
Statement test = connection.createStatement();
test.setInt(1, 45);
test.setString(2, "Pen");

Implementation

Once an object is created it can be executed using three ways:

  • boolean execute(String SQL): This is used to execute the static SQL statement present in the prepared statement.
  • int executeUpdate(String SQL): This is used to execute INSERT, DELETE, UPDATE i.e. DML statements.
  • ResultSet executeQuery(String SQL): This is used to execute SELECT statements in SQL.

Example

PreparedStatement test = con.prepareStatement("
SELECT product FROM ORDERS WHERE ID = ?");
test.setInt(1,45);
ResultSet result = test.executeQuery();

Callable statement

In some cases we need to work with multiple databases for some task and the scenario becomes very complex here. Thus, rather than sending multiple queries we can simply send the required data to the stored procedure. This will decrease the load on the database significantly. This is where callable statements work as the stored procedures which are essentially a group of statements that we compile in the database and are very beneficial.

Syntax

CallableStatement cs = con.prepareCall("{call procedure_name(?,?)}")

Implementation

Once an object is created it can be executed using three ways:

  • boolean execute(String SQL): This is used to execute in case of callable statements.

Example

CallableStatement test = con.prepareCall("{call orderinfo(?,?)}");
test.setInt(1, 45);
test.setString(2, "Pen");
cs.execute();


Practice by yourself on java online compiler.

Must Read Conditional Statements in Java

FAQs

1. What are DDL and DML commands?

Ans: DDL(Data Definition Language) defines the structure of the database we are storing our data in and DML(Data Manipulation Language) manipulates the data inside the database.

 

2. What protocol does JDBC use?

Ans: JDBC uses the TTC protocol developed by Oracle to access the Oracle Relational Database Management System.

 

3. What is JDBC type?

Ans: The JDBC type is what determines the conversion between SQL server data types and Java language data types.

 

4. What is the default port for JDBC?

Ans: JDBC has a default port of 1433 but can be made to listen to any port.

 

Key Takeaways

In this blog, we discussed how we can use the different types of JDBC statements to connect to the database and use it.

You may want to learn more about JDBC connection here.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Previous article
JDBC Connection in Java
Next article
Reflection API in Java
Live masterclass