Repaso
EspaƱol

What is JDBC?

Options

  1. A JavaScript library for building database dashboards
  2. The standard low-level Java API for running SQL (Correct option)
  3. A Java build tool that packages the applications
  4. A web framework that is used to build REST services

Answer

The standard low-level Java API to connect to relational databases and run SQL

Why

JDBC (Java Database Connectivity) lets Java talk to any relational database through a driver: open a Connection, create a statement, run SQL and read the ResultSet.

JPA/Hibernate and Spring's JdbcTemplate are built on top of JDBC.

Always use PreparedStatement with ? parameters and never concatenate user input into SQL (that is SQL injection).

String sql = "SELECT name FROM users WHERE email = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
    ps.setString(1, email);          // safe: value is bound, not pasted
    try (ResultSet rs = ps.executeQuery()) {
        if (rs.next()) System.out.println(rs.getString("name"));
    }
}

Remember

JDBC = Java to SQL database. Always PreparedStatement.

Key terms

Practice this question in Repaso