Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

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.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Ads

Online Training Classes

Share on Google+Share on Google+

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

Posted on: June 5, 2015 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads