Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet240/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   237   238   239   240   241   242   243   244   245
Bog'liq
Java 17 Recipes

 Problem
Rather than manually opening and closing resources with each database call, you would 
prefer to have the application handle such boilerplate code for you.
 Solution
Use the try-with-resources syntax to automatically close the resources that you open. 
The following block of code uses this tactic to automatically close the Connection, 
Statement, and ResultSet resources when it is finished using them.
public class TryWithResourcesExample {
public static CreateConnection createConn;
public static void main(String[] args) {
createConn = new CreateConnection();
queryDatabase();
}
Chapter 12 Working With Databases


477
public static void queryDatabase() {
String qry = "select recipe_number, recipe_name, 
description from recipes";
try (Connection conn = createConn.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(qry);) {
while (rs.next()) {
String recipe = rs.getString( 
"RECIPE_NUMBER");
String name = rs.getString("RECIPE_NAME");
String desc = rs.getString("DESCRIPTION");
System.out.println(recipe + "\t" + name + 
"\t" + desc); }
} catch (SQLException e) {
e.printStackTrace();
}
}
}
The resulting output from running this code should look similar to the following.
Successfully connected
12-1 Connecting to a Database DriverManager and DataSource 
Implementations - More to Come
12-2 Querying a Database and Retrieving Results Subject to Change
12-3 Handling SQL Exceptions Using SQLException
 How It Works
Java 7 introduced automatic resource management using try-with-resources. Through 
this technique, the developer no longer needs to close each resource manually, which is 
a change that can cut down on many lines of code.
To use this technique, you must instantiate all the resources you want to have 
automatic handling enabled within a set of parentheses after a try clause. In the solution 
to this recipe, the declared resources are Connection, Statement, and ResultSet.
Chapter 12 Working With Databases


478
try (Connection conn = createConn.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(qry);) {
Once those resources are out of scope, they are automatically closed. This means 
there is no longer a requirement to code a finally block to ensure that resources are 
closed. The automatic resource handling is available to database work and any resource 
that complies with the new java.lang.Autocloseable API. Other operations such as file 
I/O adhere to the new API as well. There is a single close() method within java.lang.
Autoclosable that manages the closing of the resource. Classes that implement the 
java.io.Closeable interface can adhere to the API.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   237   238   239   240   241   242   243   244   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling