Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Eclipse IDE: Exploring Prepared Statement and Create a Menu Driven Application in Java

In this lesson, you will learn.

  • Understanding PreparedStatement Interface
  • Methods of PreparedStatement
  • Examples

 

Understanding PreparedStatement Interface

  1. PreparedStatement is used to execute dynamic or parameterized SQL queries.
  2. PreparedStatement extends the Statement interface.
  3. Using this interface, you can pass the parameters to SQL query at run time.
  4. It is recommended to use PreparedStatement if executing a particular SQL query multiple times.

 

It gives better performance than the Statement interface. Because PreparedStatement is precompiled and the query plan is created only once irrespective of how many times you execute that query. This will save lots of time

 

Methods of PreparedStatement Interface

1. executeQuery()

  • Used for executing a SQL SELECT query.
  • Returns a ResultSet object.

Example

String query = "SELECT * FROM users WHERE username = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, "john_doe");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
    System.out.println("User ID: " + rs.getInt("id"));
}

 

2. executeUpdate()

  • Used for executing SQL INSERT, UPDATE, or DELETE statements.
  • Returns an int indicating the number of rows affected.

Example

String query = "UPDATE users SET email = ? WHERE username = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, "john_doe@example.com");
pstmt.setString(2, "john_doe");
int rowsUpdated = pstmt.executeUpdate();
System.out.println(rowsUpdated + " rows updated.");

 

3. execute()

  • It is used for executing any SQL statement.
  • Returns a boolean indicating whether the first result is a ResultSet.

Example

String query = "DELETE FROM users WHERE username = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, "john_doe");
boolean isResultSet = pstmt.execute();
if (!isResultSet) {
    int rowsDeleted = pstmt.getUpdateCount();
    System.out.println(rowsDeleted + " rows deleted.");
}

 

4. setInt(int parameterIndex, int x)

  • Sets the designated parameter to the given Java int value.

Example

String query = "INSERT INTO users (id, username) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setInt(1, 1);
pstmt.setString(2, "john_doe");
pstmt.executeUpdate();

 

5. setString(int parameterIndex, String x)

  • Sets the designated parameter to the given Java String value.

Example

String query = "INSERT INTO users (id, username) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setInt(1, 1);
pstmt.setString(2, "john_doe");
pstmt.executeUpdate();

 

6. clearParameters()

  • Clears the current parameter values immediately.

Example

String query = "INSERT INTO users (id, username) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setInt(1, 1);
pstmt.setString(2, "john_doe");
pstmt.executeUpdate();
pstmt.clearParameters();
pstmt.setInt(1, 2);
pstmt.setString(2, "jane_doe");
pstmt.executeUpdate();

 

6. addBatch()

  • Adds a set of parameters to this PreparedStatement object’s batch of commands.

Example

String query = "INSERT INTO users (id, username) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setInt(1, 1);
pstmt.setString(2, "john_doe");
pstmt.addBatch();
pstmt.setInt(1, 2);
pstmt.setString(2, "jane_doe");
pstmt.addBatch();
pstmt.executeBatch();

 

7. executeBatch()

  • Submits a batch of commands to the database for execution.

Example

String query = "INSERT INTO users (id, username) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setInt(1, 1);
pstmt.setString(2, "john_doe");
pstmt.addBatch();
pstmt.setInt(1, 2);
pstmt.setString(2, "jane_doe");
pstmt.addBatch();
int[] updateCounts = pstmt.executeBatch();

 

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review