Junior — Middle
What are the main functions of the API for database operations in Java?
sobes.tech AI
Answer from AI
An API for working with a database in Java, such as JDBC, performs the following main functions:
- Establishing a connection to the database.
- Executing SQL queries (SELECT, INSERT, UPDATE, DELETE).
- Retrieving and processing query results.
- Managing transactions (commit, rollback).
- Handling errors and exceptions related to the database.
Example of using JDBC to perform a simple query:
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString("username"));
}
rs.close();
stmt.close();
conn.close();