
com.luoshu.open.id.SqlUtil Maven / Gradle / Ivy
package com.luoshu.open.id;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 提供简单的 sql 操作工具
*/
class SqlUtil {
private static final Logger logger = LoggerFactory.getLogger(SqlUtil.class);
/**
* 操作ID的数据源
*/
private static DataSource idDataSource;
/**
* 执行SQL ,这个是没有返回值的,一般用于创建表
* @param dataSource 数据源
* @param sql sql , 一般是创建表
*/
public static void execute(DataSource dataSource , String sql) throws SQLException {
Connection connection = null;
try {
connection = dataSource.getConnection();
Statement statement = connection.createStatement();
boolean execute = statement.execute(sql);
// if(!execute){
// throw new SQLException("sql invoke error");
// }
} finally {
close(connection);
}
}
/**
* 执行 sql 查询
* @param dataSource 数据源
* @param sql 查询 sql
* @return .
*/
public static void executeQuery(DataSource dataSource , String sql) throws SQLException {
Connection connection = null;
try {
connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
// return resultSet;
} finally {
close(connection);
}
}
public static void close(Connection connection){
if(connection != null){
try {
connection.close();
} catch (Exception e) {
logger.error(e.getMessage() , e);
}
}
}
public synchronized static DataSource createOrGetDataSource(LuoshuIdProperties properties){
if(idDataSource == null){
if(hasClass("com.alibaba.druid.pool.DruidDataSource")){
idDataSource = DruidDataSourceCreator.create(properties);
}else if(hasClass("com.zaxxer.hikari.HikariDataSource")){
idDataSource = HikariDataSourceCreator.create(properties);
}else{
throw new RuntimeException("can not find DataSource class");
}
}
return idDataSource;
}
private static class DruidDataSourceCreator{
public static DataSource create(LuoshuIdProperties properties){
com.alibaba.druid.pool.DruidDataSource ds = new com.alibaba.druid.pool.DruidDataSource();
ds.setDriverClassName(properties.getJdbc().getDriverClassName());
ds.setUrl(properties.getJdbc().getUrl());
ds.setUsername(properties.getJdbc().getUsername());
ds.setPassword(properties.getJdbc().getPassword());
ds.setMaxActive(properties.getJdbc().getMaxActive());
ds.setValidationQuery("SELECT 1;");
ds.setMaxWait(60000);
return ds;
}
}
private static class HikariDataSourceCreator{
public static DataSource create(LuoshuIdProperties properties){
com.zaxxer.hikari.HikariDataSource ds = new com.zaxxer.hikari.HikariDataSource();
ds.setDriverClassName(properties.getJdbc().getDriverClassName());
ds.setJdbcUrl(properties.getJdbc().getUrl());
ds.setUsername(properties.getJdbc().getUsername());
ds.setPassword(properties.getJdbc().getPassword());
ds.setMaximumPoolSize(properties.getJdbc().getMaxActive());
ds.setConnectionTestQuery("SELECT 1;");
return ds;
}
}
public static boolean hasClass(String className){
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy