Home » Core Java » sql » Update a table row example

About Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Update a table row example

In this example we shall show you how to update a Table row. To update a Table row one should perform the following steps:

  • Load the JDBC driver, using the forName(String className) API method of the Class. In this example we use the MySQL JDBC driver.
  • Create a Connection to the database. Invoke the getConnection(String url, String user, String password) API method of the DriverManager to create the connection.
  • Create a Statement, using the createStatement() API method of the Connection.
  • Invoke the executeUpdate(String sql) API method to update a specific column. It returns the number of rows updated.
  • For updates that are executed frequently we should use the PreparedStatement, using the prepareStatement(String sql) API method of the Connection. For example, to update 10 rows invoke the setString(int parameterIndex, String x) of the PreparedStatement, setting the values to the first and second parameter of each one of the 10 rows. Then invoke the executeUpdate() API method to execute the update,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class UpdateRowsExample {
 
  public static void main(String[] args) {

    Connection connection = null;
    try {

  // Load the MySQL JDBC driver

  String driverName = "com.mysql.jdbc.Driver";

  Class.forName(driverName);


  // Create a connection to the database

  String serverName = "localhost";

  String schema = "test";

  String url = "jdbc:mysql://" + serverName +  "/" + schema;

  String username = "username";

  String password = "password";

  connection = DriverManager.getConnection(url, username, password);

  

  System.out.println("Successfully Connected to the database!");

  
    } catch (ClassNotFoundException e) {

  System.out.println("Could not find the database driver " + e.getMessage());
    } catch (SQLException e) {

  System.out.println("Could not connect to the database " + e.getMessage());
    }

    try {


/* 

  * For updates that are not executed frequently we should use the statement API.

  * updateCount contains the number of updated rows

  */

Statement statement = connection.createStatement();

int updateCount = statement.executeUpdate("UPDATE test_table SET test_col='new_test_value' WHERE test_col = 'test_value'");



System.out.println("Updated test_value successfully : " + updateCount );


/* 

  * For updates that are executed frequently we should 

  * use the prepared statement API.

  */

PreparedStatement preparedStatement = connection.prepareStatement("UPDATE test_table SET test_col=? WHERE test_col = ?");


// update 10 rows

for (int i=0; i<10; i++) {

    preparedStatement.setString(1, "new_test_value_"+i);

    preparedStatement.setString(2, "test_value_"+i);

    

    // updateCount contains the number of updated rows

    updateCount = preparedStatement.executeUpdate();

    

    System.out.println("Updated test_value_" + i +" successfully : " + updateCount );

}

    } catch (SQLException e) {

  System.out.println("Could not update data to the database " + e.getMessage());
    }

  }
}

Example Output:

Successfully Connected to the database!
Updated test_value successfully : 1
Updated test_value_0 successfully : 1
Updated test_value_1 successfully : 1
Updated test_value_2 successfully : 1
Updated test_value_3 successfully : 1
Updated test_value_4 successfully : 1
Updated test_value_5 successfully : 1
Updated test_value_6 successfully : 1
Updated test_value_7 successfully : 1
Updated test_value_8 successfully : 1
Updated test_value_9 successfully : 1

 
This was an example of how to update a Table row in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments