Updating a Row in a Database Table - Java JDBC

Java examples for JDBC:Table

Description

Updating a Row in a Database Table

Demo Code

import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;

public class Main {
  public void m() throws Exception {
    try {/*  w w  w. j ava 2  s  .  c  o m*/
      Connection connection = null;
      Statement stmt = connection.createStatement();

      // Prepare a statement to update a record
      String sql = "UPDATE my_table SET col_string='a new string' WHERE col_string = 'a string'";

      // Execute the insert statement
      int updateCount = stmt.executeUpdate(sql);
      // updateCount contains the number of updated rows
    } catch (Exception e) {
    }
  }
}

Related Tutorials