Updated March 30, 2023
Introduction to JDBC Statement
Basically, Java provides different types of interface to the user, in which the Java language provides JDBC statements. Normally a JDBC statement is used to execute the different queries of the database. In other words, we can say that a JDBC statement is a bunch of ResultSet, and we can use it as a method to get the desired object of the ResultSet. In a JDBC statement, first, we need to connect with the database; after successfully making the connection, we can execute the different JDBC statements with the database, such as CallableStatement and PreparedStatement as per user requirement.
Different JDBC Statements
Given below are the different types of JDBC statements:
1. Creating Statement Object
Basically, creating a statement is used to execute the different SQL statements as per user requirements. In this statement, we establish the connection objects by using the createStatement() method.
Syntax:
Statement stmt_obj = null;
Try
{
stmt_obj = con_obj.createStatement();
. . . .
. . . .
}
catch (SQLException e_obj)
{
. . . . . . . . .
}
Explanation:
In the above syntax, we use the createStatement() method to establish the connection with the database. Then, we can perform the different SQL statements as per user requirements listed below.
- Boolean string SQL statement: This method, for the most part, returns a Boolean value of valid on the off chance that a ResultSet object can be recovered, assuming not, this by and large and returns bogus. This technique is by, and large used to execute SQL DDL explanations or now and again when the client needs to utilize genuinely powerful SQL.
- ExecuteUpdate string SQL statement: This method, by and large, returns the number of lines influenced by the SQL statement’s execution. It is likewise used to execute SQL statements for which the client or software engineer hopes to get various columns influenced.
- ExecuteQuery ResultSet string SQL statement: Basically, this method is used to return the ResultSet object as per the user requirement. Basically, this method is used when the user requires the specified result set.
- Closing Statement Object: Soon after, the client closes Connection objects to save information base assets, and this is the very explanation for which the client or the software engineer should close the Statement object.
To close this, a basic call to the close () method will do the greater part of the work. Kindly note that if the client shuts the connection object first, then it will close the statement object too. Thus, item in request to guarantee legitimate cleanup, the client should expressly close the statement object in every case.
Syntax:
{
stmt_obj.close()
}
Explanation:
- In the above syntax, we used the close () method to close the connection. After the connection setup, we perform the different SQL statements, and at the end of the SQL statement, we must close the connection.
2. Prepared Statement
This statement is used to recompile the SQL statement. So that recompile statement we can execute multiple times as per user requirement. Basically, the prepared statement uses the SQL statement.
Let’s see how we can implement the prepared statement as follows.
Basically, there is a three-way to execute the PreparedStatement as follows:
- Execute(): Basically, this is used to execute the static SQL statement that is available in the prepared statement object as well as it also returns the Boolean values.
- executeQuery(): It is used to return the ResultSet from the current prepared statement that is available.
- executeUpdate(): Returns the number of columns influenced by the DML explanations like INSERT, DELETE, and more that is available in the current Prepared Statement.
Syntax:
preparedStatement p_stmt_obj = null;
try
{
String SQL_Stmt = "update stude set colm name= required value where colm name= required value";
P_stmt_obj = con_obj.prepareStatement(SQL_Stmt);
. . . . .
}
catch(SQLException e_obj)
)
{
}
Explanation:
- In the above syntax, we use preparedStatement to execute the DML statement such as insert, delete, etc., as per user requirement as shown in the above syntax.
3. Callable Statement
It is utilized to put away techniques which are a gathering of articulations that we aggregate in the information base for some undertaking, they are helpful when we are managing numerous tables with complex situations, and as opposed to sending various questions to the data set, we can send the necessary information to the put-away method and lower the rationale executed in the data set worker itself. The Callable statement interface given by JDBC API helps in executing the put-away methodology.
Syntax:
callableStatement call_stmt = null;
try
{
String SQL_Call_Stmt = "{call specified condition (?, ?)}";
Call_stmt = con_obj.prepareCall(SQL_Call_Stmt)
}
catch (SQLException e_obj)
{
. . .
}
Explanation:
- In the above syntax, we used a stored procedure representing SQL statements, as shown in the above syntax.
Example of JDBC Statement
Given below is the example of DBC statement:
Code:
import java.sql.*;
class demo_create {
public static void main(String[] args)
{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con_obj = DriverManager.getConnection(
"jdbc:mysql://localhost", "root", "root");
Statement c_stmt = con_obj.createStatement();
String sql_stmt = "select * from stud";
ResultSet r_stmt = c_stmt.executeQuery(sql_stmt);
while (r_stmt.next()) {
System.out.println(
"Stud_Name: " + r_stmt.getString("name"));
System.out.println(
"Dept:" + r_stmt.getString("dept"));
}
}
catch (SQLException e_obj) {
System.out.println(e_obj);
}
}
}
Explanation:
- In the above example, we try to implement a create statement in JDBC. In the above example, first, we establish a connection with the database by using the Class.forName statement as shown.
- After connection, we can fetch the records from the database. Here we try to display the name of students and departments of students. We illustrated the final output or result of the above program using the following screenshot as follows.
Output:
Similarly, we can execute all statements.
Conclusion
From the above article, we have seen different types of statements with their basic syntax, and we also saw different examples of JDBC statements. From this article, we saw how and when we use the JDBC statement.
Recommended Articles
This is a guide to JDBC Statement. Here we discuss the introduction, different JDBC statements and example for better understanding. You may also have a look at the following articles to learn more –