Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2023 Dynamia Soluciones IT S.A.S - NIT 900302344-1
* Colombia / South America
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tools.dynamia.domain.jdbc;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import tools.dynamia.commons.BeanUtils;
import tools.dynamia.commons.logger.LoggingService;
import tools.dynamia.commons.logger.SLF4JLoggingService;
import tools.dynamia.commons.reflect.PropertyInfo;
import tools.dynamia.commons.reflect.ReflectionException;
import javax.sql.DataSource;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* The Class JdbcHelper.
*
* @author Mario A. Serrano Leones
*/
public class JdbcHelper {
private final LoggingService logger = new SLF4JLoggingService(JdbcHelper.class);
/**
* The connection.
*/
private Connection connection;
/**
* The data source.
*/
private DataSource dataSource;
private boolean showSQL = true;
private Statement batch;
private boolean batchSupported;
private boolean inTransaction;
/**
* Instantiates a new jdbc helper.
*
* @param connection
* the connection
*/
public JdbcHelper(Connection connection) {
this.connection = connection;
}
/**
* Instantiates a new jdbc helper.
*
* @param dataSource
* the data source
*/
public JdbcHelper(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* Query.
*
* @param sql
* the sql
* @return the jdbc data set
*/
public JdbcDataSet query(String sql) {
try {
showSQL(sql);
Statement stm = getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stm.executeQuery(sql);
return new JdbcDataSet(stm, rs);
} catch (Exception ex) {
throw new JdbcException("Exception executing query: " + sql + " " + ex.getMessage(), ex);
}
}
/**
* Query.
*
* @param sql
* the sql
* @param params
* the params
* @return the jdbc data set
*/
public JdbcDataSet query(String sql, Object... params) {
try {
showSQL(sql);
PreparedStatement pstm = getConnection().prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
if (sql.contains("?") && params != null && params.length > 0) {
applyStatementParams(pstm, params);
}
ResultSet rs = pstm.executeQuery();
return new JdbcDataSet(pstm, rs);
} catch (Exception ex) {
throw new JdbcException("Exception executing query with params: " + sql + " " + ex.getMessage(), ex);
}
}
/**
* Query.
*
* @param sql
* the sql
* @param params
* the params
* @return the jdbc data set
*/
public JdbcDataSet query(String sql, Map params) {
try {
showSQL(sql);
NamedParameterJdbcTemplate tmp = new NamedParameterJdbcTemplate(dataSource);
List