以下是使用Result Set教程中描述的ResultSet.CONCUR_UPDATABLE和ResultSet.TYPE_SCROLL_INSENSITIVE示例。本示例將說明對表的INSERT,UPDATE和DELETE操作。
應(yīng)該注意的是,您正在處理的表應(yīng)正確設(shè)置主鍵。
該示例代碼是根據(jù)前幾章中的環(huán)境和數(shù)據(jù)庫設(shè)置編寫的。
復(fù)制并粘貼以下示例到JDBCExample.java中,如下編譯并運(yùn)行:
//步驟1.導(dǎo)入所需的軟件包 import java.sql.*; public class JDBCExample { // JDBC驅(qū)動程序名稱和數(shù)據(jù)庫URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // 數(shù)據(jù)庫憑證 static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; try{ //步驟2:注冊JDBC驅(qū)動程序 Class.forName("com.mysql.jdbc.Driver"); //步驟3:建立連接 System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //步驟4:執(zhí)行查詢以創(chuàng)建陳述 // RS示例的必需參數(shù)。 System.out.println("Creating statement..."); Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); //步驟5:執(zhí)行查詢 String sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); System.out.println("List result set for reference...."); printRs(rs); //步驟6:循環(huán)瀏覽結(jié)果集,并增加5個年齡 //移至BFR位置,以便while循環(huán)正常工作 rs.beforeFirst(); //步驟7:從結(jié)果集中提取數(shù)據(jù) while(rs.next()){ //按列名檢索 int newAge = rs.getInt("age") + 5; rs.updateDouble( "age", newAge ); rs.updateRow(); } System.out.println("List result set showing new ages..."); printRs(rs); // 在表中插入一條記錄。 //移動以使用updateXXX()插入行并添加列數(shù)據(jù) System.out.println("Inserting a new record..."); rs.moveToInsertRow(); rs.updateInt("id",104); rs.updateString("first","John"); rs.updateString("last","Paul"); rs.updateInt("age",40); //提交行 rs.insertRow(); System.out.println("List result set showing new set..."); printRs(rs); // 從表中刪除第二條記錄。 // 首先將位置設(shè)置為第二條記錄 rs.absolute( 2 ); System.out.println("List the record before deleting..."); //按列名檢索 int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //顯示值 System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); //刪除行 rs.deleteRow(); System.out.println("List result set after \ deleting one records..."); printRs(rs); //步驟8:清理環(huán)境 rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //處理JDBC錯誤 se.printStackTrace(); }catch(Exception e){ //處理Class.forName的錯誤 e.printStackTrace(); }finally{ //用于關(guān)閉資源 try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!"); }//結(jié)束main public static void printRs(ResultSet rs) throws SQLException{ //確保我們從第一行開始 rs.beforeFirst(); while(rs.next()){ //按列名檢索 int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //顯示值 System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } System.out.println(); }//結(jié)束printRs() }//結(jié)束JDBCExample
現(xiàn)在讓我們編譯上面的示例,如下所示:
C:\>javac JDBCExample.java C:\>
運(yùn)行時JDBCExample,它將產(chǎn)生以下結(jié)果-
C:\>java JDBCExample Connecting to database... Creating statement... List result set for reference.... ID: 100, Age: 33, First: Zara, Last: Ali ID: 101, Age: 40, First: Mahnaz, Last: Fatma ID: 102, Age: 50, First: Zaid, Last: Khan ID: 103, Age: 45, First: Sumit, Last: Mittal List result set showing new ages... ID: 100, Age: 38, First: Zara, Last: Ali ID: 101, Age: 45, First: Mahnaz, Last: Fatma ID: 102, Age: 55, First: Zaid, Last: Khan ID: 103, Age: 50, First: Sumit, Last: Mittal Inserting a new record... List result set showing new set... ID: 100, Age: 38, First: Zara, Last: Ali ID: 101, Age: 45, First: Mahnaz, Last: Fatma ID: 102, Age: 55, First: Zaid, Last: Khan ID: 103, Age: 50, First: Sumit, Last: Mittal ID: 104, Age: 40, First: John, Last: Paul List the record before deleting... ID: 101, Age: 45, First: Mahnaz, Last: Fatma List result set after deleting one records... ID: 100, Age: 38, First: Zara, Last: Ali ID: 102, Age: 55, First: Zaid, Last: Khan ID: 103, Age: 50, First: Sumit, Last: Mittal ID: 104, Age: 40, First: John, Last: Paul Goodbye! C:\>