Junior — Middle
Have you had experience with Java Database Connectivity (JDBC)?
sobes.tech AI
Answer from AI
JDBC (Java Database Connectivity) is a standard API for interacting Java applications with databases. With JDBC, you can execute SQL queries, retrieve results, and manage transactions.
Example of a simple connection and query execution:
import java.sql.*;
public class JdbcExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb";
String user = "root";
String password = "password";
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println("User: " + rs.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
When working with JDBC, it is important to properly manage resources (close connections, handlers), use prepared statements to prevent SQL injection, and work efficiently with transactions.