List of usage examples for java.sql ResultSet getWarnings
SQLWarning getWarnings() throws SQLException;
ResultSet
object. From source file:org.apache.hive.jdbc.TestJdbcDriver2.java
private void doTestSelectAll(String tableName, int maxRows, int fetchSize) throws Exception { boolean isPartitionTable = tableName.equals(partitionedTableName); Statement stmt = con.createStatement(); if (maxRows >= 0) { stmt.setMaxRows(maxRows);/* w w w. j a v a 2 s .co m*/ } if (fetchSize > 0) { stmt.setFetchSize(fetchSize); assertEquals(fetchSize, stmt.getFetchSize()); } // JDBC says that 0 means return all, which is the default int expectedMaxRows = maxRows < 1 ? 0 : maxRows; assertNotNull("Statement is null", stmt); assertEquals("Statement max rows not as expected", expectedMaxRows, stmt.getMaxRows()); assertFalse("Statement should not be closed", stmt.isClosed()); ResultSet res; // run some queries res = stmt.executeQuery("select * from " + tableName); assertNotNull("ResultSet is null", res); assertTrue("getResultSet() not returning expected ResultSet", res == stmt.getResultSet()); assertEquals("get update count not as expected", -1, stmt.getUpdateCount()); int i = 0; ResultSetMetaData meta = res.getMetaData(); int expectedColCount = isPartitionTable ? 3 : 2; assertEquals("Unexpected column count", expectedColCount, meta.getColumnCount()); boolean moreRow = res.next(); while (moreRow) { try { i++; assertEquals(res.getInt(1), res.getInt(tableName + ".under_col")); assertEquals(res.getInt(1), res.getInt("under_col")); assertEquals(res.getString(1), res.getString(tableName + ".under_col")); assertEquals(res.getString(1), res.getString("under_col")); assertEquals(res.getString(2), res.getString(tableName + ".value")); assertEquals(res.getString(2), res.getString("value")); if (isPartitionTable) { assertEquals(res.getString(3), partitionedColumnValue); assertEquals(res.getString(3), res.getString(partitionedColumnName)); assertEquals(res.getString(3), res.getString(tableName + "." + partitionedColumnName)); } assertFalse("Last result value was not null", res.wasNull()); assertNull("No warnings should be found on ResultSet", res.getWarnings()); res.clearWarnings(); // verifying that method is supported // System.out.println(res.getString(1) + " " + res.getString(2)); assertEquals("getInt and getString don't align for the same result value", String.valueOf(res.getInt(1)), res.getString(1)); assertEquals("Unexpected result found", "val_" + res.getString(1), res.getString(2)); moreRow = res.next(); } catch (SQLException e) { System.out.println(e.toString()); e.printStackTrace(); throw new Exception(e.toString()); } } // supposed to get 500 rows if maxRows isn't set int expectedRowCount = maxRows > 0 ? maxRows : 500; assertEquals("Incorrect number of rows returned", expectedRowCount, i); // should have no more rows assertEquals(false, moreRow); assertNull("No warnings should be found on statement", stmt.getWarnings()); stmt.clearWarnings(); // verifying that method is supported assertNull("No warnings should be found on connection", con.getWarnings()); con.clearWarnings(); // verifying that method is supported stmt.close(); assertTrue("Statement should be closed", stmt.isClosed()); }
From source file:org.diffkit.util.DKSqlUtil.java
/** * does not close resultSet_//from www. j ava2s .c om */ public static List<Map<String, ?>> readRows(ResultSet resultSet_, boolean keysUpper_) throws SQLException { if (resultSet_ == null) return null; SQLWarning warnings = resultSet_.getWarnings(); if (warnings != null) { LOG.warn(null, warnings); return null; } String[] columnNames = getColumnNames(resultSet_); if (columnNames == null) { LOG.warn(String.format("no columnNames for resultSet_->%s", resultSet_)); return null; } List<Map<String, ?>> maps = new ArrayList<Map<String, ?>>(); while (resultSet_.next()) { Map<String, ?> map = getRowMap(columnNames, resultSet_, keysUpper_); LOG.debug("map->{}", map); maps.add(map); } if (maps.isEmpty()) return null; return maps; }
From source file:org.getobjects.eoaccess.EOAdaptorChannel.java
/** * A primary fetch method./*ww w . j av a 2s . co m*/ * <p> * Creates a PreparedStatement from the statement and the bindings of the * EOSQLExpression. * <p> * @param _sqlexpr - the EOSQLExpression to execute * @return the fetch results as a List of Maps */ public List<Map<String, Object>> evaluateQueryExpression(final EOSQLExpression _sqlexpr, final EOAttribute[] _optAttrs) { this.lastException = null; // System.err.println("\nEXEC: " + _s.statement()); if (_sqlexpr == null) { log.error("evaluateQueryExpression() caller gave us no SQL ..."); return null; } final List<Map<String, Object>> binds = _sqlexpr.bindVariableDictionaries(); if (binds == null || binds.size() == 0) /* expression has no binds, perform a plain SQL query */ return this.performSQL(_sqlexpr.statement(), _optAttrs); /* otherwise, create a PreparedStatement */ final PreparedStatement stmt = this._prepareStatementWithBinds(_sqlexpr.statement(), binds); if (stmt == null) { log.error("could not create prepared statement for expr: " + _sqlexpr); return null; } /* perform query */ this.lastException = null; List<Map<String, Object>> records = null; ResultSet rs = null; try { if (sqllog.isInfoEnabled()) sqllog.info(_sqlexpr.statement()); rs = stmt.executeQuery(); SQLWarning warning = rs.getWarnings(); if (warning != null) { // TBD: find out when this happens log.warn("detected SQL warning: " + warning); } /* Collect meta data, calling meta inside fetches is rather expensive, * even though the PG JDBC adaptor also has some cache. */ final ResultSetMetaData meta = rs.getMetaData(); final int columnCount = meta.getColumnCount(); final String[] colNames = new String[columnCount]; final int[] colHashes = new int[columnCount]; final int[] colTypes = new int[columnCount]; for (int i = 1; i <= columnCount; i++) { if (_optAttrs != null) colNames[i - 1] = _optAttrs[i - 1].columnName(); else colNames[i - 1] = meta.getColumnName(i); colHashes[i - 1] = colNames[i - 1].hashCode(); colTypes[i - 1] = meta.getColumnType(i); } /* loop over results and convert them to records */ records = new ArrayList<Map<String, Object>>(128); while (rs.next()) { final EORecordMap record = new EORecordMap(colNames, colHashes); boolean ok = this.fillRecordMapFromResultSet(record, rs, colNames, colTypes); if (ok) records.add(record); } } catch (SQLException e) { /* * getSQLState() * 08S01 MySQL network-connect issues during the processing of a query * 42601 PG syntax error * 42703 PG column "number" does not exist * 22023 PG No value specified for parameter 3 (eg multiple %andQual) */ this.lastException = e; if (records != null && records.size() == 0) { records = null; if (log.isInfoEnabled()) { log.info("could not execute SQL expression " + e.getSQLState() + ":\n " + _sqlexpr.statement(), e); } // System.err.println("STATE: " + e.getSQLState()); } else { log.warn("could not execute SQL expression " + e.getSQLState() + ":\n " + _sqlexpr.statement(), e); } } finally { // TODO: we might also want to close our channel if the tear down was not // clean this._releaseResources(stmt, rs); } return records; }
From source file:org.getobjects.eoaccess.EOAdaptorChannel.java
/** * Executes the SQL string and returns a Map containing the results of the * SQL./*from w w w . j av a 2s . com*/ * <p> * If the SQL string is empty, an error is set and null is returned. * * @return null on error (check lastException), or the fetch results */ public List<Map<String, Object>> performSQL(final String _sql, final EOAttribute[] _optAttrs) { if (_sql == null || _sql.length() == 0) { log.error("performSQL caller gave us no SQL ..."); this.lastException = new Exception("got no SQL to perform!"); return null; } this.lastException = null; /* acquire DB resources */ final Statement stmt = this._createStatement(); if (stmt == null) return null; /* perform query */ ArrayList<Map<String, Object>> records = null; ResultSet rs = null; try { if (sqllog.isInfoEnabled()) sqllog.info(_sql); rs = stmt.executeQuery(_sql); SQLWarning warning = rs.getWarnings(); if (warning != null) { // TBD: find out when this happens log.warn("detected SQL warning: " + warning); } /* Collect meta data, calling meta inside fetches is rather expensive, * even though the PG JDBC adaptor also has some cache. */ final ResultSetMetaData meta = rs.getMetaData(); final int columnCount = meta.getColumnCount(); final String[] colNames = new String[columnCount]; final int[] colHashes = new int[columnCount]; final int[] colTypes = new int[columnCount]; for (int i = 1; i <= columnCount; i++) { if (_optAttrs != null) colNames[i - 1] = _optAttrs[i - 1].columnName(); else colNames[i - 1] = meta.getColumnName(i); colHashes[i - 1] = colNames[i - 1].hashCode(); colTypes[i - 1] = meta.getColumnType(i); } /* loop over results and convert them to records */ records = new ArrayList<Map<String, Object>>(128); while (rs.next()) { EORecordMap record = new EORecordMap(colNames, colHashes); boolean ok = this.fillRecordMapFromResultSet(record, rs, colNames, colTypes); if (ok) records.add(record); } } catch (SQLException e) { /* * SQLState: * 42601 - PostgreSQL for invalid SQL, like "SELECT *" or "IN ()" * 42804 - PostgreSQL for * IN types character varying and integer cannot be matched * 42P01 - PostgreSQL: relation 'notes' does not exist * 42703 - PostgreSQL: column "lastname" does not exist */ this.lastException = e; /* Note: if we already fetched records, we actually return them ... */ if (records != null && records.size() == 0) { records = null; if (log.isInfoEnabled()) { log.info("could not execute SQL statement (state=" + e.getSQLState() + "): " + _sql, e); } // System.err.println("STATE: " + e.getSQLState()); } else { log.warn("could not execute SQL statement (state=" + e.getSQLState() + "): " + _sql, e); } } finally { // TODO: we might also want to close our channel if the tear down was not // clean this._releaseResources(stmt, rs); } if (sqllog.isDebugEnabled()) sqllog.debug(" GOT RESULTS: " + records); /* compact array */ if (records != null) records.trimToSize(); return records; }