JDBC – Insert, Update and Delete example.

Hello Friends,

This is the simple example for executing the SQL statements to INSERT, UPDATE or DELETE any records from the database using JDBC. In the below code I have explained all the three operations. When we insert, update or delete any records from the database we are updating the database, hence we use the executeUpdate() function with the Statement.

Please take the note that whenever you provide the String/Text values for update then add the values in between ” ” or ‘ ‘. This is mandatory for all the Sting types else the DB will not recognize the values and will throw the error. Following is the code:

package com.JavaExamples;

import java.sql.*;

public class JDBCConnectionExample
{
	public static void main(String[] args)
	{
		System.out.println("MySQL & JDBC Connect Example.");
		Connection conn = null;
		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "code2java";// Database name
		String driver = "com.mysql.jdbc.Driver";
		String userName = "username";
		String password = "password";
		String dbtime = null;
		try
		{
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(url+dbName,userName,password);
			System.out.println("Database Connected");
			Statement stmt = conn.createStatement();

			//For inserting the data into database
			String insertQuery = "INSERT table_name(column_1,column_2,column_3,...) values(value_1,value_2,value_3,...)";
			stmt.executeUpdate(insertQuery);

			//For Updating the particular entity of the table
			String updateQuery = "UPDATE table_name SET column_name1=value_1,column_name2=value_2 WHERE column_name=some_value";
			stmt.executeUpdate(updateQuery);

			//For Deleting the specific row/s in the table
			String deleteQuery = "DELETE FROM table_name WHERE column_name=some_value";
			stmt.executeUpdate(deleteQuery);

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//This is used for Disconnecting..
			try
			{
				conn.close();
				System.out.println("Database Disconnected");
			}
			catch (SQLException e)
			{
				e.printStackTrace();
				System.out.println("Error while Disconnecting from database");
			}

		}
	}
}

Hope this will help you.
Regards,
Nikhil Naoghare.

Related Posts

  • MySql Installation

    Hello Friends,   This is one of my tutorials related to database. You all must be familiar with MySQL, but still for those who have not learned about this, for them I will recall it in short. MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases.The…

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

2 Comments

  1. You actually make it seem so easy along with your presentation however I to find this topic to be actually one thing which I think I might by no means understand. It kind of feels too complex and very extensive for me. I am looking forward in your subsequent put up, I’ll try to get the cling of it!

Leave a Reply

Your email address will not be published. Required fields are marked *

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