• JDK >= 1.8
    • Maven >= 3.0

    mvn clean install -pl jdbc -am -Dmaven.test.skip=true

    Requires that you include the packages containing the JDBC classes needed for database programming.

    1. import org.apache.iotdb.jdbc.IoTDBSQLException;
    2. /**
    3. * Before executing a SQL statement with a Statement object, you need to create a Statement object using the createStatement() method of the Connection object.
    4. * After creating a Statement object, you can use its execute() method to execute a SQL statement
    5. * Finally, remember to close the 'statement' and 'connection' objects by using their close() method
    6. * For statements with query results, we can use the getResultSet() method of the Statement object to get the result set.
    7. */
    8. public static void main(String[] args) throws SQLException {
    9. Connection connection = getConnection();
    10. if (connection == null) {
    11. System.out.println("get connection defeat");
    12. return;
    13. }
    14. Statement statement = connection.createStatement();
    15. //Create storage group
    16. try {
    17. statement.execute("SET STORAGE GROUP TO root.demo");
    18. }catch (IoTDBSQLException e){
    19. System.out.println(e.getMessage());
    20. }
    21. //Show storage group
    22. statement.execute("SHOW STORAGE GROUP");
    23. outputResult(statement.getResultSet());
    24. //Create time series
    25. //Different data type has different encoding methods. Here use INT32 as an example
    26. try {
    27. statement.execute("CREATE TIMESERIES root.demo.s0 WITH DATATYPE=INT32,ENCODING=RLE;");
    28. }catch (IoTDBSQLException e){
    29. System.out.println(e.getMessage());
    30. }
    31. //Show time series
    32. statement.execute("SHOW TIMESERIES root.demo");
    33. //Execute insert statements in batch
    34. statement.addBatch("insert into root.demo(timestamp,s0) values(1,1);");
    35. statement.addBatch("insert into root.demo(timestamp,s0) values(1,1);");
    36. statement.addBatch("insert into root.demo(timestamp,s0) values(2,17);");
    37. statement.addBatch("insert into root.demo(timestamp,s0) values(4,12);");
    38. statement.executeBatch();
    39. statement.clearBatch();
    40. //Full query statement
    41. String sql = "select * from root.demo";
    42. ResultSet resultSet = statement.executeQuery(sql);
    43. System.out.println("sql: " + sql);
    44. outputResult(resultSet);
    45. //Exact query statement
    46. sql = "select s0 from root.demo where time = 4;";
    47. resultSet= statement.executeQuery(sql);
    48. System.out.println("sql: " + sql);
    49. outputResult(resultSet);
    50. //Time range query
    51. sql = "select s0 from root.demo where time >= 2 and time < 5;";
    52. resultSet = statement.executeQuery(sql);
    53. System.out.println("sql: " + sql);
    54. outputResult(resultSet);
    55. //Aggregate query
    56. sql = "select count(s0) from root.demo;";
    57. resultSet = statement.executeQuery(sql);
    58. System.out.println("sql: " + sql);
    59. outputResult(resultSet);
    60. //Delete time series
    61. statement.execute("delete timeseries root.demo.s0");
    62. //close connection
    63. statement.close();
    64. connection.close();
    65. }
    66. public static Connection getConnection() {
    67. // JDBC driver name and database URL
    68. String url = "jdbc:iotdb://127.0.0.1:6667/";
    69. // Database credentials
    70. String username = "root";
    71. String password = "root";
    72. Connection connection = null;
    73. try {
    74. Class.forName(driver);
    75. connection = DriverManager.getConnection(url, username, password);
    76. } catch (ClassNotFoundException e) {
    77. e.printStackTrace();
    78. } catch (SQLException e) {
    79. e.printStackTrace();
    80. }
    81. return connection;
    82. }
    83. /**
    84. * This is an example of outputting the results in the ResultSet
    85. */
    86. private static void outputResult(ResultSet resultSet) throws SQLException {
    87. if (resultSet != null) {
    88. System.out.println("--------------------------");
    89. final ResultSetMetaData metaData = resultSet.getMetaData();
    90. final int columnCount = metaData.getColumnCount();
    91. for (int i = 0; i < columnCount; i++) {
    92. System.out.print(metaData.getColumnLabel(i + 1) + " ");
    93. }
    94. System.out.println();
    95. while (resultSet.next()) {
    96. for (int i = 1; ; i++) {
    97. System.out.print(resultSet.getString(i));
    98. if (i < columnCount) {
    99. System.out.print(", ");
    100. } else {
    101. System.out.println();
    102. break;
    103. }
    104. }
    105. }
    106. System.out.println("--------------------------\n");
    107. }