import java.sql.*; public class InsertRecord { public static void main(String[] args) throws SQLException { String url = "jdbc:mysql://localhost/testdb"; String username = "root"; String password = "password"; Connection conn = DriverManager.getConnection(url, username, password); String insertSql = "INSERT INTO employee (emp_id, emp_name, emp_dept) VALUES (?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(insertSql); pstmt.setInt(1, 101); pstmt.setString(2, "John Doe"); pstmt.setString(3, "IT"); int rows = pstmt.executeUpdate(); System.out.println(rows + " records inserted."); conn.close(); } }
import java.sql.*; public class UpdateRecord { public static void main(String[] args) throws SQLException { String url = "jdbc:mysql://localhost/testdb"; String username = "root"; String password = "password"; Connection conn = DriverManager.getConnection(url, username, password); String updateSql = "UPDATE employee SET emp_dept = ? WHERE emp_id = ?"; PreparedStatement pstmt = conn.prepareStatement(updateSql); pstmt.setString(1, "Marketing"); pstmt.setInt(2, 101); int rows = pstmt.executeUpdate(); System.out.println(rows + " records updated."); conn.close(); } }
import java.sql.*; public class DeleteRecord { public static void main(String[] args) throws SQLException { String url = "jdbc:mysql://localhost/testdb"; String username = "root"; String password = "password"; Connection conn = DriverManager.getConnection(url, username, password); String deleteSql = "DELETE FROM employee WHERE emp_id = ?"; PreparedStatement pstmt = conn.prepareStatement(deleteSql); pstmt.setInt(1, 101); int rows = pstmt.executeUpdate(); System.out.println(rows + " records deleted."); conn.close(); } }The java.util PreparedStatement execute method is part of the java.sql package library.