Java

Java mysql Crud Project Using Intellij Idea

This JDBC will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE in IntelliJ IDEA using Mysql Database. IntelliJ IDEA is a very famous editor to writing the Java codes. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.

We will learn how to INSERT, SELECT, UPDATE and DELETE in database by writing code to manage the products table in the database named rbcompanyemployee table consist of following columns employee name,salary,mobile.

Feature of projects

The system shall be able to record Employee details : employee name,salary,mobile.

The system shall be able to retrieve the Employee details : employee name,salary,mobile.

Then system shall be able to Edit and Delete the Employee details : employee name,salary,mobile..

Learn how to make this System Step by step

Step 1: Download JDK  Click here  follow the steps and install it. Step 2: Download an appropriate jdbc driver Click here in order to connect jdbc and mysql. Establish the database connection

 Connection con;
 PreparedStatement pst;

     public void connect()
     {
         try {
             Class.forName("com.mysql.jdbc.Driver");
             con = DriverManager.getConnection("jdbc:mysql://localhost/rbcompany", "root","");
             System.out.println("Successs");
         }
         catch (ClassNotFoundException ex)
         {
             ex.printStackTrace();

         }
         catch (SQLException ex)
         {
             ex.printStackTrace();
         }
     }

Add Records

you can use the following code snippet to add the records in to database. paste the code inside the save button.

  String empname,salary,mobile;
              empname = txtName.getText();
              salary = txtSalary.getText();
              mobile = txtMobile.getText();

                try {
                    pst = con.prepareStatement("insert into employee(empname,salary,mobile)values(?,?,?)");
                    pst.setString(1, empname);
                    pst.setString(2, salary);
                    pst.setString(3, mobile);
                    pst.executeUpdate();
                    JOptionPane.showMessageDialog(null, "Record Addedddd!!!!!");
                    table_load();
                    txtName.setText("");
                    txtSalary.setText("");
                    txtMobile.setText("");
                    txtName.requestFocus();
                }

                catch (SQLException e1)
                {

                    e1.printStackTrace();
                }

            }

View Records

you can use the following code snippet to view the records from the database. paste the code . create the function table_load() and paste the following code.

void table_load()
    {
        try
        {
            pst = con.prepareStatement("select * from products");
            ResultSet rs = pst.executeQuery();
            table_1.setModel(DbUtils.resultSetToTableModel(rs));
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

Search Records

Enter the Employee id on textfield relavent Employee information will be displayed on the relavent textfield.

     try {

            String empid = txtid.getText();

            pst = con.prepareStatement("select empname,salary,mobile from employee where id = ?");
            pst.setString(1, empid);
            ResultSet rs = pst.executeQuery();

            if(rs.next()==true)
               {
                 String empname = rs.getString(1);
                 String emsalary = rs.getString(2);
                 String emmobile = rs.getString(3);

                 txtName.setText(empname);
                 txtSalary.setText(emsalary);
                 txtMobile.setText(emmobile);

                 }
               else
                 {
                  txtName.setText("");
                  txtSalary.setText("");
                  txtMobile.setText("");
                  JOptionPane.showMessageDialog(null,"Invalid Employee No");

                }
                }
                catch (SQLException ex) 
                {
                       ex.printStackTrace();
                }
            }

Edit

you can use the following code snippet to Edit the records.

String empid,empname,salary,mobile;
                empname = txtName.getText();
                salary = txtSalary.getText();
                mobile = txtMobile.getText();
                empid = txtid.getText();
                
      try {
            pst = con.prepareStatement("update employee set empname = ?,salary = ?,mobile = ? where id = ?");
            pst.setString(1, empname);
            pst.setString(2, salary);
            pst.setString(3, mobile);
            pst.setString(4, empid);

            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Record Updateee!!!!!");
            table_load();
            txtName.setText("");
            txtSalary.setText("");
            txtMobile.setText("");
            txtName.requestFocus();
           }

      catch (SQLException e1) 
         {  
             e1.printStackTrace();
         }
      }

Delete

you can use the following code snippet to Delete the records.

Paste code inside the Delete button.

      String empid;
      empid = txtid.getText();

     try {
           pst = con.prepareStatement("delete from employee  where id = ?");

           pst.setString(1, empid);

           pst.executeUpdate();
           JOptionPane.showMessageDialog(null, "Record Deleteeeeee!!!!!");
           table_load();
           txtName.setText("");
           txtSalary.setText("");
           txtMobile.setText("");
           txtName.requestFocus();
        }

       catch (SQLException e1) 
          {

               e1.printStackTrace();
          }
       }

i have attached the video link below. which will do this tutorials step by step.

 

admin

Recent Posts

How to Create Functional Calculator in React

Introduction to React Calculator Creating a functional calculator in React is an excellent way to…

9 hours ago

Inventory Management System Angular Step by Step

This article explain how to make a Inventory Management App in Angular.this app explain the…

2 days ago

Creating Java Swing Login Application with Validation

Introduction to Java Swing Java Swing is a versatile toolkit for building graphical user interfaces…

2 days ago

React Inventory Management System Project

Inventory Management App in React.this app explain the complete module of the Inventory sales management system in React…

4 days ago

Enhancing Inventory Management with POS System and PHP Queries

Introduction to Inventory Management with POS Systems Efficient inventory management is crucial for businesses looking…

5 days ago

Inventory management system using React

In today's fast-paced business environment, an efficient inventory management system is crucial for success. Companies…

6 days ago