JDBC Video tutorial - How to update Data in a MySQL Database?

This video tutorial is for beginners in database programming using JDBC and teaches them to update the existing data in a MySQL database table.

JDBC Video tutorial - How to update Data in a MySQL Database?

JDBC Beginners tutorial for updating the data into MySQL database

The Java Database Connectivity API (JDBC) provides rich set of classes, interfaces for performing all types of database operations from the database. JDBC API allows the developers to write program, which connects to the MySQL database using pure type 4 JDBC driver.

In this video tutorial I am teaching you to write program in Java for updating the data in a MySQL database table. We have used the update statement to update the data into the table. JDBC allows the programmers to run any query against the connected database easily and it also helps to capture the exceptions that might occur at the runtime.

Steps for updating the data from Java program:

Step 1: Connect to the MySQL database
Following code connects to the MySQL database and returns the JDBC Connection object to the Java program.

con = DriverManager.getConnection(url + db, "root", "root");

Step 2: Write the update query - Following is the sql query which is used to update the data.

String sql = "update employee set name='Deepak1' where id=1 ";

Step 3: Execute the sql - The Statement object is used to execute the sql statement. Here is the code that executes the query:

st.executeUpdate(sql);

Step 4: Close the statement and connection object
Following code is used to close the Statement and Connection object:

st.close();
con.close();

Here is the video of the Tutorial:

Here is the full code of the Java program: package net.roseindia.jdbc;

import java.sql.*;

public class UpdateDataExample {

public static void main(String[] args) {

	System.out.println("Update value in Mysql database table!");
	Connection con = null;
	String url = "jdbc:mysql://localhost:3306/";
	String db = "jdbcexamples";
	String driver = "com.mysql.jdbc.Driver";
	try {
		Class.forName(driver);
		con = DriverManager.getConnection(url + db, "root", "root");
		try {
		Statement st = con.createStatement();
			
		String sql = "update employee set name='Deepak1' where id=1 ";
			
		st.executeUpdate(sql);
			 
		st.close();

		System.out.println("Data updated");
		} catch (SQLException s) {
		System.out.println("SQL statement is not executed!");
		}

		con.close();
	} catch (Exception e) {
		e.printStackTrace();
	}

}

}

To run the program right click in the java code window and then select Run As-> Java Application

After successful execution of the program record will be updated into the MySQL table.

Briefly following are the steps to update data in database:

  1. Connect to MySQL Database using JDBC
  2. Create the Statement Object
  3. Construct the sql
  4. Run the sql using the Statement object against database process results if any.

Download the source code of the JDBC Update example project in Eclipse project format.

So, in this tutorial on RoseIndia.net you have learned the example program for updating the data into MySQL database using the JDBC driver.

Check all JDBC Tutorials at http://www.roseindia.net/jdbc/index.shtml