How to Update, Insert or Delete Records In Java

Introduction

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

student.sql

--Go to MySql database and click on Import tab click on Browse button and select this file(student.sql)    
create database Studentinformation;    
use Studentinformation;    
create table student(roll int(3) primary key, name 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.

This is image title

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 the “JDBC MYSQL Driver” click on the “Add Library” button.

This is image title

STEP 4

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

This is image title

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.

This is image title

STEP 6

Click on the “Source” button just beside the “Design” button.

// Import these package   
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.SQLException;  
import java.sql.Statement;  
import javax.swing.JOptionPane;  
// Create Referesh Method  
public void Referesh() {  
    jTextField1.setText("");  
    jTextField2.setText("");  
    jTextField3.setText("");  
}  

Insert

Right-click on the “Insert” button. Go to Event -> action -> actionPerformed and add the below code**.**

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  
    try {  
        Class.forName("com.mysql.jdbc.Driver");  
        // establish connection  
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Studentinformation", "root", "");  
        Statement statement = con.createStatement();  
        statement.executeUpdate("INSERT INTO student VALUES(" + jTextField1.getText() + ",'" + jTextField2.getText() + "'," + jTextField3.getText() + ")");  
        JOptionPane.showMessageDialog(null, "Record inserted...");  
        statement.close();  
        con.close();  
        Referesh(); //Calling Referesh() method  
    } catch (SQLException | ClassNotFoundException e) {  
        JOptionPane.showMessageDialog(null, e);  
    }  
}  

Update

Right-click the “Update” button. Go to Event -> action -> actionPerformed and add the below code.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {  
    try {  
        Class.forName("com.mysql.jdbc.Driver");  
        // establish connection  
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Studentinformation", "root", "");  
        Statement stmt = con.createStatement();  
        stmt.execute("UPDATE student SET name='" + jTextField2.getText() + "',marks=" + jTextField3.getText() + " WHERE roll=" + jTextField1.getText() + "");  
        JOptionPane.showMessageDialog(null, "Record is updated...");  
        stmt.close();  
        con.close();  
        Referesh(); //Calling Referesh() method  
    } catch (SQLException | ClassNotFoundException se) {  
        JOptionPane.showMessageDialog(null, se);  
    }  
}  

**Delete **

Right-click the “Delete” button. Go to Event -> action -> actionPerformed and add the below code.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {  
    try {  
        Class.forName("com.mysql.jdbc.Driver");  
        // establish connection  
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Studentinformation", "root", "");  
        Statement statement = con.createStatement();  
        statement.executeUpdate("DELETE FROM student WHERE roll=" + jTextField1.getText() + "");  
        JOptionPane.showMessageDialog(null, "Record deleted...");  
        statement.close();  
        con.close();  
        Referesh(); //Calling Referesh() method  
    } catch (SQLException | ClassNotFoundException e) {  
        JOptionPane.showMessageDialog(null, e);  
    }  
}  

STEP 6

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

This is image title

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

#java #MySQL #JDBC #IDE #database

How to Update, Insert or Delete Records In Java
78.85 GEEK