Posts

Showing posts with the label JDBC

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...