The ResultSet interface is used to retrieve data from a database after executing a query.
importjava.sql.ResultSet;ResultSet resultSet =statement.executeQuery("SELECT * FROM mytable");while (resultSet.next()) {int id =resultSet.getInt("id");String name =resultSet.getString("name");// Process data}
Batch Processing:
JDBC supports batch processing, which allows you to group multiple SQL statements into a batch for efficient execution.
Statement statement =connection.createStatement();statement.addBatch("INSERT INTO mytable (name) VALUES ('John')");statement.addBatch("INSERT INTO mytable (name) VALUES ('Jane')");int[] result =statement.executeBatch();
Exception Handling:
Always ensure proper exception handling in your JDBC code.
For better performance, consider using connection pooling libraries like Apache DBCP or HikariCP to manage database connections efficiently.
These examples provide a basic overview of JDBC statements. Remember to include the appropriate JDBC driver for your database (e.g., MySQL, PostgreSQL) in your project's classpath. Additionally, handle resource cleanup (e.g., closing statements and connections) properly to avoid resource leaks.