JDBC当中Scrollable和Updatable ResultSet的用法和Helloworld例子
马克-to-win:在前面的jdbc的Helloworld程序当中,我们接触了最简单的 Statement。那种Statement的光标只能向前移。意思就是访问完2,只能继续访问3,不能再回过头来访问1。还有就是当我们查询数据库的时候,我们不能同时修改数据库。但在现实生活当中,我们确实有这种需求,就是如果当我们正在查询一个数据库的时候,发现某个数据有问题,想当时就修改它。对付这种情况,sun公司专门提供了一种新的Statement。即Scrollable(可滚动的,可向前可向后)和Updatable(可更新的)的 Statement。即con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
1.Update(更新) a row程序
以下这个程序就把第二条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程序
下面的程序在第二个row的后面插入了一个新的row(因为rs.absolute(2);)实际上,当你取回一个ResultSet后,和这个 ResultSet联系在一起的,还有一个叫InsertRow的特殊的row。它就像是一个特殊的缓冲区。rs.moveToInsertRow(); 就把你的光标指向它了,接着你把你要插入的数据先放在这个缓冲区里。当你执行rs.insertRow()的时候,这个特殊的row里的数据,就被插入到数据库当中了。
例: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();
}
}