0% found this document useful (0 votes)
53 views10 pages

WT Unit-4 One Shot Notes Programs by Brevilearning YT

Uploaded by

shubham86757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views10 pages

WT Unit-4 One Shot Notes Programs by Brevilearning YT

Uploaded by

shubham86757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JDBC (Java Database Connectivity)

Workflow codes:

1. Establishing a Connection

Code:

Java:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class DatabaseConnection {

public static Connection getConnection() throws SQLException {

// Replace with your database's URL, username, and password

String url = "jdbc:mysql://localhost:3306/mydatabase";

String user = "myusername";

String password = "mypassword";

// Establishing the connection

return DriverManager.getConnection(url, user, password);

}
2. Executing SQL Queries with Prepared Statements

Code:

Java:

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class DataManipulation {

public void joinAndManipulateData() {

// SQL query with a join operation

String sql = "SELECT a.column1, b.column2 FROM tableA a JOIN tableB b ON a.id = b.a_id
WHERE a.column1 = ?";

try (Connection conn = DatabaseConnection.getConnection();

PreparedStatement pstmt = conn.prepareStatement(sql)) {

// Setting the parameter for the prepared statement

pstmt.setString(1, "someValue");

// Executing the query

try (ResultSet rs = pstmt.executeQuery()) {

while (rs.next()) {

// Retrieving data from the result set

String column1 = rs.getString("column1");

String column2 = rs.getString("column2");

// Process the results as needed


System.out.println("Column1: " + column1 + ", Column2: " + column2);

} catch (SQLException e) {

e.printStackTrace();

3. Transaction Processing

Code:

Java:

public class TransactionExample {

public void performTransaction() {

// SQL update statements

String updateTableA = "UPDATE tableA SET column1 = ? WHERE id = ?";

String updateTableB = "UPDATE tableB SET column2 = ? WHERE a_id = ?";

try (Connection conn = DatabaseConnection.getConnection()) {

// Start transaction

conn.setAutoCommit(false);

try (PreparedStatement pstmtA = conn.prepareStatement(updateTableA);

PreparedStatement pstmtB = conn.prepareStatement(updateTableB)) {


// Update tableA

pstmtA.setString(1, "newValue");

pstmtA.setInt(2, 1);

pstmtA.executeUpdate();

// Update tableB

pstmtB.setString(1, "newValue");

pstmtB.setInt(2, 1);

pstmtB.executeUpdate();

// Commit transaction

conn.commit();

} catch (SQLException e) {

// Rollback transaction if something goes wrong

conn.rollback();

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}
4. Using Stored Procedures

Code:

1. Creating a Stored Procedure in the Database

Sql:

CREATE PROCEDURE updateData(IN param1 VARCHAR(50), IN param2 INT)

BEGIN

UPDATE tableA SET column1 = param1 WHERE id = param2;

UPDATE tableB SET column2 = param1 WHERE a_id = param2;

END;

2. Calling the Stored Procedure from Java

java:

import java.sql.CallableStatement;

import java.sql.Connection;

import java.sql.SQLException;

public class StoredProcedureExample {

public void callStoredProcedure() {

// SQL call to the stored procedure

String sql = "{CALL updateData(?, ?)}";

try (Connection conn = DatabaseConnection.getConnection();


CallableStatement cstmt = conn.prepareCall(sql)) {

// Setting parameters for the stored procedure

cstmt.setString(1, "newValue");

cstmt.setInt(2, 1);

// Executing the stored procedure

Cstmt.execute();

Catch(SQLException e){

E.printStackTrace();

You might also like