@Test public void testExecuteSqlQuery() throws Exception { sqlInputs.setDbType(ORACLE_DB_TYPE); sqlInputs.setDbPort(30); sqlInputs.setDbServer("localhost"); sqlInputs.setDbName("/dbName"); sqlInputs.setSqlCommand(SQL_QUERY); sqlInputs.setTimeout(SQL_QUERY_TIMEOUT); SQLQueryService.executeSqlQuery(sqlInputs); assertEquals("defaulLabel,defaulLabel,defaulLabel", sqlInputs.getStrColumns()); verify(connectionMock, Mockito.times(1)).setReadOnly(true); verify(statementMock, Mockito.times(1)).setQueryTimeout(SQL_QUERY_TIMEOUT); verify(statementMock, Mockito.times(1)).executeQuery(SQL_QUERY); }
public static void executeSqlQuery(@NotNull final SQLInputs sqlInputs) throws Exception { if (StringUtils.isEmpty(sqlInputs.getSqlCommand())) { throw new Exception("command input is empty."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { connection.setReadOnly(true); Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency()); statement.setQueryTimeout(sqlInputs.getTimeout()); final ResultSet results = statement.executeQuery(sqlInputs.getSqlCommand()); final ResultSetMetaData mtd = results.getMetaData(); int iNumCols = mtd.getColumnCount(); final StringBuilder strColumns = new StringBuilder(sqlInputs.getStrColumns()); for (int i = 1; i <= iNumCols; i++) { if (i > 1) { strColumns.append(sqlInputs.getStrDelim()); } strColumns.append(mtd.getColumnLabel(i)); } sqlInputs.setStrColumns(strColumns.toString()); while (results.next()) { final StringBuilder strRowHolder = new StringBuilder(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strRowHolder.append(sqlInputs.getStrDelim()); if (results.getString(i) != null) { String value = results.getString(i).trim(); if (sqlInputs.isNetcool()) value = SQLUtils.processNullTerminatedString(value); strRowHolder.append(value); } } sqlInputs.getLRows().add(strRowHolder.toString()); } } }
SQLQueryService { public static void executeSqlQuery(@NotNull final SQLInputs sqlInputs) throws Exception { if (StringUtils.isEmpty(sqlInputs.getSqlCommand())) { throw new Exception("command input is empty."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { connection.setReadOnly(true); Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency()); statement.setQueryTimeout(sqlInputs.getTimeout()); final ResultSet results = statement.executeQuery(sqlInputs.getSqlCommand()); final ResultSetMetaData mtd = results.getMetaData(); int iNumCols = mtd.getColumnCount(); final StringBuilder strColumns = new StringBuilder(sqlInputs.getStrColumns()); for (int i = 1; i <= iNumCols; i++) { if (i > 1) { strColumns.append(sqlInputs.getStrDelim()); } strColumns.append(mtd.getColumnLabel(i)); } sqlInputs.setStrColumns(strColumns.toString()); while (results.next()) { final StringBuilder strRowHolder = new StringBuilder(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strRowHolder.append(sqlInputs.getStrDelim()); if (results.getString(i) != null) { String value = results.getString(i).trim(); if (sqlInputs.isNetcool()) value = SQLUtils.processNullTerminatedString(value); strRowHolder.append(value); } } sqlInputs.getLRows().add(strRowHolder.toString()); } } } }
SQLQueryService { public static void executeSqlQuery(@NotNull final SQLInputs sqlInputs) throws Exception { if (StringUtils.isEmpty(sqlInputs.getSqlCommand())) { throw new Exception("command input is empty."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { connection.setReadOnly(true); Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency()); statement.setQueryTimeout(sqlInputs.getTimeout()); final ResultSet results = statement.executeQuery(sqlInputs.getSqlCommand()); final ResultSetMetaData mtd = results.getMetaData(); int iNumCols = mtd.getColumnCount(); final StringBuilder strColumns = new StringBuilder(sqlInputs.getStrColumns()); for (int i = 1; i <= iNumCols; i++) { if (i > 1) { strColumns.append(sqlInputs.getStrDelim()); } strColumns.append(mtd.getColumnLabel(i)); } sqlInputs.setStrColumns(strColumns.toString()); while (results.next()) { final StringBuilder strRowHolder = new StringBuilder(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strRowHolder.append(sqlInputs.getStrDelim()); if (results.getString(i) != null) { String value = results.getString(i).trim(); if (sqlInputs.isNetcool()) value = SQLUtils.processNullTerminatedString(value); strRowHolder.append(value); } } sqlInputs.getLRows().add(strRowHolder.toString()); } } }  }
SQLQueryService { public static void executeSqlQuery(@NotNull final SQLInputs sqlInputs) throws Exception { if (StringUtils.isEmpty(sqlInputs.getSqlCommand())) { throw new Exception("command input is empty."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { connection.setReadOnly(true); Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency()); statement.setQueryTimeout(sqlInputs.getTimeout()); final ResultSet results = statement.executeQuery(sqlInputs.getSqlCommand()); final ResultSetMetaData mtd = results.getMetaData(); int iNumCols = mtd.getColumnCount(); final StringBuilder strColumns = new StringBuilder(sqlInputs.getStrColumns()); for (int i = 1; i <= iNumCols; i++) { if (i > 1) { strColumns.append(sqlInputs.getStrDelim()); } strColumns.append(mtd.getColumnLabel(i)); } sqlInputs.setStrColumns(strColumns.toString()); while (results.next()) { final StringBuilder strRowHolder = new StringBuilder(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strRowHolder.append(sqlInputs.getStrDelim()); if (results.getString(i) != null) { String value = results.getString(i).trim(); if (sqlInputs.isNetcool()) value = SQLUtils.processNullTerminatedString(value); strRowHolder.append(value); } } sqlInputs.getLRows().add(strRowHolder.toString()); } } }  static void executeSqlQuery(@NotNull final SQLInputs sqlInputs); }
SQLQueryService { public static void executeSqlQuery(@NotNull final SQLInputs sqlInputs) throws Exception { if (StringUtils.isEmpty(sqlInputs.getSqlCommand())) { throw new Exception("command input is empty."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { connection.setReadOnly(true); Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency()); statement.setQueryTimeout(sqlInputs.getTimeout()); final ResultSet results = statement.executeQuery(sqlInputs.getSqlCommand()); final ResultSetMetaData mtd = results.getMetaData(); int iNumCols = mtd.getColumnCount(); final StringBuilder strColumns = new StringBuilder(sqlInputs.getStrColumns()); for (int i = 1; i <= iNumCols; i++) { if (i > 1) { strColumns.append(sqlInputs.getStrDelim()); } strColumns.append(mtd.getColumnLabel(i)); } sqlInputs.setStrColumns(strColumns.toString()); while (results.next()) { final StringBuilder strRowHolder = new StringBuilder(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strRowHolder.append(sqlInputs.getStrDelim()); if (results.getString(i) != null) { String value = results.getString(i).trim(); if (sqlInputs.isNetcool()) value = SQLUtils.processNullTerminatedString(value); strRowHolder.append(value); } } sqlInputs.getLRows().add(strRowHolder.toString()); } } }  static void executeSqlQuery(@NotNull final SQLInputs sqlInputs);  }