Junior — Middle
Can you explain what is meant by the Statement term in JDBC usage?
sobes.tech AI
Answer from AI
In JDBC (Java Database Connectivity), Statement is an interface used to execute SQL queries against a database. With a Statement object, you can send SQL commands (such as SELECT, INSERT, UPDATE, DELETE) and receive results.
Main features:
- Created through a
Connectionobject using thecreateStatement()method. - Allows executing queries with
executeQuery()(for SELECT) andexecuteUpdate()(for INSERT, UPDATE, DELETE). - Does not support parameterized queries, which can lead to vulnerabilities (e.g., SQL injection).
Example usage:
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString("username"));
}
rs.close();
stmt.close();
conn.close();