Java / Java Database Connectivity (JDBC)
Executing SQL Queries with Statement
This tutorial will guide you through executing SQL queries using JDBC's Statement interface. You'll learn how to create a Statement object and use it to execute various types of S…
Section overview
5 resourcesCovers database interaction with JDBC API for connecting, querying, and updating databases.
1. Introduction
1.1 Brief Explanation of the Tutorial's Goal
This tutorial aims to guide you through executing SQL queries using JDBC's Statement interface. We'll cover the basics, including how to create a Statement object and use it to execute various types of SQL queries.
1.2 What Will You Learn
By the end of this tutorial, you will be able to:
- Understand the JDBC Statement interface functionality
- Create a Statement object
- Execute SQL queries using a Statement object
1.3 Prerequisites
You should have a basic understanding of Java and SQL before starting this tutorial.
2. Step-by-Step Guide
2.1 Creating a Statement Object
To execute SQL queries, we first need to create a Statement object. This can be done using the createStatement() method of the Connection object.
Statement statement = connection.createStatement();
2.2 Executing SQL Queries
With the statement object, you can now execute SQL queries. There are three methods you can use:
executeQuery(): For SELECT queries which return a ResultSet.executeUpdate(): For INSERT, UPDATE, DELETE, or DDL statements.execute(): For any type of SQL statement.
3. Code Examples
3.1 SELECT Query
// Create a Statement object
Statement statement = connection.createStatement();
// Execute SELECT query
ResultSet resultSet = statement.executeQuery("SELECT * FROM students");
// Fetch each row from the ResultSet
while (resultSet.next()) {
System.out.println(resultSet.getInt("id") + ", " + resultSet.getString("name"));
}
This code will print out all students in the "students" table.
3.2 INSERT Query
// Create a Statement object
Statement statement = connection.createStatement();
// Execute INSERT query
int rowsAffected = statement.executeUpdate("INSERT INTO students VALUES (1, 'John Doe')");
// Print out the number of inserted rows
System.out.println("Inserted " + rowsAffected + " row(s).");
This code will insert a new student into the "students" table and print out the number of inserted rows.
4. Summary
In this tutorial, we've learned how to create a Statement object using the JDBC's Statement interface and execute various types of SQL queries. This is a fundamental skill for interacting with databases in Java.
5. Practice Exercises
5.1 Exercise 1
Write a Java program to update a student's name in the "students" table.
5.2 Exercise 2
Write a Java program to delete a student from the "students" table.
5.3 Exercise 3
Write a Java program to select all students from the "students" table and print out their names.
For further practice, try to execute more complex SQL queries and handle the results in your Java program.
6. Solutions
// Exercise 1
Statement statement = connection.createStatement();
int rowsAffected = statement.executeUpdate("UPDATE students SET name = 'Jane Doe' WHERE id = 1");
System.out.println("Updated " + rowsAffected + " row(s).");
// Exercise 2
Statement statement = connection.createStatement();
int rowsAffected = statement.executeUpdate("DELETE FROM students WHERE id = 1");
System.out.println("Deleted " + rowsAffected + " row(s).");
// Exercise 3
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT name FROM students");
while (resultSet.next()) {
System.out.println(resultSet.getString("name"));
}
Remember to replace the SQL queries with your own ones, and ensure that your database connection works properly. Good luck!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article