In Focus

Insert, Update, And Delete Records In Java

This blog demonstrates the CRUD operations in Java using the JDBC (Java Database Connectivity) into MySQL database using NetBeans IDE.

Introduction

MySQL is an RDBMS (Relational Database Management System) database. In this blog, we will learn using JDBC (Java Database Connectivity) to insert, update and delete data into MySQL database using the NetBeans IDE.

student.sql

  1. --Go to MySql database and click on Import tab click on Browse button and select this file(student.sql)  
  2. create database Studentinformation;  
  3. use Studentinformation;  
  4. create table student(roll int(3) primary keyname varchar(20), marks int(3));  

STEP 1

Open the NetBeans IDE. Click on File -> New Project, select the Java category; from Projects, select Java Application. Click on the "Next" button.

Insert, Update And Delete Record In Java 

STEP 2

Enter your project name and uncheck "Create Main Class" then click the "Finish" button.

STEP 3

Expand your project folder and right-click on the Libraries folder and click "Add Library…".

Select "JDBC MYSQL Driver" click on the "Add Library" button.

Insert, Update And Delete Record In Java Insert, Update And Delete Record In Java

STEP 4

Right-click on the project folder and go to New -> JFrame Form and enter your Class Name. Now, click the "Finish" button.

Insert, Update And Delete Record In Java 

STEP 5

Drag and drop three jLabel, three JTextField, and three button fields. Now, change the jLabel text and the button text like in the below image.

Insert, Update And Delete Record In Java 

STEP 6

Click on the "Source" button just beside the "Design" button.

  1. // Import these package   
  2. import java.sql.Connection;  
  3. import java.sql.DriverManager;  
  4. import java.sql.SQLException;  
  5. import java.sql.Statement;  
  6. import javax.swing.JOptionPane;  
  7.   
  8. // Create Referesh Method  
  9. public void Referesh() {  
  10.     jTextField1.setText("");  
  11.     jTextField2.setText("");  
  12.     jTextField3.setText("");  
  13. }  

Insert
 
Right-click on the "Insert" button. Go to Event -> action -> actionPerformed and add the below code.
  1. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  
  2.     try {  
  3.         Class.forName("com.mysql.jdbc.Driver");  
  4.         // establish connection  
  5.         Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Studentinformation""root""");  
  6.         Statement statement = con.createStatement();  
  7.         statement.executeUpdate("INSERT INTO student VALUES(" + jTextField1.getText() + ",'" + jTextField2.getText() + "'," + jTextField3.getText() + ")");  
  8.         JOptionPane.showMessageDialog(null"Record inserted...");  
  9.         statement.close();  
  10.         con.close();  
  11.         Referesh(); //Calling Referesh() method  
  12.     } catch (SQLException | ClassNotFoundException e) {  
  13.         JOptionPane.showMessageDialog(null, e);  
  14.     }  
  15. }  

Update
 
Right-click the "Update" button. Go to Event -> action -> actionPerformed and add the below code.
  1. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {  
  2.     try {  
  3.         Class.forName("com.mysql.jdbc.Driver");  
  4.         // establish connection  
  5.         Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Studentinformation""root""");  
  6.         Statement stmt = con.createStatement();  
  7.         stmt.execute("UPDATE student SET name='" + jTextField2.getText() + "',marks=" + jTextField3.getText() + " WHERE roll=" + jTextField1.getText() + "");  
  8.         JOptionPane.showMessageDialog(null"Record is updated...");  
  9.         stmt.close();  
  10.         con.close();  
  11.         Referesh(); //Calling Referesh() method  
  12.     } catch (SQLException | ClassNotFoundException se) {  
  13.         JOptionPane.showMessageDialog(null, se);  
  14.     }  
  15. }  

Delete 

Right-click the "Delete" button. Go to Event -> action -> actionPerformed and add the below code.
  1. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {  
  2.     try {  
  3.         Class.forName("com.mysql.jdbc.Driver");  
  4.         // establish connection  
  5.         Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Studentinformation""root""");  
  6.         Statement statement = con.createStatement();  
  7.         statement.executeUpdate("DELETE FROM student WHERE roll=" + jTextField1.getText() + "");  
  8.         JOptionPane.showMessageDialog(null"Record deleted...");  
  9.         statement.close();  
  10.         con.close();  
  11.         Referesh(); //Calling Referesh() method  
  12.     } catch (SQLException | ClassNotFoundException e) {  
  13.         JOptionPane.showMessageDialog(null, e);  
  14.     }  
  15. }  

STEP 6

Now, your project is completed. Run (F6) the project.

Insert, Update And Delete Record In Java 

In this blog, I covered MySQL database operations (Insert, Update and Delete) with JDBC using NetBeans IDE. If you have any problem, please comment.