What are the different types of JDBC Statements?

In the previous blog, we have seen what is JDBC and how to connect to the sql and execute simple queries. Here we will see the different types of JDBC Statements.

In JDBC ‘Statement’ interface is used to create the basic SQL statements. There are different types of JDBC statements present.

  1. Create Statement
  2. Prepared Statement
  3. Callable Statement

We will see each statement one by one in JDBC.

Before Checking all the statements we will create the DatabaseConnector class as below, code for the class can be copied from here…

Create Statement:

  • We can create this type of statement using the createStatement() method of the Connection interface.
  • This method creates a Statement object for sending SQL statements to the database. 
  • SQL statements without parameters are typically executed using Statement objects.
  • We can execute static sql queries at runtime using this type of Statement object.
  • This createStatement() method throws SQLException.

An example of the Create Statement is as follows, To copy the CreateStatementTest class click here…

After an object of the statement is created we can execute and get the results by using the executeQuery method which accepts sql query as a parameter and returns the ResultSet.

Output for the above example is as below:

Prepared Statement:

  • This is the dynamic statement that we can execute multiple times by passing different values each time.
  • We can write and pass SQL query which has ‘?’ instead of the actual parameter value, and we can replace this ‘?’ at runtime, which makes this statement more dynamic.
  • As shown in the example below we are selecting from the table for given roll number and name.
  • We are replacing roll number ( which is of type integer) and name (which is of type string) with setInt and setString methods respectively.
  • These methods accept two parameters, first is the position of the question mark, and second is a value that we need to replace with that question mark.
  • It’s always a better practice to use parameterized (which has ‘?’ and values are replaced at runtime) queries instead of static queries to avoid the SQL Injection issue.

Copy code for following class from here…

In the above example, an actual query which got executed after replacing the question mark values is as below.

Output for the above PreparedStatementTest class will be as below:

Callable Statement

  • Callable statements are used to call the Stored procedures
  • In MySQL, we have seen how to create custom Stored Procedures, and in the MySQL Error Handling blog we have created one procedure which is ‘perform_division’. 
  • We will call this procedure using the Callable statement.
  • Like Prepared statements callable statements also accept a parameterized query and values of question marks can be replaced later at runtime.

The stored procedure ‘perform_division’ is as follows:

The example for a Callable statement is as follows, copy the CallableStatementTest class from here…

Output for the above Callable statement class is as below:

-A blog by Shwetali Khambe

Related Posts