Open In App

How to Create a Statement Object in JDBC ?

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads