Sobes.tech
Junior — Middle

What methods do you usually use to interact with relational databases?

sobes.tech AI

Answer from AI

To interact with relational databases in Java, the following methods are commonly used:

  • JDBC (Java Database Connectivity) — a low-level API for executing SQL queries directly.

  • ORM frameworks (e.g., Hibernate, JPA) — allow working with the database through objects, simplifying the mapping between tables and classes.

  • Spring JDBC Template — simplifies working with JDBC by handling exceptions and resource management.

Example of a simple query using JDBC:

Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE id = ?");
stmt.setInt(1, 10);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
    System.out.println(rs.getString("name"));
}
rs.close();
stmt.close();
conn.close();

The choice of method depends on the complexity of the project and performance requirements.

What methods do you usually use to interact with… - sobes.tech