Danh mục

Java Database Programming Bible- P4

Số trang: 50      Loại file: pdf      Dung lượng: 752.00 KB      Lượt xem: 28      Lượt tải: 0    
10.10.2023

Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Database Programming Bible- P4: Welcome to Java Database Programming Bible. This book is for readers who are alreadyfamiliar with Java, and who want to know more about working with databases. The JDBCApplication Programming Interface has made database programming an importantaspect of Java development, particularly where Web applications are concerned.
Nội dung trích xuất từ tài liệu:
Java Database Programming Bible- P4 Chapter 4:Introduction to JDBC 2. Set a new value for each column in the row by using the appropriate update method. 3. Call the method insertRow() to insert the new row into the result set and, simultaneously, into the database. Listing 4-9 demonstrates the use of the UpdatableResultSet to insert a new row into a database. Listing 4-9: Using UpdatableResultSet to insert a new row Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection con = DriverManager.getConnection (jdbc:odbc:Contacts); Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery(query); rs.moveToInsertRow(); rs.updateInt(Contact_ID, 150); rs.updateString(First_Name, Nigel); rs.updateString(Last_Name, Thornebury); Y FL rs.insertRow(); AM If you insert a row without supplying a value for every column in the row, the default value for the column will be used if there is one. Otherwise, if the column accepts SQL NULL values, a NULL will be inserted. Failing either of those, a SQLException TE will be thrown. You will also get a SQLException if a required table column is missing in the ResultSet you use to insert the row, so the query used to get the ResultSet object should generally select all columns, though you will probably want to use a WHERE clause to limit the number of rows returned by your SELECT statement. Caution If you move the cursor from the insert row before calling the method insertRow(), you will lose all of the values you have added to the insert row. To move the cursor from the insert row back to the result set, you can use any of the methods that put the cursor on a specific row: first, last, beforeFirst, afterLast, and absolute. You can also use the methods previous and relative because the result set maintains a record of the current row while accessing the insert row. In addition, you can use a special method: moveToCurrentRow(), which can be called only when the cursor is on the insert row. This method moves the cursor from the insert row back to the row that was previously the current row. -149 - Team-Fly®Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 4:Introduction to JDBC Deleting a Row Deleting a row in an UpdatableResultSet is very simple. All you have to do is move the cursor to the row you want to delete and call the method deleteRow(). The example in the following code snippet shows how to delete the third row in a result set by getting the ResultSet object, moving the cursor to the third row, and using the deleteRow() method: Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection con = DriverManager.getConnection (jdbc:odbc:Contacts); Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery(query); rs.absolute(3); rs.deleteRow(); Caution Be aware that different JDBC drivers handle deletions in different ways. Some remove a deleted row so that it is no longer visible in a result set, and others insert a blank row where the deleted row used to be. When you make a change to a ResultSet, the change may not necessarily be visible. The next section explains the reasons. Seeing Changes in ResultSets Changes made to a ResultSet are not necessarily visible, either to the ResultSet itself or to other open transactions. In this context, the terms visible and not visible have the following meanings: § An update is visible if the updated value can be retrieved by calling the appr ...

Tài liệu được xem nhiều:

Tài liệu cùng danh mục:

Tài liệu mới: