How to Execute SQL Query in Java with JDBC Example
Connection to connect to the database, create a Statement or PreparedStatement, then call executeQuery(sql) for SELECT or executeUpdate(sql) for INSERT/UPDATE/DELETE.Examples
How to Think About It
Connection. Then create a Statement object to send SQL commands. Use executeQuery for queries that return data, and executeUpdate for commands that change data. Finally, process the results or check the update count.Algorithm
Code
import java.sql.*; public class ExecuteSQL { public static void main(String[] args) { String url = "jdbc:h2:mem:testdb"; String sql = "SELECT 1 AS number"; try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { while (rs.next()) { System.out.println("Number: " + rs.getInt("number")); } } catch (SQLException e) { e.printStackTrace(); } } }
Dry Run
Let's trace the example SELECT query through the code
Connect to database
Connection established to jdbc:h2:mem:testdb
Create statement
Statement object created from connection
Execute query
SQL 'SELECT 1 AS number' sent to database
Process result
ResultSet contains one row with number=1
Print output
Prints 'Number: 1'
| Step | Action | Value |
|---|---|---|
| 1 | Connect | jdbc:h2:mem:testdb |
| 2 | Create Statement | Statement object |
| 3 | Execute Query | SELECT 1 AS number |
| 4 | Read ResultSet | number=1 |
| 5 | Number: 1 |
Why This Works
Step 1: Connect to database
The DriverManager.getConnection method opens a connection to the database URL.
Step 2: Create statement
A Statement object lets you send SQL commands to the database.
Step 3: Execute query
executeQuery runs the SQL and returns a ResultSet for reading data.
Step 4: Process results
You loop through the ResultSet to get each row's data.
Alternative Approaches
import java.sql.*; public class ExecuteSQLPrepared { public static void main(String[] args) { String url = "jdbc:h2:mem:testdb"; String sql = "SELECT ? AS number"; try (Connection conn = DriverManager.getConnection(url); PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setInt(1, 1); try (ResultSet rs = pstmt.executeQuery()) { while (rs.next()) { System.out.println("Number: " + rs.getInt("number")); } } } catch (SQLException e) { e.printStackTrace(); } } }
import java.sql.*; public class ExecuteUpdateSQL { public static void main(String[] args) { String url = "jdbc:h2:mem:testdb"; String create = "CREATE TABLE test(id INT)"; String insert = "INSERT INTO test VALUES(1)"; try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement()) { stmt.execute(create); int rows = stmt.executeUpdate(insert); System.out.println("Rows inserted: " + rows); } catch (SQLException e) { e.printStackTrace(); } } }
Complexity: O(n) time, O(n) space
Time Complexity
The time depends on the number of rows returned or affected by the query, so it is O(n) where n is the number of rows processed.
Space Complexity
Space is O(n) for storing the ResultSet data in memory while processing rows.
Which Approach is Fastest?
Using PreparedStatement is slightly faster for repeated queries and safer, while Statement is simpler for one-off queries.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Statement.executeQuery | O(n) | O(n) | Simple SELECT queries |
| PreparedStatement.executeQuery | O(n) | O(n) | Parameterized and repeated queries |
| Statement.executeUpdate | O(1) | O(1) | Data modification commands |
Connection, Statement, and ResultSet to avoid resource leaks.executeQuery for SELECT or using executeUpdate incorrectly causes errors.