CRUD Operations In Java With Source Code

In this article, We will discuss what is CRUD operations in Java. What is the importance of CRUD in Java? The project and details used in the system and How to make CRUD in Java? The other one is Steps on how to run the CRUD operations in Java.

What is CRUD Operations In Java?

CRUD In Java are the essential functions of persistent storage, and CRUD is an acronym for them. To retrieve and return data from a database, CRUD operations can employ forms or an interface view. The primary key in the input parameter is used to read the table records.

CRUD operations can use forms or an interface view to retrieve and return data from a database.

Procedures Function

  • CREATE – This is a form view to create a new record in the database
  • READ – Reads the table records based on the primary key within the input parameter.
  • UPDATE – Updates the content of the table based on the specified primary key for a record.
  • DELETE – Deletes a specified row in the table.

Importance of CRUD In Java

Facilitate operation security control: in the real world, some people can read but not modify the same data, such as your income, which you can read but not update or delete (at least without authorization). Some people can only read and write their own data and have no access to the data of others.

Most full stack applications require the ability to create, read, update, and delete things in a web application. For example, whether we’re constructing a storefront, a blog posting page, a todo list, or a social media clone, we’ll rapidly become stuck if we don’t use CRUD activities.

CRUD Operations Project Details and Technology

Project Name:CRUD Operations Project In Java With Source Code
AbstractCRUD Operation In Java is an acronym for CREATE, READ, UPDATE and DELETE which are basic functions of persistent storage.
Language/s Used:JAVA
JAVA version (Recommended):8
Database:MySQL
Type:Desktop Application
Developer:Source Code Hero
Updates:0
CRUD Operations Project In Java With Source Code – Project Information

How To Make CRUD Operations In Java?

Here are the codes on how to create CRUD operations in Java.

First, execute this code to make a database for CRUD Operation.

-- phpMyAdmin SQL Dump
-- version 5.0.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 25, 2021 at 07:40 AM
-- Server version: 10.4.14-MariaDB
-- PHP Version: 7.4.10

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `user1`
--

-- --------------------------------------------------------

--
-- Table structure for table `student`
--

CREATE TABLE `student` (
  `id` int(11) NOT NULL,
  `fname` varchar(20) NOT NULL,
  `lname` varchar(20) NOT NULL,
  `id_number` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `student`
--

INSERT INTO `student` (`id`, `fname`, `lname`, `id_number`) VALUES
(3, 'jude', 'suarez', '123'),
(4, 'adrian', 'mercurio', '122233');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `student`
--
ALTER TABLE `student`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `id_number` (`id_number`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `student`
--
ALTER TABLE `student`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

CREATE:

String fname = txtFname.getText().trim();
        String lname = txtLname.getText().trim();
        String id = txtId.getText().trim();
        
        if (!fname.isEmpty() && !lname.isEmpty() && !id.isEmpty()) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user1", "root", "");
                String sql = "select * from student where id_number='" + id + "'";
                st = con.createStatement();
                ResultSet rs = st.executeQuery(sql);
                if (!rs.first()) {
                    saveUser(fname, lname, id);
                    DefaultTableModel model = (DefaultTableModel) tblStudents.getModel();
                    Object[] row = new Object[4];
                    row[0] = fname;
                    row[1] = lname;
                    row[2] = id;
                    model.addRow(row);
                } else {
                    alert("Please provide a different id number", "Similar id found");
                }

                clear();
            } catch (SQLException | ClassNotFoundException ex) {
                Logger.getLogger(Crud.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    con.close();
                    st.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Crud.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } //        else if (!id.matches("^[0-9]{8}$")) {
        //            alert("please provide a valid id number", "Wrong id");
        //        } 
        else {
            alert("please fill in all the details");
        }
    }

READ:

int i = tblStudents.getSelectedRow();
        TableModel model = tblStudents.getModel();
        txtFname.setText(model.getValueAt(i, 0).toString());
        txtLname.setText(model.getValueAt(i, 1).toString());
        txtId.setText(model.getValueAt(i, 2).toString());

UPDATE:

String fname = txtFname.getText().trim();
        String lname = txtLname.getText().trim();
        String id = txtId.getText().trim();
        if (!fname.isEmpty() && !lname.isEmpty() && !id.isEmpty()) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user1", "root", "");
                String sql = "select * from student where id_number='" + id + "'";
                st = con.createStatement();
                ResultSet rs = st.executeQuery(sql);
                if (rs.first()) {
                    update(fname, lname, id);
                    DefaultTableModel model = (DefaultTableModel) tblStudents.getModel();
                    model.setRowCount(0);                   
                    fetch();
                    alert("Update was successful");
                    
                } else {
                    alert("There is no such student", "Update error");
                    clear();
                }

            } catch (ClassNotFoundException | SQLException ex) {
                Logger.getLogger(Crud.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            alert("There is nothing to update :(","No row selected");
        }

DELETE:

int i = tblStudents.getSelectedRow();
        if (i >= 0) {
            int option = JOptionPane.showConfirmDialog(rootPane,
                    "Are you sure you want to Delete?", "Delete confirmation", JOptionPane.YES_NO_OPTION);
            if (option == 0) {
                TableModel model = tblStudents.getModel();

                String id = model.getValueAt(i, 2).toString();
                if (tblStudents.getSelectedRows().length == 1) {
                    delete(id);
                    DefaultTableModel model1 = (DefaultTableModel) tblStudents.getModel();
                    model1.setRowCount(0);
                    fetch();
                    clear();
                }
            }
        } else {
            alert("Please select a row to delete");
        }

To start executing a Java Project, make sure that you have NetBeans IDE or any platform of Java installed on your computer.

Steps on how to run the CRUD Operations In Java With Source Code

Time needed: 5 minutes

These are the steps on how to run CRUD Operations In Java With Source Code.

  • Step 1: Download the source code.

    First, download the source code given below.
    crud download source code

  • Step 2: Extract file.

    Next, after you finished download the source code, extract the zip file.
    crud extract file

  • Step 3: Click open project.

    Next, open netbeans IDE and click open project and choose your download source code.
    crud open project

  • Step 4: Run the project.

    Next, right click the project folder and click run.
    crud run project

Downloadable Source Code Here!

Summary

This article is a way to enhance and develop our skills and logic ideas which is important in practicing the Java programming language which is a most well-known and most usable programming language in many companies.

As a result, this System is a basic project for all beginning and intermediate JAVA users who want to broaden their understanding of JAVA Desktop applications.

Finally, the entire JAVA project with open-source code is an absolute project and a valuable way for users to understand and explore more about it.

Inquiries

If you have any questions or suggestions about CRUD Operations In Java With Source Code, please feel free to leave a comment below.

Leave a Comment