⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 batch.java

📁 一个Java持久层类库
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package ActiveObject.core;import ActiveObject.vo.ObjectInfo;import ActiveObject.exception.*;import java.sql.*;import java.util.*;/** * * @author tanjiazhang */public class Batch extends Task{    private Connection connection = null;        private Map<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();        protected Batch(Connection connection) throws SQLException    {        this.connection = connection;        connection.setAutoCommit(false);//显式声明事务    }        /**     * 保存一个对象,但不会令该对象成为持久化对象     */    public Batch save(ActiveRecord record) throws ObjectAnalysisException, SQLException    {        ObjectInfo objectInfo = ActiveRecordHelper.analizeObjectInfo(record.getClass());           String key = createSaveKey(record);        PreparedStatement stmt = statements.get(key);        if(stmt == null)        {            stmt = connection.prepareStatement(objectInfo.getBasicSqlStatement().getInsertStatement());            statements.put(key, stmt);        }        objectInfo.setupSaveParams(stmt, record);        stmt.addBatch();        //System.out.println(objectInfo.getBasicSqlStatement().getInsertStatement());        return this;    }    public Batch update(ActiveRecord record) throws ObjectAnalysisException, ObjectNotActive, SQLException    {        if(record.isActive == false)            throw new ObjectNotActive("This is not a active object");        ObjectInfo objectInfo = ActiveRecordHelper.analizeObjectInfo(record.getClass());        String key = createUpdateKey(record);        PreparedStatement stmt = statements.get(key);        if(stmt == null)        {            stmt = connection.prepareStatement(objectInfo.getBasicSqlStatement().getUpdateStatement());            statements.put(key, stmt);        }        objectInfo.setupUpdateParams(stmt, record);        stmt.addBatch();        return this;    }        public Batch delete(ActiveRecord record) throws ObjectAnalysisException, ObjectNotActive, SQLException    {        if(record.isActive == false)            throw new ObjectNotActive();        ObjectInfo objectInfo = ActiveRecordHelper.analizeObjectInfo(record.getClass());        String key = createDeleteKey(record);        PreparedStatement stmt = this.statements.get(key);        if(stmt == null)        {            stmt = this.connection.prepareStatement(objectInfo.getBasicSqlStatement().getDeleteStatement());            this.statements.put(key, stmt);        }        objectInfo.setupDeleteParams(stmt, record);        stmt.addBatch();        return this;    }        /**     * 执行批量操作,需要手动关闭     * @throws java.sql.SQLException     */    public void execute() throws SQLException    {        for(String ket : statements.keySet())        {            statements.get(ket).executeBatch();        }        connection.commit();    }        /**     * 关闭batch并释放资源     * @throws java.sql.SQLException     */    public void close() throws SQLException    {        connection.close();        this.statements = null;    }        /**     * Execute the batch and then close it     * @throws java.sql.SQLException     */    public final void finish() throws SQLException    {        execute();        close();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -