Open In App

How to Create a Statement Object in JDBC ?

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.

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:

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.




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.




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.




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.


Article Tags :