Java JDBC CRUD Operations
In this page:
Create Operation (INSERT)
To add new records safely, avoid building SQL through raw string concatenation, which opens the door to SQL injection. PreparedStatements compile the query structure once and bind raw input values as parameters, keeping user-supplied data from ever being interpreted as SQL.
Example: Create Operation (INSERT)
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> table = new ArrayList<>();
String sql = "INSERT INTO users (name) VALUES (?)"; // bound parameter, not concatenated
String name = "Riya";
table.add(name); // simulated insert
System.out.println("Executed: " + sql + " with [" + name + "]");
}
}
Login to try C/C++/Java/PHP code in the editor
Update Operation (UPDATE)
UPDATE statements modify existing rows, with the columns to change and the WHERE clause both passed as bound PreparedStatement parameters rather than concatenated strings. Always check the executeUpdate() return value afterward, since it tells you how many rows actually matched and were changed.
Example: Update Operation (UPDATE)
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> table = new ArrayList<>(List.of("Riya"));
String sql = "UPDATE users SET name = ? WHERE name = ?";
int rowsChanged = table.contains("Riya") ? 1 : 0;
table.set(0, "Riya Sharma");
System.out.println("Executed: " + sql + " -- rows changed: " + rowsChanged);
}
}
Login to try C/C++/Java/PHP code in the editor
Batch SQL Operations
Batch processing speeds up bulk data tasks by bundling many individual INSERT, UPDATE, or DELETE statements together and sending them to the database in a single network round trip via executeBatch(), instead of paying that round-trip cost once per row.
Example: Batch SQL Operations
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> batch = List.of(
"INSERT INTO logs VALUES ('a')",
"INSERT INTO logs VALUES ('b')",
"INSERT INTO logs VALUES ('c')"
);
System.out.println("Sending " + batch.size() + " statements in a single round trip via executeBatch()");
}
}
Login to try C/C++/Java/PHP code in the editor
Read Operation (SELECT)
Reading data back out of the database is done with a SELECT query executed through a PreparedStatement. The results come back as a ResultSet, which you iterate row by row using next(), pulling out each column with its corresponding typed getter method.
Example: Read Operation (SELECT)
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> table = List.of("Riya", "Aman");
String sql = "SELECT name FROM users";
for (String row : table) { // simulates iterating a ResultSet with next()
System.out.println(row);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Delete Operation (DELETE)
Deleting rows uses a DELETE statement, again through a PreparedStatement so the row identifier is passed as a bound parameter rather than concatenated into the SQL string. executeUpdate() returns the number of rows actually removed, which is worth checking to confirm the delete matched what you expected.
Example: Delete Operation (DELETE)
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> table = new ArrayList<>(List.of("Riya", "Aman"));
String sql = "DELETE FROM users WHERE name = ?";
String target = "Aman";
boolean removed = table.remove(target);
System.out.println("Executed: " + sql + " -- rows removed: " + (removed ? 1 : 0));
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: