Unit 5 Java
Unit 5 Java
5.Close Connection
Types of JDBC Drivers
JDBC (Java Database Connectivity) drivers are used to connect Java
applications to databases. There are four types of JDBC drivers:
1. Type-1: JDBC-ODBC Bridge Driver
o MySQL: com.mysql.cj.jdbc.Driver
o PostgreSQL: org.postgresql.Driver
o Oracle: oracle.jdbc.OracleDriver
1. Driver Interface (java.sql.Driver)
The Driver interface in JDBC is implemented by database vendors to
provide connectivity to their databases. It acts as a blueprint for all JDBC
drivers.
Key Points:
while (rs.next()) {
System.out.println(rs.getInt("id") + " - " +
rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Examples:-
import java.sql.*;
public class StatementExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String user = "root";
String password = "password";
try (Connection con = DriverManager.getConnection(url, user,
password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM
employees")) {
while (rs.next()) {
System.out.println(rs.getInt("id") + " - " +
rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Drawbacks of Statement:
Example:-
import java.sql.*;
public class PreparedStatementExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String user = "root";
String password = "password";
String sql = "SELECT * FROM employees WHERE department = ?";
try (Connection con = DriverManager.getConnection(url, user,
password);
PreparedStatement pstmt = con.prepareStatement(sql)) {
Example:-
import java.sql.*;
public class ResultSetExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String user = "root";
String password = "password";
try (Connection con = DriverManager.getConnection(url, user,
password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM
employees")) {
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
double salary = rs.getDouble("salary");
System.out.println("ID: " + id + ", Name: " + name + ", Salary: "
+ salary);
}
} catch (SQLException e) {
e.printStackTrace();
}
}}
Steps to Follow
1. Load and register the JDBC driver (Not required in Java 8+).
2. Establish a connection using DriverManager.getConnection().
3. Use PreparedStatement to insert and fetch data.
4. Use ResultSet to process query results.
5. Close the database connection.
Example:-
import java.sql.*;
public class JDBCPreparedStatementExample {
public static void main(String[] args) {
// Database connection details
String url = "jdbc:mysql://localhost:3306/mydatabase"; // Change
"mydatabase" to your DB name
ResultSet rs = pstmt.executeQuery();
System.out.println("\nEmployees in IT Department:");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String department = rs.getString("department");
double salary = rs.getDouble("salary");
System.out.println("ID: " + id + ", Name: " + name + ",
Salary: $" + salary);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4.Processing Results: