Home » Java programming language

How to edit a record using JDBC in Java?

In this article, we are going to learn how to edit a record in MYSQL table using JDBC through java program? Submitted by Manu Jemini, on October 15, 2017

Prerequisite:

  1. How to create a table using JDBC in Java?
  2. How to insert records through JDBC in Java?
  3. How to display all records using JDBC in Java?
  4. How to display particular record by a field using JDBC in Java?
  5. How to delete a particular record using JDBC in Java?

Note: To edit a record from MYSQL table, you should know at least one field of that record.

First of all, we establish a connection between MYSQL and JAVA using Connection class, by creating an object named cn of this class.

Then we take input of a field, of which we want to edit record.

Then, we will prepare a MySQL query statement to display record from table with a where clause, to execute this query statement, we created an object named smt of Statement class, that will be used to execute query by using executeQuery() method. Now, we display record and put up a question "Which Field U Want to Edit?".

After taking field name, we will prepare an update query and call method named executeUpdate().

Database details:

  • Hostname: localhost
  • Port number: 3306
  • Username: root
  • Password: 123
  • Database name: demo
  • Table name: employees
  • Field: empid (employee id)

Java program to edit a record using JDBC

import java.io.DataInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class EditRecord {
	public static void main(String[] args) {
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			//serverhost = localhost, port=3306, username=root, password=123
			Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","123");

			Statement smt=cn.createStatement();

			DataInputStream KB=new DataInputStream(System.in);

			//input employee id by which we are going to edit record
			System.out.print("Enter Employee ID:");
			String eid=KB.readLine();

			//query to take data of a particular record from table employee
			String q="Select * from employees where empid='"+eid+"'";

			//to execute query
			ResultSet rs=smt.executeQuery(q);
			if(rs.next())
			{
				//to show the data
				System.out.println("Employee id:"+rs.getString(1));
				System.out.println("1.Employee Name:"+rs.getString(2));
				System.out.println("2.Employee DOB:"+rs.getString(3));
				System.out.println("3.Employee City:"+rs.getString(4));
				System.out.println("4.Employee Salary:"+rs.getString(5));
				System.out.println("5.Exit");
				System.out.println("Which Field U Want to Edit?");
				String ch=KB.readLine();
				String pat="";
				//cases to choose field you want to edit
				switch(ch)
				{ 
					case "1":
						System.out.print("Enter New Name:");
						String nn=KB.readLine();
						pat="empname='"+nn+"'";
						break;

					case "2":
						System.out.print("Enter New DOB:");
						String nd=KB.readLine();
						pat="dob='"+nd+"'";
						break;

					case "3":
						System.out.print("Enter New City:");
						String nc=KB.readLine();
						pat="city='"+nc+"'";
						break;

					case "4":
						System.out.print("Enter New Salary:");
						String ns=KB.readLine();
						pat="salary="+ns;
						break;

					case "5":
						System.out.println("Exit");
						break;

					default:
						System.out.println("Wrong Option");
						break;
				}
				if(!pat.equals(""))
				{
					//query to edit data of a particular record from table employee
					q="update employees set "+pat+" where empid='"+eid+"'";
					//to execute update
					smt.executeUpdate(q);
					System.out.println("Record Updated....");
				}
			}
			else
			{
				System.out.println("Record Not Found...");
			}
			cn.close();
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
}

Output (In console)

Enter Employee ID: 100
Employee id: 100
  1. Employee Name: Aman
  2. Employee DOB: 10/10/1990
  3. Employee City: Delhi
  4. Employee Salary: 35000
  5. Exit
Which Field U Want to Edit? 1 Enter New Name: Arun Record Updated....


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.