Java Database Connectivity (JDBC) is a standard Java API used for database connectivity and access. It provides a mechanism to access data from a variety of databases using uniform methods. Mastering JDBC is crucial for any Java developer, as it bridges the gap between Java applications and databases. Let's dive into some practical examples of using JDBC.

Before we start, ensure you have the correct database driver (e.g., MySQL Connector/J, Oracle JDBC Driver) added to your project's classpath.

Establishing a Database Connection
Connecting to a database is the first step in interacting with a data source. It's accomplished using the DriverManager or DataSource class. Here's how you can connect to a MySQL database using both methods:

Using DriverManager
DriverManager provides a convenient way to load a driver and obtain a connection to the database.

```java String url = "jdbc:mysql://localhost:3306/mydatabase"; Properties props = new Properties(); props.setProperty("user", "myuser"); props.setProperty("password", "mypassword"); Connection conn = DriverManager.getConnection(url, props); ```
Using DataSource
DataSource is part of the Java EE/Jakarta EE specification and provides a pooled connection interface. Here's an example using a MySQL DataSource:
```java MySQLDataSource dataSource = new MySQLDataSource(); dataSource.setURL("jdbc:mysql://localhost:3306/mydatabase"); dataSource.setUser("myuser"); dataSource.setPassword("mypassword"); DataSource myDataSource = dataSource; Connection conn = myDataSource.getConnection(); ```
Executing SQL Queries

Once connected, you can execute SQL statements using the Connection object's createStatement() and prepareStatement() methods.
Executing Simple SELECT Statements
To execute a simple SELECT query, use the createStatement() method and the executeQuery() method:

```java Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM MyTable"); while (rs.next()) { System.out.println(rs.getString("column_name")); } ```
Executing PreparedStatements
PreparedStatements are used for parameterized queries and are more efficient and secure.




















Here's an example of using a PreparedStatement to fetch user data by ID:
Executing DML Queries (INSERT, UPDATE, DELETE)
ForINSERT, UPDATE, and DELETE operations, use the executeUpdate() method of the Statement or PreparedStatement objects.
To insert a new user record:
```java String sql = "INSERT INTO Users (username, email) VALUES (?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "newuser"); pstmt.setString(2, "newuser@example.com"); pstmt.executeUpdate(); ```
To update an existing user's email:
```java PreparedStatement pstmt = conn.prepareStatement("UPDATE Users SET email = ? WHERE user_id = ?"); pstmt.setString(1, "updated_email@example.com"); pstmt.setInt(2, 101); pstmt.executeUpdate(); ```
Mastering JDBC allows you to interact with databases securely and efficiently. Continuously learning and practicing JDBC examples will make you proficient in working with databases in Java.
Happy coding! Keep exploring the world of JDBC to unlock its full potential in your applications.