Junior — Middle
What is your experience with the JDBC library for database interaction?
sobes.tech AI
Answer from AI
I have experience using JDBC for automating testing of database interactions. With JDBC, I wrote tests that executed SQL queries, verified data correctness, and checked the database state after application operations.
Main points:
- Created a connection to the database using
DriverManager. - Executed queries with
PreparedStatementto prevent SQL injection. - Processed results using
ResultSet. - Used transactions to rollback changes after tests to avoid affecting the database state.
Example code for data retrieval:
Connection conn = DriverManager.getConnection(dbUrl, user, password);
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE id = ?");
stmt.setInt(1, 10);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String username = rs.getString("username");
// checks and assertions
}
rs.close();
stmt.close();
conn.close();