paint-brush
Mastering Java JDBC CRUD Operations in Eclipse Using MySQLby@realnamehidden
1,025 reads
1,025 reads

Mastering Java JDBC CRUD Operations in Eclipse Using MySQL

by N JJuly 5th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In 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.
featured image - Mastering Java JDBC CRUD Operations in Eclipse Using MySQL
N J HackerNoon profile picture


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.


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".