add
This commit is contained in:
@ -3,6 +3,7 @@ package com.qiaoba.common.database.handlers.schema;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.database.utils.SqlUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -32,14 +33,10 @@ public class MysqlSchemaHandler implements SchemaHandler {
|
||||
// eg: use qiaoba-1;
|
||||
String sql = StrUtil.format("use `{}-{}`;", baseDatabase, BaseContext.getTenantId());
|
||||
log.debug("Run MysqlSchemaHandler, Sql: {}", sql);
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = conn.createStatement();
|
||||
statement.execute(sql);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
IoUtil.close(statement);
|
||||
SqlUtil.runSql(conn, sql);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.database.config.DynamicDataSourceConfig;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
@ -49,7 +50,7 @@ public class JdbcUtil {
|
||||
/**
|
||||
* 检查数据源连接可用性
|
||||
*
|
||||
* @param conn 数据源
|
||||
* @param conn 数据源
|
||||
* @return 结果
|
||||
*/
|
||||
public static boolean checkConnect(Connection conn) {
|
||||
@ -81,4 +82,28 @@ public class JdbcUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static CheckResult check(String driver, String url, String username, String password) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName(driver);
|
||||
//建立连接
|
||||
conn = DriverManager.getConnection(url, username, password);
|
||||
return new CheckResult(true, null);
|
||||
} catch (Exception e) {
|
||||
return new CheckResult(false, e.getMessage());
|
||||
} finally {
|
||||
IoUtil.close(conn);
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class CheckResult {
|
||||
private boolean result;
|
||||
private String msg;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(check("oracle.jdbc.OracleDriver", "jdbc:oracle:thin:@//192.168.0.205:1521/ORCL", "system", "root"));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package com.qiaoba.common.database.utils;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* SqlUtil
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/27 17:30
|
||||
*/
|
||||
public class SqlUtil {
|
||||
|
||||
public static void runSql(Connection connection, String sql) throws SQLException {
|
||||
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = connection.createStatement();
|
||||
statement.execute(sql);
|
||||
} finally {
|
||||
IoUtil.close(statement);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user