Share on Google+Share on Google+

JDBC: Update Records Example

In this section, you will learn how to update records of the table using JDBC API.

JDBC: Update Records Example

In this section, you will learn how to update records of the table using JDBC API.

Update Records : Update record is most important operation of database. If you want to change one field value of a record then you will not delete that record and reinsert after correction so for such situation you can use update statement to update particular value of record.

Before writing update we need to create connection to the database. Here is some terms necessary to understand -

Connection: This  interface specifies connection with specific databases like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of this interface.

Class.forName(String driver): It loads the driver.

DriverManager  :  The DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property

getConnection(String url+dbName, String userName, String password): This method establishes a connection to specified database url.

he MySQL connection URL has the following format:

  • host :The host name where MySQL server is running. Default is - the IP address of localhost.
  • port : The port number where MySQL is listening for connection. Default is 3306.
  • dbName : The name of an existing database on MySQL server. If not specified, the connection starts no current database.
  • Property : The name of a supported connection properties. "userName" and "password" are 2 most important properties.

Statement: This interface executes the SQL statement and returns the result it produces.

createStatement(): It is a method of Connection interface which returns Statement object.

executeUpdate(String sql): This method of Statement interface execute sql statements(insert or delete or update) which takes the sql query of string type and returns int.

Example : In this example we are updating student table. We are setting value of name field 'Ron' where name value is Roxi.

package jdbc;

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

class UpdateRecords{
public static void main(String[] args){
System.out.println("Update Records Example...");
Connection con = null;
Statement statement = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "students";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {

// Connecting to the database
con = DriverManager.getConnection(url + dbName, userName, password);
try {
statement = con.createStatement();

// updating records
String sql = "UPDATE student SET name='Ron' WHERE name='Roxi'";
System.out.println("Updated successfully");

String sql1 = "SELECT * FROM student";
rs = statement.executeQuery(sql1);
while ( {
int roll = rs.getInt("roll_no");
String name = rs.getString("name");
String course = rs.getString("course");
System.out.println(roll + "\t" + name + "\t" + course);


} catch (SQLException e) {
System.out.println("Table doesn't exist.");
} catch (Exception e) {

Output :

Update Records Example...
Updated successfully
RollNo	Name	Course
1	Ron	MCA
2	Mandy	BCA
3	Rose	MCA



Posted on: September 25, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.