Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
How It Works
The same basic code format is used for performing just about every database task. The format is as follows. 1. Obtain a connection to the database. 2. Create a statement from the connection. 3. Perform a database task with the statement. 4. Do something with the results of the database task. 5. Close the statement (and database connection if you’re finished using it). Chapter 12 Working With Databases 443 The main difference between performing a query using JDBC and DML is that you call different methods on the Statement object, depending on which operation you want to perform. To perform a query, you need to call the Statement executeQuery() method. To perform DML tasks such as insert, update, and delete, call the executeUpdate() method. The performCreate() method in the solution to this recipe demonstrates inserting a record into a database. To insert a record in the database, construct a SQL INSERT statement in string format. To perform the insert, pass the SQL string to the Statement object’s executeUpdate() method. If the INSERT is performed, an int value is returned that specifies the number of rows that have been inserted. If the INSERT operation is not performed successfully, either a zero is returned, or SQLException is thrown, indicating a problem with the statement or database connection. The performRead() method in the solution to this recipe demonstrates querying the database. To execute a query, call the Statement object’s executeQuery() method, passing a SQL statement in string format. The result is a ResultSet object, which can then work with the returned data. For more information on performing queries, see Recipe 12-4. The performUpdate() method in the solution to this recipe demonstrates updating records within a database table. First, construct a SQL UPDATE statement in string format. Next, to perform the update operation, pass the SQL string to the Statement object’s executeUpdate() method. If the UPDATE is successfully performed, an int value is returned, which specifies the number of updated records. If the UPDATE operation is not performed successfully, either a zero is returned, or SQLException is thrown, indicating a problem with the statement or database connection. The last database operation that needs to be covered is the DELETE operation. The performDelete() method in the solution to this recipe demonstrates deleting records from the database. First, construct a SQL DELETE statement in string format. Next, to execute the deletion, pass the SQL string to the Statement object’s executeUpdate() method. If the deletion is successful, an int value specifying the number of rows deleted is returned. Otherwise, if the deletion fails, a zero is returned, or SQLException is thrown, indicating a problem with the statement or database connection. Almost every database application uses at least one of the CRUD operations at some point. This is foundational JDBC that needs to be known if you are working with databases within Java applications. Even if you do not work directly with the JDBC API, it is good to know these foundational basics. Chapter 12 Working With Databases |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling