java当中JDBC当中Scrollable和Updatable ResultSet的用法和Helloworld例子
[学习笔记]
在前面的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例子的更多相关文章
- java当中JDBC当中请给出一个sql server的helloworld例子
[学习笔记] 1.sql server的helloworld例子: import java.sql.*; public class JdbcHelloSqlServer { public stati ...
- java当中JDBC当中请给出一个DataSource的单态模式(SingleTon)HelloWorld例子
[学习笔记] 2.DataSource的单态模式(SingleTon)程序 咱们还接着上面的例子来说.1万个人要看书.千万确保要只建立一个图书馆.要是一不留神,建了两个或三个图书馆,那可就亏大发了.对 ...
- java中JDBC当中请给出一个DataSource的HelloWorld例子
在前面 的jdbc的Helloworld程序当中,我们用DriverManager来获取数据库连接.事实上通过这种方法获取数据库连接,是比较耗费计算机资 源的.当然了,这也是没有办法的事儿.就像我们买 ...
- java当中JDBC当中JNDI用来查找dataSource的例子
[学习笔记] 8.JNDI用来查找dataSource的例子: import javax.naming.InitialContext;import javax.naming.Context; impo ...
- java当中JDBC当中请给出一个sql server的stored procedure例子
3.sql server的stored procedure例子: import java.sql.*;public class StoredProc0 {public static void main ...
- java当中JDBC当中的transaction例子
[学习笔记] 7.jdbc的transaction例子: import java.sql.*; public class MySQlTransaction1 { public static void ...
- java当中JDBC当中请给出一个sql server的dataSource的helloworld例子
[学习笔记] 4. sql server的dataSource的helloworld: import java.sql.*;import javax.sql.*;import net.sourcef ...
- java当中JDBC当中请给出一个SQLServer DataSource and SingleTon例子
[学习笔记] 5.SQLServer DataSource and SingleTon: import net.sourceforge.jtds.jdbcx.*;import java.sql.*;i ...
- java当中JDBC当中请给出一个Oracle DataSource and SingleTon例子
[学习笔记] 6.Oracle DataSource and SingleTon: import oracle.jdbc.pool.OracleDataSource;import java.sql.C ...
随机推荐
- 2019 SDN上机第一次实验作业
1. 安装轻量级网络仿真工具Mininet 先从GitHub上获取mininet源码,再输入命令进行安装,代码分别如下: git clone https://github.com/mininet/mi ...
- python TypeError: unsupported operand type(s) for +: 'geoprocessing value object' and 'str'
TypeError: unsupported operand type(s) for +: 'geoprocessing value object' and 'str' if self.params[ ...
- 前端通用下载文件方法(兼容IE)
之前在网上看到一个博主写的前端通用的下载文件的方法,个人觉得很实用,所以mark一下,方便以后查阅 源文地址(源文还有上传/下载excel文件方法) 因为项目要求要兼容IE浏览器,所以完善了一下之前博 ...
- javaweb--Rest访问(RestTemplate)
Rest访问(RestTemplate)在实际的项目中,往往需要发送一个Get/Post请求到其他的系统(Rest API),比如向人员管理部门请求,然后解析返回信息获取该用户的基本信息等.JDK传统 ...
- 000 基于Spring boot发送邮件
发送邮件的程序,使用QQ的服务器,经过测试,完全可行.可复现 一:准备工作 1.找到账号的授权码 这个是程序需要使用的. 在设置中查找. 2.新建项目的目录 二:完整的程序代码 1.pom.xml & ...
- PHP如何防止注入及开发安全
1.PHP注入的基本原理 程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对 用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据 库查询代码,根据程序返回 ...
- java-mybaits-014-数据库缓存设计【querycache、mybatis一级缓存、二级缓存】
一.概述 一般来说,可以在5个方面进行缓存的设计: 1.最底层可以配置的是数据库自带的query cache, 2.mybatis的一级缓存,默认情况下都处于开启状态,只能使用自带的Perpetual ...
- spring boot集成Websocket
websocket实现后台像前端主动推送消息的模式,可以减去前端的请求获取数据的模式.而后台主动推送消息一般都是要求消息回馈比较及时,同时减少前端ajax轮询请求,减少资源开销. spring boo ...
- QCamera检测摄像头
The QCamera class provides interface for system camera devices. More... Header: #include <QCamera ...
- C/C++代码静态分析工具调研
C/C++代码静态分析工具调研 摘自:https://www.jianshu.com/p/92886d979401 简述 静态分析(static analysis)是指在不执行代码的情况下对其进行分析 ...