JDBC當(dāng)中Scrollable和Updatable ResultSet的用法和Helloworld例子

馬克-to-win:在前面的jdbc的Helloworld程序當(dāng)中,我們接觸了最簡(jiǎn)單的 Statement。那種Statement的光標(biāo)只能向前移。意思就是訪問(wèn)完2,只能繼續(xù)訪問(wèn)3,不能再回過(guò)頭來(lái)訪問(wèn)1。還有就是當(dāng)我們查詢數(shù)據(jù)庫(kù)的時(shí)候,我們不能同時(shí)修改數(shù)據(jù)庫(kù)。但在現(xiàn)實(shí)生活當(dāng)中,我們確實(shí)有這種需求,就是如果當(dāng)我們正在查詢一個(gè)數(shù)據(jù)庫(kù)的時(shí)候,發(fā)現(xiàn)某個(gè)數(shù)據(jù)有問(wèn)題,想當(dāng)時(shí)就修改它。對(duì)付這種情況,sun公司專門提供了一種新的Statement。即Scrollable(可滾動(dòng)的,可向前可向后)和Updatable(可更新的)的 Statement。即con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。

1.Update(更新) a row程序

以下這個(gè)程序就把第二條row的id更改成了“11”。



例:5.1.1

/*when do this experiment, if it is sql server,pls make sure you have a primary key in your table.*/
import java.sql.*;
public class TestMark_to_win {
    public static void main(String[] args) throws SQLException,
            ClassNotFoundException {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = java.sql.DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "1234");
            String s = "select * from login";
        /* A default ResultSet object is not updatable and has a cursor that
             * moves forward only. Thus, you can iterate through it only once
             * and only from the first row to the last row. It is possible to
             * produce ResultSet objects that are scrollable and/or updatable.
             */
            Statement stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                            ResultSet.CONCUR_UPDATABLE);
            ResultSet rs = stm.executeQuery(s);
            /* boolean absolute(int row) Moves the cursor to the given row
             * number in this ResultSet object.who is 1? experimentally,but
             * undocumentally, the order is based on the primary key column.
             */
            rs.absolute(2);
     /* public void updateString(String columnName,string s)throws SQLException
             * Updates the designated column with a string value. The updater
             * methods are used to update column values in the current row or
             * the insert row. The updater methods do not update the underlying
             * database; instead the updateRow or insertRow methods are called
             * to update the database. */
            rs.updateString("id", "11");
            // rs.cancelRowUpdates();
            /* public void updateRow() throws SQLException Updates the
             * underlying database with the new contents of the current row of
             * this ResultSet object.*/
            rs.updateRow();
            rs.close();
            stm.close();
            con.close();
    }
}




2.insert a row程序

下面的程序在第二個(gè)row的后面插入了一個(gè)新的row(因?yàn)閞s.absolute(2);)實(shí)際上,當(dāng)你取回一個(gè)ResultSet后,和這個(gè) ResultSet聯(lián)系在一起的,還有一個(gè)叫InsertRow的特殊的row。它就像是一個(gè)特殊的緩沖區(qū)。rs.moveToInsertRow(); 就把你的光標(biāo)指向它了,接著你把你要插入的數(shù)據(jù)先放在這個(gè)緩沖區(qū)里。當(dāng)你執(zhí)行rs.insertRow()的時(shí)候,這個(gè)特殊的row里的數(shù)據(jù),就被插入到數(shù)據(jù)庫(kù)當(dāng)中了。



例:5.2.1

/*when do this experiment, if it is sql server,pls make sure you have a primary key in your table.*/
import java.sql.*;
public class TestMark_to_win {
    public static void main(String[] args) throws SQLException,
            ClassNotFoundException {

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = java.sql.DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test", "root", "1234");
        String s = "select * from login";
        /*
         * A default ResultSet object is not updatable and has a cursor that
         * moves forward only. Thus, you can iterate through it only once and
         * only from the first row to the last row. It is possible to produce
         * ResultSet objects that are scrollable and/or updatable.
         */
        Statement stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = stm.executeQuery(s);
        rs.absolute(2);
        /* public void moveToInsertRow() throws SQLException Moves
         * the cursor to the insert row. The current cursor position is
         * remembered while the cursor is positioned on the insert row. The
         * insert row is a special row associated with an updatable result set.
         * It is essentially a buffer where a new row may be constructed by
         * calling the updater methods prior to inserting the row into the
         * result set.
         */
        rs.moveToInsertRow();
        rs.updateString("id", "2.5");
        rs.updateString("name", "qqq");
        /* public void insertRow() throws SQLException Inserts the contents of
         * the insert row into this ResultSet object and into the database.
         */
        rs.insertRow();


        rs.close();
        stm.close();
        con.close();

    }
}