A Statement object is used to invoke SQL statements in the database. The following snippet of code shows how to retrieve a Connection object from the connection pool. This object is used to create a new Statement, which, in turn, is used to execute an SQL statement:
try (Connection connection = pool.getConnection()) {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.execute("SELECT content FROM messages");
}
In this chapter, we are using the Statement interface and its createStatement counterpart method. In more critical applications, you should use the PreparedStatement interface and the prepareStatement method in order to increase performance and prevent SQL injection attacks.