JDBC CRUD Query

Once you have successfully established a JDBC connection, you can execute SQL queries (SELECT, INSERT, UPDATE, DELETE) using Statement or PreparedStatement.

Fetch Data from Database (SELECT):

🔹 executeQuery() → Used for SELECT queries (returns ResultSet).

🔹 ResultSet → Stores the retrieved data from the database.

🔹 rs.next() → Moves to the next row in the result set.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCSelectExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb";
String user = "root";
String password = "password";

try (Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM employees")) {

while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String role = rs.getString("role");
System.out.println("ID: " + id + ", Name: " + name + ", Role: " + role);
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}

 Inserting Data using PreparedStatement:

🔹 executeUpdate() → Used for INSERT, UPDATE, DELETE (returns affected row count).

🔹 pstmt.setString(1, "John Doe") → Sets the first placeholder (?).

🔹 pstmt.setString(2, "Manager") → Sets the second placeholder (?).

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCInsertExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb";
String user = "root";
String password = "password";

String sql = "INSERT INTO employees (name, role) VALUES (?, ?)";

try (Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = con.prepareStatement(sql)) {

pstmt.setString(1, "John Doe"); // First parameter
pstmt.setString(2, "Manager"); // Second parameter

int rowsInserted = pstmt.executeUpdate(); // Execute query

if (rowsInserted > 0) {
System.out.println("✅ Data inserted successfully!");
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}

Updating Data using PreparedStatement:

String sql = "UPDATE employees SET role = ? WHERE id = ?";

try (PreparedStatement pstmt = con.prepareStatement(sql)) {
pstmt.setString(1, "Senior Developer");
pstmt.setInt(2, 101); // Employee ID

int rowsUpdated = pstmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("✅ Employee role updated successfully!");
}
}

Deleting Data using PreparedStatement:

String sql = "DELETE FROM employees WHERE id = ?";

try (PreparedStatement pstmt = con.prepareStatement(sql)) {
pstmt.setInt(1, 101); // Employee ID to delete

int rowsDeleted = pstmt.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("✅ Employee deleted successfully!");
}
}

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.