Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, JDBC (Java Database Connectivity) is an API that allows Java applications to interact with databases. Two important components of JDBC are ‘ResultSet‘ and ‘PreparedStatement‘. Let’s dive deeper into each with detailed descriptions and examples.

PreparedStatement

Description

PreparedStatement is an interface in the java.sql package. It extends the Statement interface and provides a more efficient and secure way to execute SQL queries. The primary advantages of using PreparedStatement are:

  1. Pre-compilation: SQL queries are pre-compiled and stored in a PreparedStatement object. This can improve performance, especially when executing the same query multiple times.
  2. Prevention of SQL Injection: By using placeholders (?) in SQL statements and setting parameter values through methods, PreparedStatement helps prevent SQL injection attacks.
  3. Parameter Binding: Allows binding of parameters to placeholders, making it easier to handle SQL queries with varying parameters.

Example

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

public class PreparedStatementExample {
    public static void main(String[] args) {
        String jdbcURL = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "root";
        String password = "password";

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            // Establishing a connection
            connection = DriverManager.getConnection(jdbcURL, username, password);

            // SQL query with placeholders
            String sql = "SELECT * FROM users WHERE username = ? AND password = ?";

            // Creating PreparedStatement object
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "john_doe"); // Setting the first parameter
            preparedStatement.setString(2, "secret123"); // Setting the second parameter

            // Executing the query
            resultSet = preparedStatement.executeQuery();

            // Processing the ResultSet
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String usernameDB = resultSet.getString("username");
                String passwordDB = resultSet.getString("password");
                System.out.println("ID: " + id + ", Username: " + usernameDB + ", Password: " + passwordDB);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // Closing resources
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

				
			

ResultSet

Description

ResultSet‘ is an interface in the ‘java.sql‘ package that represents the result set of a database query. It is used to retrieve and manipulate the data returned by the SQL query. The ResultSet object maintains a cursor pointing to its current row of data. Initially, the cursor is positioned before the first row. The ‘next()‘ method moves the cursor to the next row.

Example

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

public class ResultSetExample {
    public static void main(String[] args) {
        String jdbcURL = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "root";
        String password = "password";

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            // Establishing a connection
            connection = DriverManager.getConnection(jdbcURL, username, password);

            // SQL query
            String sql = "SELECT * FROM users";

            // Creating PreparedStatement object
            preparedStatement = connection.prepareStatement(sql);

            // Executing the query
            resultSet = preparedStatement.executeQuery();

            // Processing the ResultSet
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String usernameDB = resultSet.getString("username");
                String passwordDB = resultSet.getString("password");
                System.out.println("ID: " + id + ", Username: " + usernameDB + ", Password: " + passwordDB);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // Closing resources
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

				
			

Key Methods

PreparedStatement Methods

  • setString(int parameterIndex, String x): Sets the designated parameter to the given Java String value.
  • setInt(int parameterIndex, int x): Sets the designated parameter to the given Java int value.
  • executeQuery(): Executes the SQL query in this PreparedStatement object and returns a ResultSet object.
  • executeUpdate(): Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, and returns the number of affected rows

ResultSet Methods

  • next(): Moves the cursor to the next row.
  • getInt(String columnLabel): Retrieves the value of the designated column as an int.
  • getString(String columnLabel): Retrieves the value of the designated column as a String.
  • close(): Releases this ResultSet object’s database and JDBC resources immediately.
  •  
Scroll to Top