Mastering Java JDBC CRUD Operations in Eclipse Using MySQL

Written by realnamehidden | Published 2023/07/05
Tech Story Tags: java | jdbc | database | crud | mysql | java-programming | database-management | programming-tips

TLDRIn this article, we will see how can we perform [crud](https://hackernoon.com/optimizing-crud-operations-in-flutter-how-to-implement-http-requests-with-dio) operations with [MySQL] using JDBC. We will then cover how to create a table in MySQL database and how to insert, update, and delete the rows using the JDBC program.via the TL;DR App

In this article, we will see how can we perform crud operations with MySQL database using JDBC. We will then cover how to create a table in MySQL database and how to insert, update, and delete the rows using the JDBC program.

You can find a youtube video below with my step-by-step process.

https://www.youtube.com/watch?v=fUBoDErcDHA&embedable=true&transcript=true

Let’s dive straight into the code

Insert Class

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Insert {
    public static void main(String[] args) {
        //load the driver
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try (Connection con = DriverManager.getConnection("jdbc:mysql://@localhost:3306/new_schema", "root", "root"); Statement st = con.createStatement();) {
            String query = "INSERT INTO EMP(EID,ENAME) values(1,'Ram')";
            int count = st.executeUpdate(query);
            if (count == 0) {
                System.out.println("Record not inserted");
            } else {
                System.out.println("Record Inserted");
            }
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Delete Class

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Delete {
    public static void main(String[] args) {
        // load the driver
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try (Connection con = DriverManager.getConnection("jdbc:mysql://@localhost:3306/new_schema", "root", "root"); Statement st = con.createStatement();) {
            String query = "DELETE FROM EMP WHERE EID=1";
            int count = st.executeUpdate(query);
            if (count != 0) {
                System.out.println("Record Deleted");
            } else {
                System.out.println("Record Not Deleted");
            }
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Update Class

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Update {
    public static void main(String[] args) {
        // load the driver
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try (Connection con = DriverManager.getConnection("jdbc:mysql://@localhost:3306/new_schema", "root", "root"); Statement st = con.createStatement();) {
            String query = "UPDATE EMP SET ENAME='SAM' WHERE EID=1";
            int count = st.executeUpdate(query);
            if (count != 0) {
                System.out.println("Record UPDATED");
            } else {
                System.out.println("Record Not UPDATED");
            }
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/Select Class

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Select {
    public static void main(String[] args) {
        // load the driver
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try (Connection con = DriverManager.getConnection("jdbc:mysql://@localhost:3306/new_schema", "root", "root"); Statement st = con.createStatement();) {
            String query = "SELECT EID,ENAME FROM EMP";
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The lead image for this article was generated by HackerNoon's AI Image Generator via the prompt "database".


Written by realnamehidden | Student
Published by HackerNoon on 2023/07/05