[学习笔记]

在前面的jdbc的Helloworld程序当中,我们接触了最简单的 Statement。那种Statement的光标只能向前移。意思就是访问完2,只能继续访问3,不能再回过头来访问1。还有就是当我们查询数据库的时 候,我们不能同时修改数据库。但在现实生活当中,我们确实有这种需求,就是如果当我们正在查询一个数据库的时候,发现某个数据有问题,想当时就修改它。对 付这种情况,sun公司专门提供了一种新的Statement。即Scrollable(可滚动的,可向前可向后)和Updatable(可更新的)的 Statement。即con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
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();
    }
}

文章转载自原文:https://blog.csdn.net/qq_44594249/article/details/100765190

java当中JDBC当中Scrollable和Updatable ResultSet的用法和Helloworld例子的更多相关文章

  1. java当中JDBC当中请给出一个sql server的helloworld例子

    [学习笔记] 1.sql server的helloworld例子: import java.sql.*; public class JdbcHelloSqlServer {  public stati ...

  2. java当中JDBC当中请给出一个DataSource的单态模式(SingleTon)HelloWorld例子

    [学习笔记] 2.DataSource的单态模式(SingleTon)程序 咱们还接着上面的例子来说.1万个人要看书.千万确保要只建立一个图书馆.要是一不留神,建了两个或三个图书馆,那可就亏大发了.对 ...

  3. java中JDBC当中请给出一个DataSource的HelloWorld例子

    在前面 的jdbc的Helloworld程序当中,我们用DriverManager来获取数据库连接.事实上通过这种方法获取数据库连接,是比较耗费计算机资 源的.当然了,这也是没有办法的事儿.就像我们买 ...

  4. java当中JDBC当中JNDI用来查找dataSource的例子

    [学习笔记] 8.JNDI用来查找dataSource的例子: import javax.naming.InitialContext;import javax.naming.Context; impo ...

  5. java当中JDBC当中请给出一个sql server的stored procedure例子

    3.sql server的stored procedure例子: import java.sql.*;public class StoredProc0 {public static void main ...

  6. java当中JDBC当中的transaction例子

    [学习笔记] 7.jdbc的transaction例子: import java.sql.*; public class MySQlTransaction1 { public static void ...

  7. java当中JDBC当中请给出一个sql server的dataSource的helloworld例子

     [学习笔记] 4. sql server的dataSource的helloworld: import java.sql.*;import javax.sql.*;import net.sourcef ...

  8. java当中JDBC当中请给出一个SQLServer DataSource and SingleTon例子

    [学习笔记] 5.SQLServer DataSource and SingleTon: import net.sourceforge.jtds.jdbcx.*;import java.sql.*;i ...

  9. java当中JDBC当中请给出一个Oracle DataSource and SingleTon例子

    [学习笔记] 6.Oracle DataSource and SingleTon: import oracle.jdbc.pool.OracleDataSource;import java.sql.C ...

随机推荐

  1. Codeforces Round #576 (div.1 + div.2)

    Div2 A 长度为\(n(n≤10^5)\)的数组,每个元素不同,求有多少个位置\(d\)满足\(d - x \le j < d \And d < j \le d + y a_d< ...

  2. docker中部署django项目~~Dockfile方式和compose方式

    1.  背景:   本机win10上,后端django框架代码与前端vue框架代码联调通过. 2.  目的:   在centos7系统服务器上使用docker容器部署该项目. 3.  方案一:仅使用基 ...

  3. C# 使用大漠插件, 源码在Github和码云 ..希望对大家有所帮助

    c# 使用大漠插件. 完成 类似 按键精灵的 功能. 方法 注释 正在慢慢的 完善中 目录 仓库 github 码云 准备 效果图 如何运行 1. 注册 大漠dll 到com (资源在 源码 DLL ...

  4. C格式字符串转为二叉树

    最近在LeetCode做题,二叉树出现错误时不好排查,于是自己写了一个函数,将前序遍历格式字串转换成二叉树. 形如 "AB#D##C##" 的字符串,"#"表示 ...

  5. k8s 配置文件 详解

    apiVersion: v1 # [必须]版本号kind: Pod # [必选]Podmetadata: # [必选-Object]元数据 name: String # [必选] Pod的名称 nam ...

  6. 【转】Android检查手机是否被root

    目前来说Android平台并没有提供能够root检查的工具.但是我们可以通过两种方式来判断 手机里面是否有su文件 这个su文件是不是能够执行 但是这两种检查方式都存在缺点. 第一种存在误测和漏测的情 ...

  7. Rare-Variant Association Analysis | 罕见变异的关联分析

    Rare-Variant Association Analysis: Study Designs and Statistical Tests 10 Years of GWAS Discovery: B ...

  8. CMU Database Systems - Query Processing

    Query Model Query处理有三种方式, 首先是Iterator model,这是最基本的model,又称为volcano,pipeline模式 他是top-down的模式,通过next函数 ...

  9. MiniUI treeGrid 动态加载数据与静态加载数据的区别

    说明:treegrid静态数据加载时数据结构是一棵树包含children节点集合,而采用动态加载数据时数据是List结构的具体项. 静态加载数据 test1.html <!DOCTYPE htm ...

  10. mysql 安装参考

    https://blog.csdn.net/qq_38756992/article/details/84929787 https://www.cnblogs.com/joyny/p/10991194. ...