import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @description:clickhouse连接测试
* @author: ZhiWen
* @create: 2021-01-05
**/
public class ClickhouseUse {
public static void main(String[] args) {
//todo 如果连接拒绝 在配置里打开外网访问 <listen_host>::</listen_host>
String sql = "select count(*) count from tb_user";
String address = "jdbc:clickhouse://172.24.103.4:8123/default";
Connection connection = null;
Statement statement = null;
ResultSet results = null;
try {
Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
connection = DriverManager.getConnection(address,"default","123456");
statement = connection.createStatement();
results = statement.executeQuery(sql);
ResultSetMetaData rsmd = results.getMetaData();
List<Map> list = new ArrayList();
while (results.next()) {
Map map = new HashMap();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
map.put(rsmd.getColumnName(i), results.getString(rsmd.getColumnName(i)));
}
list.add(map);
}
for (Map map : list) {
System.err.println(map);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (results != null) {
results.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*create table tb_user(
uid Int32,
name String,
age UInt32,
gender String
)engine = TinyLog;*/
}