Java MCQ: Which interface is used to execute SQL queries in JDBC?
a) Statement
b) ResultSet
c) Connection
d) PreparedStatement
Answer:
a) Statement
Explanation:
The Statement
interface is used to execute SQL queries in JDBC. It provides methods to execute SQL statements, such as executeQuery()
for SELECT statements, executeUpdate()
for INSERT, UPDATE, and DELETE statements, and execute()
for executing any kind of SQL statement.
Here’s an example of using Statement
to execute a query:
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM employees");
This example creates a Statement
object and uses it to execute a SELECT query, returning the results in a ResultSet
object.
In addition to Statement
, JDBC also provides the PreparedStatement
interface for executing parameterized queries, which is more secure and efficient for repeated queries.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html