How to Create a Statement Object in JDBC ?
Last Updated :
28 Apr, 2025
JDBC stands for Java Database Connectivity. It is used in Java-based API which allows Java Applications to interact and perform operations on relational databases. Creating a "Statement" object in JDBC is an important step while dealing with a database in Java. A "Statement" object is used to execute SQL queries in Java Programming Language.
In this article, we will learn about how to create a Statement object in JDBC.
Prerequisites:
Before starting the concept of creating a "Statement" object in JDBC you should know about the basic understanding of the mentioned points below.
- Install Database: Have a relational database system installed and running on your system. Some common databases are used with JDBC including MySQL, PostgreSQL, Oracle, and SQLite. You also know some details about your database like the URL, username, and password to connect to it.
- JDBC Driver: After that JDBC driver corresponds to the database you are using So that you can import JAR files or add dependencies in the pom file of the project.
- About connectivity and knowledge of database: Understand the connection details of your database, Like URL format. The URL should specify the database type, host address, port number, and name of the database. Along with this, you should also know about SQL (Structured Query Language) to perform operations, such as creating, updating, delete the data into the database.
Step-by-Step Implementation to Create a Statement Object
In this article, we will discuss the basic functionality of "Statement" in JDBC (Java Database Connectivity) and its uses.
Use of "Statement" in Java Application:
- Helps in interaction with relational databases.
- Use to send SQL statements to a database.
Let's understand with the help of steps to create a "Statement" Object in Java
Step 1: Import JDBC Package
Before starting with code need to import "java.sql" and the specific JDBC driver package for the database.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
Step 2: Register JDBC Driver
Next, registered the JDBC driver. In this example, we are using MySQL database so, added MySQL JDBC driver in the main method of the class.
Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class StatementExample {
public static void main(String[] args) {
//Register JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
}
}
Step 3: Establish a Database Connection
Now we need to establish the connection between the database using the "DriverManager.getConnection" method. By adding the URL, username, and password of your database.
Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class StatementExample {
public static void main(String[] args) {
// Register JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish a Database Connection
String url = "jdbc:mysql://localhost:3306/databasename";
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
}
}
Step 4: Create a Statement Object
Once the connection is established now create a "Statement" object with the help of the "createStatement" method of the "Connection" interface in Java.
Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class StatementExample {
public static void main(String[] args) {
try {
// Register JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish a Database Connection
String url = "jdbc:mysql://localhost:3306/databasename";
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
// Create a Statement Object
Statement statement = connection.createStatement();
// Use the "Statement" object to execute SQL query
statement.executeUpdate("INSERT INTO tablename (columnname) VALUES ('datavalue')");
// Close the connection
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Conclusion
The "Statement" object in JDBC is used to interact with the database using Java. The process of creating an object in JDBC of connectivity for Java application. It allows us to execute SQL queries and perform operations in a database like create, update, and delete. For Java developers, it is important to have an understanding of this topic for working on Java application that involves database interactions. It also helps in managing transactions, committing, and rolling back the transaction when necessary.
Similar Reads
How to Retrieve Data from a ResultSet in JDBC? In Java database connectivity, the ResultSet is an interface, and it has a lot of built-in methods for handling the results of SQL queries. In this article, we will learn to retrieve data from a ResultSet in JDBC. ResultSet in InterfaceResultSet interface represents the result set of a database quer
4 min read
How to Handle SQLException in JDBC? Java Database Connectivity (JDBC) serves as the backbone for Java applications when interacting with databases. While establishing connections and executing queries, we developers often encounter SQLExceptions, which are inevitable in the real world. Handling those exceptions is crucial in the devel
4 min read
How to Handle SQL Injection in JDBC using PreparedStatement? In this article, we can learn how to prevent SQL injection in JDBC by using Java programming. Basically, SQL injection is a type of security vulnerability It occurs when hackers or attackers can change or manipulate the SQL query, and this can lead to unauthorized access to resources and also perfor
4 min read
How to Execute a SQL Query with Named Parameters in JDBC? Executing the SQL (Structured Query Language) query with named parameters in JDBC (Java Database Connectivity) is the fundamental feature of the database interaction in Java. Named parameters provide convenient and it provides a secure way to pass the values into the SQL queries without using concat
4 min read
How to Get the Insert ID in JDBC? In Java, for getting the insert ID in JDBC, we have built-in methods like getGeneratedKeys(). This method can be able to get the inserted ID in JDBC. Here, in this article, we have used PreparedStatement for insertion purposes and the SQL Injection attack free also. After performing an insertion ope
3 min read