Selenium Database Management & SQL 2 — Questions and Answers
Question 1: In Selenium test automation, which Java class is commonly used to execute SQL queries against a JDBC data source?
- java.sql.Statement (Correct answer)
- java.sql.ResultSet
- java.sql.DriverManager
- java.sql.PreparedStatement
Correct answer: java.sql.Statement
java.sql.Statement is used to execute static SQL queries, while PreparedStatement is for parameterized queries.
Question 2: A Selenium test needs to verify that a newly created user record exists in the database. Which SQL clause is most appropriate to check for a specific email value?
- ORDER BY
- GROUP BY
- WHERE (Correct answer)
- HAVING
Correct answer: WHERE
The WHERE clause filters rows based on a condition, making it ideal for locating a specific record by a column value.
Question 3: What is the primary advantage of using PreparedStatement over Statement when writing Selenium database verification helpers?
- It auto-commits transactions
- It prevents SQL injection by parameterizing inputs (Correct answer)
- It caches query results automatically
- It supports multiple simultaneous connections
Correct answer: It prevents SQL injection by parameterizing inputs
PreparedStatement uses placeholders that are safely escaped, preventing SQL injection from test data inputs.
Question 4: After a Selenium UI test registers a new account, you run SELECT COUNT(*) FROM users WHERE email='test@test.com'. The result returns 0. What does this indicate?
- The database connection failed
- The registration did not persist to the database (Correct answer)
- The email column has a unique constraint
- The query syntax is incorrect
Correct answer: The registration did not persist to the database
A count of 0 means no matching row was found, indicating the UI registration action did not save data to the database.
Question 5: Which SQL statement should a Selenium test use to clean up test data inserted during a test run?
- TRUNCATE users
- DROP TABLE users
- DELETE FROM users WHERE email LIKE 'selenium_%' (Correct answer)
- UPDATE users SET active=0
Correct answer: DELETE FROM users WHERE email LIKE 'selenium_%'
A targeted DELETE with a WHERE clause removes only test-specific rows without affecting real data.
Question 6: In a data-driven Selenium framework, test inputs are stored in a database table. Which SQL keyword retrieves all columns from that table?
- GET
- FETCH
- SELECT * (Correct answer)
- SHOW
Correct answer: SELECT *
SELECT * retrieves all columns from the specified table, which is commonly used when reading test data rows.
Question 7: A Selenium test inserts a row in a @BeforeClass method and expects it to be visible in later test methods. Which database concept ensures this row persists between those methods?
- Rollback
- Transaction commit (Correct answer)
- Savepoint
- Index rebuild
Correct answer: Transaction commit
A commit finalizes the transaction and makes the inserted row permanently visible to subsequent queries.
In Selenium test automation, which Java class is commonly used to execute SQL queries against a JDBC data source?