Open In App

How to handle connection leaks in JDBC?

In this article, we will learn how to handle connection leaks in JDBC. Handling connection leaks in JDBC involves ensuring that database connections are properly closed after they are no longer needed. The connection leaks occur when a connection is opened but not closed. Due to connection leaks, there is a chance of loss of data, so we need to close the database connection after the use of the database. We have different ways to handle the JDBC connection leaks namely using try-catch. It can handle database leaks. Another one is the finally block, it can be able to close the database connection whether we use it or not. And also, have other approaches also to handle JDBC connection leaks.

By following these approaches, we can be able to handle connection leaks in JDBC effectively.

Implementation to handle connection leaks in JDBC

Below, we have provided an example to handle connection leaks in JDBC.

Syntax:

The try-catch statement with finally block statement to handle connection leaks in JDBC

 try {
Connection con = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
if (con != null) {
con.close();
}
}

Program to handle connection leaks in JDBC

package geeksforgeeks;

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

public class RetrieveDataExample {

    public static void main(String[] args) {
        
        Connection con = null;
        ResultSet resultSet = null;
        try {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");

            if (con != null) {
                
                // Retrieve all data
                String selectQuery = "SELECT * FROM book";
                PreparedStatement selectStatement = con.prepareStatement(selectQuery);
                resultSet = selectStatement.executeQuery();

                System.out.println("The Available Data\n");

                while (resultSet.next()) {
                    int id = resultSet.getInt("id");

                    // Check for NULL values
                    String author_name = resultSet.getString("author");
                    if (resultSet.wasNull()) {
                        author_name = "N/A"; // Or any default value you prefer
                    }

                    String book_name = resultSet.getString("name");
                    if (resultSet.wasNull()) {
                        book_name = "N/A"; // Or any default value you prefer
                    }

                    String book_price = resultSet.getString("price");
                    if (resultSet.wasNull()) {
                        book_price = "N/A"; // Or any default value you prefer
                    }

                    System.out.println("ID: " + id + ", Author_Name: " + author_name + ", Book_Name: " + book_name + ", Book_Price " + book_price);
                }
            } else {
                System.out.println("Not Connected...");
            }

        } catch (SQLException e) {
            System.out.println("Exception is " + e.getMessage());
        }
        finally {
            // Close resources in reverse order
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

Output:

Below we can refer the output of the available data:

Output Data

Explanation of the above Program:

Article Tags :