Nearby lessons

29 of 37

JDBC - CRUD 1: Insert Records

📌 What You Will Learn
  • The difference between Select and Non-Select operations
  • executeQuery vs executeUpdate — when to use which
  • Reading results with the ResultSet methods
  • Complete CRUD programs: Insert, Select, Update, Delete
  • The ResultSet object and how it works

CRUD 1: Insert Records is a fundamental part of database programming with JDBC. This lesson explains CRUD Program 1 — INSERT (Non-Select) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

CRUD Program 1 — INSERT (Non-Select)

Example01
JCode Cell
1import java.sql.*;
2 
3public class InsertDemo {
4 public static void main(String[] args) throws Exception {
5 Connection con = DriverManager.getConnection(
6 "jdbc:mysql://localhost:3306/company", "root", "password");
7 Statement st = con.createStatement();
8 
9 int rows = st.executeUpdate(
10 "INSERT INTO employee VALUES (101, 'Rahul', 50000)");
11 System.out.println("Rows inserted: " + rows);
12 con.close();
13 }
14}
Output
Rows inserted: 1
📝 Key Takeaways
  • Select ops (SELECT) use executeQuery() → ResultSet.
  • Non-select ops (INSERT/UPDATE/DELETE) use executeUpdate() → row count.
  • rs.next() moves the cursor; getInt/getString/getDouble read values.
  • CRUD = Create(INSERT), Retrieve(SELECT), Update, Delete.
  • Always close the connection after work.
  • Use PreparedStatement for real applications (SQL injection safety).

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1