更新时间:2024-10-21 GMT+08:00
分享

查询Doris表数据

本章节介绍查询Doris表数据样例代码。

以下代码片段在“JDBCExample”类中。

以Java JDBC方式执行SQl语句查询集群中的dbName.tableName表数据。

String querySql = "select * from " + dbName + "." + tableName + " limit 10";
private static void query(Connection connection, String sql) throws Exception {
   try (Statement stmt = connection.createStatement();
         ResultSet resultSet = stmt.executeQuery(sql)) {
      ResultSetMetaData md = resultSet.getMetaData();
      int columnCount = md.getColumnCount();
      StringBuffer stringBuffer = new StringBuffer();
      logger.info("Start to print query result.");
      for (int i = 1; i <= columnCount; i++) {
         stringBuffer.append(md.getColumnName(i));
         stringBuffer.append("  ");
      }
      logger.info(stringBuffer.toString());

      while (resultSet.next()) {
         stringBuffer = new StringBuffer();
         for (int i = 1; i <= columnCount; i++) {
            stringBuffer.append(resultSet.getObject(i));
            stringBuffer.append("  ");
         }
         logger.info(stringBuffer.toString());
      }
   } catch (Exception e) {
      logger.error("Execute sql {} failed.", sql, e);
      throw new Exception(e);
   }
}

相关文档