Junior — Middle
What types of operators or commands can be used in JDBC to execute SQL queries?
sobes.tech AI
Answer from AI
In JDBC, three main types of statements are used to execute SQL queries:
-
Statement — used for executing simple SQL queries without parameters, such as static queries like SELECT, INSERT, UPDATE, DELETE.
-
PreparedStatement — allows for executing parameterized queries. This enhances security (protection against SQL injection) and performance when executing the same query with different parameters repeatedly.
-
CallableStatement — used for calling stored procedures in the database.
Example of using PreparedStatement:
String sql = "SELECT * FROM users WHERE id = ?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, 10);
ResultSet rs = pstmt.executeQuery();