Posts

Showing posts with the label Java Database Connectivity

JDBC Batch Update in Java: Statement vs PreparedStatement with Advantages and Working

JDBC Batch Update in Java Batch Update in JDBC is a process where multiple SQL queries are grouped together and sent to the database server in a single request. Instead of executing SQL queries one by one, batch update improves performance and efficiency by reducing network traffic between the Java application and the database server . ✅ Why Use Batch Update? 🚀 Improves application performance 🌐 Reduces network calls to the database 📉 Minimizes database load 🧠 Efficient for bulk insert, update, and delete operations Real-Life Example: When inserting thousands of student records or updating employee salaries in bulk, batch update is the best approach. 🔹 Batch Update Using Statement Batch update can be performed using the Statement interface in the following way: Steps: Create a Statement object. Create a batch. Add SQL queries (similar or dissimilar) to the batch. Execute the batch. Important Methods: addBatch(String query) Adds an SQL query to the batch. This method is declared i...

JDBC Statement in Java: Creating Statements and Executing SQL Queries (Execute, ExecuteQuery, ExecuteUpdate)

JDBC Statement in Java In JDBC, a Statement is an interface present in the java.sql package. It is used to send SQL queries from a Java program to a database server. The implementation of the Statement interface is provided by the database vendor (such as MySQL, Oracle, PostgreSQL) inside the JDBC driver JAR file. Creating a JDBC Statement To create a JDBC Statement, a database connection is required. The Connection interface provides the createStatement() method. This method returns an object of a subclass of the Statement interface. Method Syntax: public Statement createStatement(); The returned Statement object is used to execute SQL queries on the database . Executing SQL Queries Using Statement The Statement interface provides different methods to execute SQL queries depending on the query type. 1️⃣ execute(String query) A generic method used to execute any type of SQL query. Can execute DDL, DML, and DQL queries. Examples : CREATE TABLE (DDL) INSERT, UPDATE, DELETE (DML) SELECT (...

How to Establish a Database Connection in Java Using JDBC (DriverManager Explained)

Establishing a Database Connection in JDBC (Java) To communicate with a database server from a Java application, the programmer must establish a database connection using JDBC (Java Database Connectivity). Role of DriverManager and Connection The DriverManager class is present in the java.sql package. It provides static factory methods to create and return an object of the Connection interface. The Connection interface represents an active connection between a Java program and a database server. 👉 The implementation of the Connection interface is provided by the database vendor (such as MySQL, Oracle, PostgreSQL) inside a JDBC driver JAR file (e.g., mysql-connector.jar). JDBC API vs JDBC Driver              Component.                                   Description java.sql package.                 Part of JDBC API (avai...

JDBC Tutorial in Java: Architecture, API, and Real-World Examples

JDBC (Java Database Connectivity) JDBC (Java Database Connectivity) is a standard Java API that allows Java applications to communicate with a database server. It provides a set of interfaces and classes used to connect to databases, execute SQL queries, and process results. JDBC is part of the Java Standard Edition and is available in the java.sql package, which is included in the rt.jar file. Since it is part of the Java built-in libraries, programmers can directly use JDBC by importing the required classes. What JDBC Does Establishes a connection between a Java program and a database Executes SQL queries (SELECT, INSERT, UPDATE, DELETE) Retrieves and processes data from the database Manages database transactions JDBC API Structure The JDBC API consists of interfaces defined in the java.sql package. These interfaces are implemented by database vendors (such as MySQL, Oracle, PostgreSQL) through JDBC drivers. The JDBC driver acts as a bridge between Java applications and the database...

Database Configuration in Java: Definition, Components, and Best Practices

📘 Database Configuration in Java Database Configuration refers to a set of credentials and settings used by an application to establish communication with a database server. These details allow a Java application to connect securely and perform database operations such as insert, update, delete, and retrieve data. 🔑 Components of Database Configuration A typical database configuration includes the following details: Database URL – Specifies the database location and type Port Number – Identifies the port on which the database server is running Username – Authorized user to access the database Password – Authentication credential for the user 📌 Note: Database credentials are unique for every database server and should be handled securely. 📂 Where Database Configuration Can Be Written Database configuration details can be stored in different places depending on the application design: Interface Properties file (.properties) web.xml file (for web applications) 🧩 Database Configurati...