diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java index a8d6ceb713136dd2a25b92de286daac86e01fa09..5c6b0545e91a13b793478efa1ec687e616d1a9ef 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java @@ -242,7 +242,7 @@ public class TSDBStatement implements Statement { public void addBatch(String sql) throws SQLException { if (batchedArgs == null) { - batchedArgs = new ArrayList(); + batchedArgs = new ArrayList<>(); } batchedArgs.add(sql); } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java index 039250dd96e8e224f2d039d08d515cabb6d33223..0d0e7ac067600390be57b61ea0e4a4e49cf7e27c 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java @@ -7,17 +7,35 @@ import org.apache.calcite.sql.parser.SqlParseException; import org.apache.commons.dbcp2.BasicDataSource; import java.sql.*; -import java.util.HashMap; -import java.util.Map; import java.util.Properties; public class CalciteTest { public static void main(String[] args) throws SqlParseException, ClassNotFoundException, SQLException { +// CalciteConnection calciteConnection = testMyqsl(); + CalciteConnection calciteConnection = testTSDB(); + + Statement statement = calciteConnection.createStatement(); + ResultSet resultSet = statement.executeQuery("select * from test.t"); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + for (int i = 1; i <= metaData.getColumnCount(); i++) { + String columnLabel = metaData.getColumnLabel(i); + System.out.println(columnLabel + " : " + resultSet.getString(i)); + } + } + resultSet.close(); + statement.close(); + calciteConnection.close(); + } + + private static CalciteConnection testMyqsl() throws ClassNotFoundException, SQLException { //创建Calcite Connection对象 Class.forName("org.apache.calcite.jdbc.Driver"); Properties info = new Properties(); + info.setProperty("lex", "JAVA"); info.setProperty("caseSensitive", "false"); Connection connection = DriverManager.getConnection("jdbc:calcite:", info); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); @@ -26,55 +44,34 @@ public class CalciteTest { // JDBC adapter Class.forName("com.mysql.jdbc.Driver"); BasicDataSource dataSource = new BasicDataSource(); - dataSource.setUrl("jdbc:mysql://192.168.56.101:3306"); + dataSource.setUrl("jdbc:mysql://192.168.236.135:3306/test"); dataSource.setUsername("root"); dataSource.setPassword("123456"); - Map map = new HashMap<>(); - JdbcSchema schema = JdbcSchema.create(rootSchema, "hr", dataSource, null, null); - rootSchema.add("hr", schema); - - Statement statement = calciteConnection.createStatement(); - ResultSet resultSet = statement.executeQuery("select * from hr.depts"); - - while (resultSet.next()) { - ResultSetMetaData metaData = resultSet.getMetaData(); - for (int i = 1; i <= metaData.getColumnCount(); i++) { - String columnLabel = metaData.getColumnLabel(i); - System.out.println(columnLabel + " : " + resultSet.getString(i)); - } - } - resultSet.close(); - statement.close(); - connection.close(); + JdbcSchema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test"); + rootSchema.add("test", schema); + return calciteConnection; + } - //创建TDengine的数据源schema -// Class.forName("com.taosdata.jdbc.TSDBDriver"); -// String url = "jdbc:TAOS://127.0.0.1:6030/hdb"; -// dataSource.setUrl(url); -// dataSource.setUsername("root"); -// dataSource.setPassword("taosdata"); + private static CalciteConnection testTSDB() throws SQLException, ClassNotFoundException { + //创建Calcite Connection对象 + Class.forName("org.apache.calcite.jdbc.Driver"); + Properties info = new Properties(); + info.setProperty("lex", "JAVA"); + info.setProperty("caseSensitive", "false"); + Connection connection = DriverManager.getConnection("jdbc:calcite:", info); + CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); + SchemaPlus rootSchema = calciteConnection.getRootSchema(); -// Class.forName("com.mysql.jdbc.Driver"); -// String url = "jdbc:mysql://localhost:3306/hdb"; -// BasicDataSource dataSource = new BasicDataSource(); -// dataSource.setUrl(url); -// dataSource.setUsername("root"); -// dataSource.setPassword("123456"); - //这里hdb是在tdengine中创建的数据库名 -// JdbcSchema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test"); -// Schema schema = JdbcSchema.create(rootSchema, "test", dataSource, "hdb", null); - //创建新的schema自动映射到原来的hdb数据库 -// rootSchema.add("test", schema); + // JDBC adapter + Class.forName("com.taosdata.jdbc.TSDBDriver"); + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setUrl("jdbc:TAOS://192.168.236.135:6030/test"); + dataSource.setUsername("root"); + dataSource.setPassword("taosdata"); + JdbcSchema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test"); + rootSchema.add("test", schema); -// Statement stmt = calciteConnection.createStatement(); - //查询schema test中的表,表名是tdengine中的表 -// ResultSet rs = stmt.executeQuery("select * from test.t"); -// for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { -// System.out.println(rs.getMetaData().getColumnName(i)); -// } -// while (rs.next()) { -// System.out.println(rs.getObject(1)); -// } + return calciteConnection; } }