Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet236/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   232   233   234   235   236   237   238   239   ...   245
Bog'liq
Java 17 Recipes

 How It Works
Sometimes you need to update data as you are parsing it. Usually, this technique 
involves testing the values returned from the database and updating them after 
comparison with another value. The easiest way is to make the ResultSet object 
updatable by passing the ResultSet.CONCUR_UPDATABLE constant to the Connection 
object’s createStatement() or prepareStatement() method. Doing so causes the 
Statement or PreparedStatement to produce an updatable ResultSet.
Note some database JDbC drivers do not support updatable ResultSets. see 
the documentation of your JDbC driver for more information. this code was run 
using oracle’s ojdbc6.jar JDbC driver on oracle database 11.2 release.
Chapter 12 Working With Databases


467
The format for creating a Statement object that produces an updatable ResultSet 
is to pass the ResultSet type as the first argument and the ResultSet concurrency 
as the second argument. The scroll type must be TYPE_SCROLL_SENSITIVE to ensure 
that ResultSet is sensitive to any updates. The following code demonstrates this 
technique by creating a Statement object that produces a scrollable and updatable 
ResultSet object.
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
The format for creating a PreparedStatement object that produces an updatable 
ResultSet is to pass the SQL string as the first argument, the ResultSet type as the 
second argument, and the ResultSet concurrency as the third argument. The solution 
to this recipe demonstrates this technique using the following line of code.
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
Both lines of code discussed in this section produce scrollable and updatable 
ResultSet objects. Once you have obtained an updatable ResultSet, you can use it just 
like an ordinary ResultSet for fetching values retrieved from the database. In addition, 
you can call one of the ResultSet object’s updateXXX() methods to update any value 
within the ResultSet. In the solution to this recipe, the updateString() method is 
called, passing the position of the value from the query as the first argument and the 
updated text as the second argument. In this case, the fourth element column listed in 
the SQL query is updated.
rs.updateString(4, desc + " -- More to come");
Finally, to persist the values that you have changed, call the ResultSet updateRow() 
method, as seen in the solution to this recipe.
rs.updateRow();
Creating an updatable ResultSet is not something that you need to do every day. In 
fact, you might never need to create an updatable ResultSet. However, for the cases in 
which such a strategy is needed, this technique can come in very handy.
Chapter 12 Working With Databases


468

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   232   233   234   235   236   237   238   239   ...   245




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