用法

依赖关系

  • JDK >= 1.8
  • Maven >= 3.0

如何仅打包jdbc项目

在根目录中:

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

如何在本地Maven存储库中安装

在根目录中:

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

结合使用IoTDB JDBC和Maven

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.iotdb</groupId>
  4. <artifactId>iotdb-jdbc</artifactId>
  5. <version>0.8.2</version>
  6. </dependency>
  7. </dependencies>

例子

本章提供了有关如何打开数据库连接,执行SQL查询以及显示结果的示例。

要求您包含包含数据库编程所需的JDBC类的软件包。

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