.
This commit is contained in:
@@ -1,81 +0,0 @@
|
||||
package net.tccn.dbq.jdbc;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/10/11 17:47.
|
||||
*/
|
||||
public class JdbcAccount {
|
||||
|
||||
private String url;
|
||||
private String user;
|
||||
private String pwd;
|
||||
private String cate;//数据库类型
|
||||
private Integer connectMax = 5;//默认最大连接数5
|
||||
|
||||
/*public JdbcAccount() {
|
||||
}*/
|
||||
|
||||
public JdbcAccount(String url, String user, String pwd) {
|
||||
this.url = url;
|
||||
this.user = user;
|
||||
this.pwd = pwd;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getPwd() {
|
||||
return pwd;
|
||||
}
|
||||
|
||||
public void setPwd(String pwd) {
|
||||
this.pwd = pwd;
|
||||
}
|
||||
|
||||
public String getCate() {
|
||||
return cate;
|
||||
}
|
||||
|
||||
public void setCate(String cate) {
|
||||
this.cate = cate;
|
||||
}
|
||||
|
||||
public Integer getConnectMax() {
|
||||
return connectMax;
|
||||
}
|
||||
|
||||
public void setConnectMax(Integer connectMax) {
|
||||
this.connectMax = connectMax;
|
||||
}
|
||||
//-------------------------------------------------------
|
||||
|
||||
public String parse() {
|
||||
int start = url.indexOf("//") + 2;
|
||||
int end = url.indexOf("/", start);
|
||||
int endDef = url.indexOf("?", end);
|
||||
if (endDef == -1) {
|
||||
endDef = url.length();
|
||||
}
|
||||
String host = url.substring(start, end == -1 ? url.length() : end);
|
||||
return user + "@" + host;
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
package net.tccn.dbq.jdbc;
|
||||
|
||||
import net.tccn.dbq.table.Table;
|
||||
import org.redkale.util.Comment;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/**
|
||||
* JdbcService.
|
||||
*
|
||||
* @author: liangxianyou at 2018/10/8 10:39.
|
||||
*/
|
||||
public class JdbcService {
|
||||
|
||||
private JdbcAccount account;
|
||||
|
||||
private static ConcurrentHashMap<JdbcAccount, Connection> jdbcPool = new ConcurrentHashMap<>();
|
||||
|
||||
public JdbcService(JdbcAccount account) {
|
||||
this.account = account;
|
||||
}
|
||||
//-------------- query -----------------
|
||||
private <R> List<R> executeQuery(String sql, Map columns, BiFunction<ResultSet, Map, R> fun) throws SQLException {
|
||||
try (
|
||||
Connection connection = DriverManager.getConnection(account.getUrl(), account.getUser(), account.getPwd());
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
ResultSet resultSet = ps.executeQuery()) {
|
||||
List list = new ArrayList();
|
||||
while (resultSet.next()) {
|
||||
list.add(fun.apply(resultSet, columns));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> getCatalogs(Connection connection) throws SQLException {
|
||||
List<String> catalogs = new ArrayList<>();
|
||||
try (
|
||||
PreparedStatement ps = connection.prepareStatement("show databases;");
|
||||
ResultSet rs = ps.executeQuery()
|
||||
) {
|
||||
while (rs.next()) {
|
||||
catalogs.add(rs.getString(1));
|
||||
}
|
||||
}
|
||||
return catalogs;
|
||||
}
|
||||
|
||||
public static Connection getConnection(JdbcAccount account) throws SQLException {
|
||||
Connection connection = jdbcPool.get(account);
|
||||
if (connection == null) {
|
||||
connection = DriverManager.getConnection(account.getUrl(), account.getUser(), account.getPwd());
|
||||
jdbcPool.put(account, connection);
|
||||
}
|
||||
|
||||
int i = 8000 * 12;//96000;
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Comment("列表数据查询")
|
||||
public static List<Map> findList(Connection connection, String sql) throws SQLException {
|
||||
try (
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
List list = new ArrayList();
|
||||
while (rs.next()) {
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int count = metaData.getColumnCount();
|
||||
|
||||
Map row = new HashMap();
|
||||
for (int i = 1; i <= count; i++) {
|
||||
String columnTypeName = metaData.getColumnTypeName(i);
|
||||
String columnName = metaData.getColumnName(i);
|
||||
row.put(columnName, null);
|
||||
|
||||
if (rs.getObject(i) != null) {
|
||||
switch (columnTypeName) {
|
||||
case "DATETIME":
|
||||
case "TIMESTAMP":
|
||||
case "DATE":
|
||||
row.put(columnName, rs.getTimestamp(i).getTime()); break;
|
||||
default:
|
||||
row.put(columnName, rs.getObject(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
list.add(row);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
@Comment("统计总数")
|
||||
public static int findNumber(Connection connection, String sql) throws SQLException {
|
||||
try (
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
|
||||
rs.next();
|
||||
return rs.getInt(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int update(Connection connection, String sql) throws SQLException {
|
||||
try (
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
){
|
||||
return ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- DDL ---------------------
|
||||
public static boolean createTable(Connection connection, Table table) throws SQLException {
|
||||
if (table.getCatalog() != null && table.getCatalog().length() > 0) {
|
||||
connection.setCatalog(table.getCatalog());
|
||||
}
|
||||
|
||||
String tableDdl = ""; // table.getTableDdl();
|
||||
System.out.println(tableDdl);
|
||||
try (PreparedStatement ps = connection.prepareStatement(tableDdl)) {
|
||||
return ps.execute();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
String url = "jdbc:mysql://192.168.202.11:3306/gxbii_dev";
|
||||
String user = "root";
|
||||
String pwd = "eversec123098";
|
||||
|
||||
JdbcAccount account = new JdbcAccount(url, user, pwd);
|
||||
|
||||
Connection connection = account.getConnection();
|
||||
|
||||
System.out.println(connection);
|
||||
|
||||
|
||||
String sql = "select * from basic_concat limit 10";
|
||||
List<Map> list = JdbcService.findList(connection, sql);
|
||||
System.out.println(list);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,196 +0,0 @@
|
||||
package net.tccn.dbq.jdbc;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/12/21 17:42.
|
||||
*/
|
||||
public class JdbcSource {
|
||||
|
||||
private JdbcAccount account;
|
||||
private static ConcurrentHashMap<String, JdbcSource> sources = new ConcurrentHashMap<>();
|
||||
private List<Connection> connections = new ArrayList<>();
|
||||
private AtomicInteger connectNum = new AtomicInteger();
|
||||
|
||||
private JdbcSource() {
|
||||
}
|
||||
public JdbcSource(JdbcAccount account) {
|
||||
String key = account.parse();
|
||||
synchronized (sources) {
|
||||
JdbcSource source = sources.get(key);
|
||||
if (source == null) {
|
||||
source = new JdbcSource();
|
||||
}
|
||||
source.account = account;
|
||||
|
||||
do {
|
||||
try {
|
||||
Connection connection = DriverManager.getConnection(account.getUrl(), account.getUser(), account.getPwd());
|
||||
connections.add(connection);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
} while (connectNum.incrementAndGet() < 2);//默认初始化连接数 2
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取连接
|
||||
* @return
|
||||
*/
|
||||
private Connection getConnection() {
|
||||
synchronized (connections) {
|
||||
//有闲置连接,直接返回
|
||||
if (connections.size() > 0) {
|
||||
return connections.remove(0);
|
||||
}
|
||||
|
||||
//没有闲置连接,总连接数小于最大连接数,创建新连接
|
||||
if (connectNum.get() < account.getConnectMax()) {
|
||||
try {
|
||||
return DriverManager.getConnection(account.getUrl(), account.getUser(), account.getPwd());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//已达最大连接,且没有闲置,等待
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放连接
|
||||
* @param connection
|
||||
*/
|
||||
private void releaseConnection(Connection connection) {
|
||||
connections.add(connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过Account 获取连接
|
||||
* 同一个连接对象同一时刻只能被一个线程所使用,
|
||||
* 两种方案:
|
||||
* 1、多个连接对象,每次线程得到对应的连接,用完返还连接,
|
||||
* 2、一到多个连接,每次数据库操作交给执行队列执行,并返回执行结果
|
||||
* @param account
|
||||
* @return
|
||||
*/
|
||||
/*public Connection getConnection(JdbcAccount account) {
|
||||
String key = account.parse();
|
||||
|
||||
List<Connection> conns = sources.get(key);
|
||||
if (conns == null) {
|
||||
conns = new ArrayList<>();
|
||||
}
|
||||
if (conns.size() == 0) {
|
||||
try {
|
||||
Connection connection = DriverManager.getConnection(account.getUrl(), account.getUser(), account.getPwd());
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}*/
|
||||
|
||||
|
||||
private Function<ResultSet, Map> dataToMap = (rs) -> {
|
||||
Map row = new HashMap();
|
||||
try {
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int count = metaData.getColumnCount();
|
||||
|
||||
for (int i = 1; i <= count; i++) {
|
||||
String columnTypeName = metaData.getColumnTypeName(i);
|
||||
String columnName = metaData.getColumnName(i);
|
||||
row.put(columnName, null);
|
||||
|
||||
if (rs.getObject(i) != null) {
|
||||
switch (columnTypeName) {
|
||||
case "DATETIME":
|
||||
case "TIMESTAMP":
|
||||
case "DATE":
|
||||
row.put(columnName, rs.getTimestamp(i).getTime()); break;
|
||||
default:
|
||||
row.put(columnName, rs.getObject(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return row;
|
||||
};
|
||||
|
||||
private Function<ResultSet, List<Map>> dataToList = (rs) -> {
|
||||
List list = new ArrayList<>();
|
||||
try {
|
||||
while (rs.next()) {
|
||||
list.add(dataToMap.apply(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
};
|
||||
|
||||
/**
|
||||
* 通过sql查询 数据,
|
||||
* 将每次查询任务加入到查询队列
|
||||
* @param sql
|
||||
* @return
|
||||
*/
|
||||
private Queue<String> finds = new LinkedBlockingQueue<>();
|
||||
public CompletableFuture<List<Map>> find(String sql) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
List<Map> list = null;
|
||||
Connection connection = getConnection();
|
||||
try (
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
) {
|
||||
list = dataToList.apply(rs);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
releaseConnection(connection);
|
||||
return list;
|
||||
});
|
||||
|
||||
/*
|
||||
Connection connection = getConnection(null);
|
||||
|
||||
|
||||
try (
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
) {
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
*/
|
||||
}
|
||||
|
||||
private void find(String sql, int cate) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user