createStatement参数 和 批处理

1、createStatement();

2、createStatement(int resultSetType, int resultSetConcurrency)

3、createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)

 

 

其中resultSetType可选值是:

1、ResultSet.TYPE_FORWARD_ONLY :

在ResultSet中只能先前移动游标只允许向前访问一次,并且不会受到其他用户对该数据库所作更改的影响.

2、ResultSet.TYPE_SCROLL_INSENSITIVE :

在ResultSet中可以随心所欲的先前向后移动游标,不会受到其他用户对该数据库所作更改的影响

3、ResultSet.TYPE_SCROLL_SENSITIVE :

在ResultSet中可以随心所欲的先前向后移动游标,这种类型受到其他用户所作更改的影响。如果用户在执行完查询之后删除一个记录,那个记录将从 ResultSet 中消失。

同时

ResultSet的值有所改变的时候,他可以得到改变后的最新的值其中

resultSetConcurrency

(第二个参数默认为CONCUR_READ_ONLY:指定不可以更新 ResultSet

CONCUR_UPDATABLE:指定可以更新 ResultSet)

可选值是:

1、ResultSet.CONCUR_READ_ONLY 在ResultSet中的数据记录是只读的,可以修改

2、ResultSet.CONCUR_UPDATABLE 在ResultSet中的数据记录可以任意修改,然后更新会数据库其中resultSetHoldability

可选值是:

1、ResultSet.HOLD_CURSORS_OVER_COMMIT 表示修改提交时,不关闭ResultSet的游标

2、ResultSet.CLOSE_CURSORS_AT_COMMIT 表示修改提交时,关闭ResultSet的游标

 

 

 

 

 

 

demo

Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs2 = stmt.executeQuery("SELECT * FROM user");

rs2.next();

rs2.updateDate("lastdatetime", new Date(Calendar.getInstance().getTimeInMillis())); rs2.updateRow();

rs2.afterLast();

while(rs2.previous())

{ /**....*/ } rs.beforeFirst();

while(rs2.next())

{ /**....*/ } rs.last();

rs.first();

rs.absolute(5); //游标移动到第5条

rs.absolute(-1); //游标移动到最后一条

rs.relative(-5); //游标向上移动5条

rs.relative(2); //游标向下移动2条

rs.deleteRow(); //删除当前行

rs.last(); //游标移动到最后

rs.updateString("summary", "This is ...");//设置更新的字段值

rs.cancelRowUpdates();//取消刚才输入的更新

rs.getRow();//得到当前行号

rs.moveToInsertRow(); //游标移动到要新增的那条记录上

rs.updateInt("id", 1);

rs.updateString(2, "my name");

rs.insertRow(); //插入新记录

 

 

JDBC2.0提供的还有一个功能就是数据库的批量操作:

con.setAutoCommit(false);

Statement stmt3 = con.createStatement();

stmt3.addBatch("insert .....");  //push

stmt3.addBatch("insert .....");   //push

int[] rows = stmt3.executeBatch();

con.commit();

但是有一点要注意,stmt3.executeBatch()他不会自动给你回滚数据操作,当你有5条update语句的时候,如果第三条发生错误,那么将无法自动回滚前两条update语句的影响,所以一定要自己手工进行事务管理。

 

 

 

 

 

 

 

 

 

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
// JBuilder API Decompiler stub source generated from class file // 2010-1-15 // -- implementation of methods is not available package com.ibatis.common.jdbc; // Imports import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.Savepoint; import java.sql.Statement; import java.util.Map; public class SimplePooledConnection implements InvocationHandler { // Fields private static final String CLOSE = "close"; private static final Class[] IFACES; private int hashCode; private SimpleDataSource dataSource; private Connection realConnection; private Connection proxyConnection; private long checkoutTimestamp; private long createdTimestamp; private long lastUsedTimestamp; private int connectionTypeCode; private boolean valid; // Constructors public SimplePooledConnection(Connection connection, SimpleDataSource dataSource) { } // Methods public void invalidate() { } public boolean isValid() { return false;} public Connection getRealConnection() { return null;} public Connection getProxyConnection() { return null;} public int getRealHashCode() { return 0;} public int getConnectionTypeCode() { return 0;} public void setConnectionTypeCode(int connectionTypeCode) { } public long getCreatedTimestamp() { return 0L;} public void setCreatedTimestamp(long createdTimestamp) { } public long getLastUsedTimestamp() { return 0L;} public void setLastUsedTimestamp(long lastUsedTimestamp) { } public long getTimeElapsedSinceLastUse() { return 0L;} public long getAge() { return 0L;} public long getCheckoutTimestamp() { return 0L;} public void setCheckoutTimestamp(long timestamp) { } public long getCheckoutTime() { return 0L;} private Connection getValidConnection() { return null;} public int hashCode() { return 0;} public boolean equals(Object obj) { return false;} public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null;} public Statement createStatement() throws SQLException { return null;} public PreparedStatement prepareStatement(String sql) throws SQLException { return null;} public CallableStatement prepareCall(String sql) throws SQLException { return null;} public String nativeSQL(String sql) throws SQLException { return null;} public void setAutoCommit(boolean autoCommit) throws SQLException { } public boolean getAutoCommit() throws SQLException { return false;} public void commit() throws SQLException { } public void rollback() throws SQLException { } public void close() throws SQLException { } public boolean isClosed() throws SQLException { return false;} public DatabaseMetaData getMetaData() throws SQLException { return null;} public void setReadOnly(boolean readOnly) throws SQLException { } public boolean isReadOnly() throws SQLException { return false;} public void setCatalog(String catalog) throws SQLException { } public String getCatalog() throws SQLException { return null;} public void setTransactionIsolation(int level) throws SQLException { } public int getTransactionIsolation() throws SQLException { return 0;} public SQLWarning getWarnings() throws SQLException { return null;} public void clearWarnings() throws SQLException { } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public Map getTypeMap() throws SQLException { return null;} public void setTypeMap(Map map) throws SQLException { } public void setHoldability(int holdability) throws SQLException { } public int getHoldability() throws SQLException { return 0;} public Savepoint setSavepoint() throws SQLException { return null;} public Savepoint setSavepoint(String name) throws SQLException { return null;} public void rollback(Savepoint savepoint) throws SQLException { } public void releaseSavepoint(Savepoint savepoint) throws SQLException { } public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { return null;} }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值