All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.dbutils.InsertSQLContext Maven / Gradle / Ivy

package net.dongliu.dbutils;

import net.dongliu.commons.exception.Exceptions;
import net.dongliu.commons.io.Closeables;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * For query sql execute
 *
 * @author Liu Dong
 */
public class InsertSQLContext extends SingleSQLContext {
    @Override
    protected InsertSQLContext self() {
        return this;
    }

    public SQLResultSet execute() {
        try {
            PreparedStatement statement = keyColumns == null ?
                    connection.prepareStatement(clause, Statement.RETURN_GENERATED_KEYS)
                    : connection.prepareStatement(clause, keyColumns);
            try {
                this.fillStatement(statement, params);
                statement.executeUpdate();
                ResultSet rs = statement.getGeneratedKeys();
                if (closeConn) {
                    return new SQLResultSet(rs, statement, connection);
                } else {
                    return new SQLResultSet(rs, statement);
                }
            } catch (Throwable e) {
                Closeables.closeQuietly(statement);
                throw e;
            }
        } catch (SQLException e) {
            if (closeConn) {
                Closeables.closeQuietly(connection);
            }
            throw Exceptions.sneakyThrow(e);
        } catch (Throwable t) {
            if (closeConn) {
                Closeables.closeQuietly(connection);
            }
            throw t;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy