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.
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();
}
}
}
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();
}
}
}
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".