Skip to main content

Posts

Understanding the Difference Between Statement and PreparedStatement in Java JDBC

 When working with databases in Java, the choice between Statement and PreparedStatement can significantly impact performance, security, and overall application efficiency. While both are used to execute SQL queries, understanding when and how to use each can help you write more optimized and secure Java code. In this blog, we will dive deep into the differences between Statement and PreparedStatement in Java, why PreparedStatement is often preferred, and what impact it has on performance, especially for static SQL queries. 1. Overview of Statement and PreparedStatement Both Statement and PreparedStatement are interfaces provided by the java.sql package and are used to execute SQL commands in a relational database. However, their usage patterns and underlying mechanisms differ significantly. Statement : A Statement is used to execute simple, static SQL queries. Each time a query is executed using Statement , it is sent to the database, where it is parsed, compiled, optimized,

Hackrank test sample api consumption java code sample

import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class TransactionFetcher {     public static List<double[]> getTransaction(int locationId, String txnType) {         int page = 1;         List<double[]> transactions = new ArrayList<>();         while (true) {             try {                 String urlString = "https://jsonmock.hackerrank.com/api/transactions/search?" +                         "txnType=" + txnType + "&page=" + page;                 URL url = new URL(urlString);                 HttpURLConnection connection = (HttpURLConnection) url.openConnection();                 connection.setRequestMethod("GET");                 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));                 String inputLine;        

Learning How to Map One-to-Many Relationships in JPA Spring Boot with PostgreSQL

  Introduction In this blog post, we explore how to effectively map one-to-many relationships using Spring Boot and PostgreSQL. This relationship type is common in database design, where one entity (e.g., a post) can have multiple related entities (e.g., comments). We'll dive into the implementation details with code snippets and provide insights into best practices. Understanding One-to-Many Relationships A one-to-many relationship signifies that one entity instance can be associated with multiple instances of another entity. In our case: Post Entity : Represents a blog post with fields such as id , title , content , and a collection of comments . Comment Entity : Represents comments on posts, including fields like id , content , and a reference to the post it belongs to. Mapping with Spring Boot and PostgreSQL Let's examine how we define and manage this relationship in our Spring Boot application: Post Entity  @Entity @Getter @Setter @Builder @AllArgsConstructor @NoArgsCons

Choosing the Right AWS Service for Deploying Spring Boot Microservices

Deploying a Spring Boot microservice on AWS offers several options, each tailored to different needs and preferences. This guide explores various deployment options, helping you make an informed decision based on your requirements. 1. AWS Elastic Beanstalk Overview : AWS Elastic Beanstalk is a Platform as a Service (PaaS) that simplifies deploying and scaling web applications and services. It automatically handles the deployment, from capacity provisioning to load balancing and monitoring. Pros : Ease of Use : Simplifies deployment with minimal configuration. Integrated Monitoring : Built-in monitoring and logging. Managed Infrastructure : Automatically manages underlying infrastructure. Multi-Environment Support : Easy to manage multiple environments (development, staging, production). Cons : Limited Customization : Less control over the underlying infrastructure compared to EC2. Cost : Potentially higher costs for managed services. Best For : Quick deployment of applications with mi

Understanding the Lifecycle of a Spring Bean: Initialization and Destruction Explained

Spring Framework is renowned for its robust management of application components. One of the key aspects that make Spring so powerful is its comprehensive bean lifecycle management. In this blog, we'll explore the complete lifecycle of a Spring bean, including initialization and destruction methods. We'll delve into the sequence in which these methods are called, using various interfaces, annotations, and custom methods to illustrate the process.  Table of Contents Introduction to Spring Bean Lifecycle Spring Configuration and Bean Definition Bean Initialization Sequence Bean Destruction Sequence Complete Example with Output Conclusion Introduction to Spring Bean Lifecycle In Spring, a bean's lifecycle comprises various phases from instantiation, property population, and initialization to destruction. Understanding this lifecycle is crucial for developers to ensure proper resource management and application behavior. Spring Configuration and Bean Definition Before diving i

Tree Based Common problems and patterns

  Find the height of the tree. public class BinaryTreeHeight { public static int heightOfBinaryTree (TreeNode root) { if (root == null ) { return - 1 ; // Height of an empty tree is -1 } int leftHeight = heightOfBinaryTree(root.left); int rightHeight = heightOfBinaryTree(root.right); // Height of the tree is the maximum of left and right subtree heights plus 1 for the root return Math.max(leftHeight, rightHeight) + 1 ; } Find the Level of the Node. private static int findLevel (TreeNode root, TreeNode node, int level) { if (root == null ) { return - 1 ; // Node not found, return -1 } if (root == node) { return level; // Node found, return current level } // Check left subtree int leftLevel = findLevel(root.left, node, level + 1 ); if (leftLevel != - 1 ) { return leftLevel; // Node found in t