Skip to main content

Posts

Showing posts from August, 2024

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;