public static void main(String[] args) throws Exception{
// 测试 demo,未设置 ideaMaxTime(超过空闲时间释放jdbc连接)
while (true){
long start= System.currentTimeMillis();
Class.forName("org.apache.calcite.jdbc.Driver");
Properties info= new Properties();
info.setProperty("lex", "JAVA");
Connection connection= DriverManager.getConnection("jdbc:calcite:", info);
CalciteConnection calciteConnection= connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema= calciteConnection.getRootSchema();
//db1
Class.forName("com.mysql.cj.jdbc.Driver");
BasicDataSource ds1= new BasicDataSource();
ds1.setUrl("jdbc:mysql://localhost:3310/fire_control_acceptance?useUnicode=true&serverTimezone=UTC&allowMultiQueries=true&characterEncoding=utf-8&useSSL=false&nullNamePatternMatchesAll=true");
ds1.setUsername("root");
ds1.setPassword("winsion123");
Schema schema1= JdbcSchema.create(rootSchema, "fire_control_acceptance",
ds1, null, "fire_control_acceptance");
rootSchema.add("fire_control_acceptance", schema1);
//db2
Class.forName("com.mysql.cj.jdbc.Driver");
BasicDataSource ds2= new BasicDataSource();
ds2.setUrl("jdbc:mysql://localhost:3310/fire_maintenance?useUnicode=true&serverTimezone=UTC&allowMultiQueries=true&characterEncoding=utf-8&useSSL=false&nullNamePatternMatchesAll=true");
ds2.setUsername("root");
ds2.setPassword("winsion123");
Schema schema2= JdbcSchema.create(rootSchema, "fire_maintenance",
ds2, null, "fire_maintenance");
rootSchema.add("fire_maintenance", schema2);
Statement statement= calciteConnection.createStatement();
ResultSet resultSet= statement.executeQuery(
"select f.factory_id, fc.factory_name, f.name " +
"from fire_maintenance.cf_fac f " +
"join fire_control_acceptance.f_factory fc " +
"on f.factory_id = fc.factory_id");
// resultSet 转list
List list= new ArrayList();
int columnCount= resultSet.getMetaData().getColumnCount();
Map rowData;
while (resultSet.next()) {
rowData= new HashMap(columnCount);
for (int i= 1; i<= columnCount; i++) {
rowData.put(resultSet.getMetaData().getColumnName(i), resultSet.getObject(i));
}
list.add(rowData);
}
System.out.println(list);
resultSet.close();
statement.close();
connection.close();
long end= System.currentTimeMillis();
System.out.println(end- start);
}