java_unit_4
java_unit_4
• wait() – Makes thread wait until another thread invokes notify() or notifyAll().
• notify() – Wakes up one waiting thread.
• notifyAll() – Wakes up all waiting threads.
1 Parikshit 20 CS
2 Mayur 21 IT
PIMPRI CHINCHWAD UNIVERSITY
Structured Query Language (SQL)
• Basic SQL Commands
▫ DDL (Data Definition Language) – Defines the structure of the database
CREATE, ALTER, DROP
▫ DML (Data Manipulation Language) – Manipulates the data in the table
INSERT, UPDATE, DELETE
▫ DQL (Data Query Language) – Query the data
SELECT
▫ TCL (Transaction Control Language) – Manage transactions.
COMMIT, ROLLBACK
▫ DCL (Data Control Language) – Manage access.
GRANT, REVOKE
PIMPRI CHINCHWAD UNIVERSITY
Java Database Connectivity (JDBC)
• JDBC (Java Database Connectivity) is an API (Application Programming
Interface) that enables Java applications to interact with relational databases.
Component Description
• Example:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:testdb");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "user", "password");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
2. Create a Connection
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
3. Create a Statement
Statement stmt = con.createStatement();
while (rs.next()) {
students.add(new Student(rs.getInt(1), rs.getString(2), rs.getInt(3)));
}
con.close();
} catch (Exception e) {
e.printStackTrace();
PIMPRI CHINCHWAD UNIVERSITY
Using DAO (Data Access Object) Pattern
• DAO Example: StudentDAO.java
public class StudentDAO {
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
students.add(new Student(rs.getInt(1), rs.getString(2), rs.getInt(3)));
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return students;
}
} PIMPRI CHINCHWAD UNIVERSITY
Using DAO (Data Access Object) Pattern
• DAO Example: Main.java