Java JDBC CRUD Operations in Eclipse using MySql

 Java JDBC CRUD Operations in Eclipse using MySql 


For Explanation Watch Video :




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

Comments

Popular posts from this blog

Print Prime Numbers Hackerrank Solution - PL/SQL

how to store html form data in mysql database using hibernate