Creating a statement and executing an SQL query

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 Statementwhich, 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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset