Refine search
/** * Executes the SQL as specified by {@link #getSequenceQuery()}. */ @Override protected long getNextKey() throws DataAccessException { Connection con = DataSourceUtils.getConnection(getDataSource()); Statement stmt = null; ResultSet rs = null; try { stmt = con.createStatement(); DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); rs = stmt.executeQuery(getSequenceQuery()); if (rs.next()) { return rs.getLong(1); } else { throw new DataAccessResourceFailureException("Sequence query did not return a result"); } } catch (SQLException ex) { throw new DataAccessResourceFailureException("Could not obtain sequence value", ex); } finally { JdbcUtils.closeResultSet(rs); JdbcUtils.closeStatement(stmt); DataSourceUtils.releaseConnection(con, getDataSource()); } }
private static boolean requiresTemporaryLob(DataSource dataSource) { try { String productName = JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName"); return "Oracle".equalsIgnoreCase(JdbcUtils.commonDatabaseName(productName)); } catch (MetaDataAccessException ex) { return false; } }
if (this.useNewConnection) { con = getDataSource().getConnection(); if (con.getAutoCommit()) { mustRestoreAutoCommit = true; con.setAutoCommit(false); con = DataSourceUtils.getConnection(getDataSource()); stmt = con.createStatement(); if (!this.useNewConnection) { DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); if (!rs.next()) { throw new DataAccessResourceFailureException("last_insert_id() failed after executing an update"); this.maxId = rs.getLong(1); JdbcUtils.closeResultSet(rs); JdbcUtils.closeStatement(stmt); if (con != null) { if (this.useNewConnection) { "Unable to commit new sequence value changes for " + getIncrementerName()); JdbcUtils.closeConnection(con);
public static void findCoffee(int coffeeId, String[] coffeeDescription) throws SQLException { Connection connection = null; PreparedStatement statement = null; try { connection = DriverManager.getConnection("jdbc:default:connection"); String sql = "SELECT * FROM COFFEE_BEVERAGES WHERE ID = ? "; statement = connection.prepareStatement(sql); statement.setLong(1, coffeeId); ResultSet resultset = statement.executeQuery(); resultset.next(); coffeeDescription[0] = resultset.getString("COFFEE_DESCRIPTION"); } finally { JdbcUtils.closeStatement(statement); JdbcUtils.closeConnection(connection); } }
"DataSource returned null from getConnection(): " + this.dataSource); stmt = con.createStatement(); stmt.execute(this.validationQuery); validated = true; JdbcUtils.closeStatement(stmt); JdbcUtils.closeConnection(con);
public static Object getResultSetValue(ResultSet rs, int index, @Nullable Class<?> requiredType) throws SQLException { if (requiredType == null) { return getResultSetValue(rs, index); return rs.getString(index); value = rs.getBoolean(index); value = rs.getByte(index); return getResultSetValue(rs, index);
tableColumns = databaseMetaData.getColumns( metaDataCatalogName, metaDataSchemaName, metaDataTableName, null); while (tableColumns.next()) { String columnName = tableColumns.getString("COLUMN_NAME"); int dataType = tableColumns.getInt("DATA_TYPE"); if (dataType == Types.DECIMAL) { String typeName = tableColumns.getString("TYPE_NAME"); JdbcUtils.closeResultSet(tableColumns);
tables = databaseMetaData.getTables( catalogNameToUse(catalogName), schemaNameToUse(schemaName), tableNameToUse(tableName), null); while (tables != null && tables.next()) { TableMetaData tmd = new TableMetaData(); tmd.setCatalogName(tables.getString("TABLE_CAT")); tmd.setSchemaName(tables.getString("TABLE_SCHEM")); tmd.setTableName(tables.getString("TABLE_NAME")); if (tmd.getSchemaName() == null) { JdbcUtils.closeResultSet(tables);
rs=(ResultSet)cs.getObject(oracleCursorIndex); ResultSetMetaData metadata=rs.getMetaData(); int columnCount=metadata.getColumnCount(); List<Map<String,Object>> result=new ArrayList<Map<String,Object>>(); while(rs.next()){ Map<String,Object> map=new HashMap<String,Object>(); for(int i=1;i<=columnCount;i++){ String columnName=metadata.getColumnLabel(i); map.put(columnName, rs.getObject(columnName)); throw new ReportException(e); }finally{ JdbcUtils.closeResultSet(rs); JdbcUtils.closeStatement(cs); JdbcUtils.closeConnection(conn);
try { DatabaseMetaData databaseMetaData = con.getMetaData(); tableData = metaData.getTables(convertTableName); while ((tableData != null) && tableData.next()) { Map<String, Object> values = readColumns(tableData, initColumnsForTable()); JdbcUtils.closeResultSet(tableData);
/** * Implementation of ResultSetCallbackHandler. * Work out column size if this is the first row, otherwise just count rows. * <p>Subclasses can perform custom extraction or processing * by overriding the {@code processRow(ResultSet, int)} method. * @see #processRow(java.sql.ResultSet, int) */ @Override public final void processRow(ResultSet rs) throws SQLException { if (this.rowCount == 0) { ResultSetMetaData rsmd = rs.getMetaData(); this.columnCount = rsmd.getColumnCount(); this.columnTypes = new int[this.columnCount]; this.columnNames = new String[this.columnCount]; for (int i = 0; i < this.columnCount; i++) { this.columnTypes[i] = rsmd.getColumnType(i + 1); this.columnNames[i] = JdbcUtils.lookupColumnName(rsmd, i + 1); } // could also get column names } processRow(rs, this.rowCount++); }
@Override @Nullable public <T> T execute(StatementCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); Connection con = DataSourceUtils.getConnection(obtainDataSource()); Statement stmt = null; try { stmt = con.createStatement(); applyStatementSettings(stmt); T result = action.doInStatement(stmt); handleWarnings(stmt); return result; } catch (SQLException ex) { // Release Connection early, to avoid potential connection pool deadlock // in the case when the exception translator hasn't been initialized yet. String sql = getSql(action); JdbcUtils.closeStatement(stmt); stmt = null; DataSourceUtils.releaseConnection(con, getDataSource()); con = null; throw translateException("StatementCallback", sql, ex); } finally { JdbcUtils.closeStatement(stmt); DataSourceUtils.releaseConnection(con, getDataSource()); } }
private static List<Column> readColumns(DatabaseMetaDataWrapper metaData, String tableName) throws SQLException { ResultSet columnData = null; try { columnData = metaData.getColumns(tableName, null); List<Column> columns = new ArrayList<Column>(); Map<String, Object> values; for (; columnData.next(); columns.add(readColumn(metaData, values))) { Map<String, Object> tmp = readColumns(columnData, initColumnsForColumn()); if (tableName.equalsIgnoreCase((String) tmp.get("TABLE_NAME"))) { values = tmp; } else { break; } } return columns; } finally { JdbcUtils.closeResultSet(columnData); } }
@Override protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws java.sql.SQLException { ResultSetMetaData metaData = rs.getMetaData(); String columnName = metaData.getColumnName(index).toLowerCase(); switch(columnName) { case "src_app_id": case "app_id": case "handler": return transform(rs, index, APP_ID); case "src_bus_code": case "bus_code": return transform(rs, index, BUSINESS_CODE); case "called_methods": return id2callMethodsString((String) JdbcUtils.getResultSetValue(rs, index, String.class)); case "md5": byte[] md5Bytes = (byte[]) JdbcUtils.getResultSetValue(rs, index, byte[].class); return ObjectDigestUtil.byteArrayToHexString(md5Bytes); default: return JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType()); } }; };
public static void findAllCoffeeBeverages(ResultSet[] coffeeBeverages) throws SQLException { Connection connection = null; PreparedStatement statement = null; try { connection = DriverManager.getConnection("jdbc:default:connection"); String sql = "SELECT * FROM COFFEE_BEVERAGES"; statement = connection.prepareStatement(sql);//NOSONAR see below coffeeBeverages[0] = statement.executeQuery(); } finally { // JdbcUtils.closeStatement(statement); // cannot close due to result set being returned JdbcUtils.closeConnection(connection); } } }
public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Bean bean = TinyDBUtil.getBeanInstance(beanType, schema); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); for (int index = 1; index <= columnCount; index++) { String columnName = JdbcUtils.lookupColumnName(rsmd, index) .toLowerCase(); String propertyName = converter .dbFieldNameToPropertyName(columnName); bean.put(propertyName, rs.getObject(columnName)); } return bean; }
public static void createUser(String username, String password, String email) throws SQLException { Connection conn = null; PreparedStatement stmt = null; try { conn = DriverManager.getConnection("jdbc:default:connection"); String sql = "INSERT INTO USERS " + "(USERNAME, PASSWORD, EMAIL) VALUES (?,?,?)"; stmt = conn.prepareStatement(sql); stmt.setString(1, username); stmt.setString(2, password); stmt.setString(3, email); stmt.executeUpdate(); } finally { JdbcUtils.closeStatement(stmt); JdbcUtils.closeConnection(conn); } }
/** * Parse the Database ProductName from the Connection and return the matching DatabaseType */ public static DatabaseType fromMetaData(Connection connection) throws MetaDataAccessException { String databaseProductName = null; try { databaseProductName = connection.getMetaData().getDatabaseProductName(); } catch (SQLException e) { throw new MetaDataAccessException(e.getMessage()); } if (databaseProductName != null) { databaseProductName = JdbcUtils.commonDatabaseName(databaseProductName); try { return fromProductName(databaseProductName); } catch (IllegalArgumentException e) { throw new MetaDataAccessException(e.getMessage()); } } else { throw new MetaDataAccessException("Database Type not found for connection"); } }
if (rowNum == 0) { if (StringUtils.isNotEmpty(keyColumn)) { keyColumnIndex = rs.findColumn(keyColumn); if (keyColumnIndex <= 0) { throw new IllegalArgumentException(String.format( Object key = JdbcUtils.getResultSetValue(rs, keyColumnIndex, keyType); if (key != null && !keyType.isInstance(key)) { ResultSetMetaData rsmd = rs.getMetaData(); throw new TypeMismatchDataAccessException( // NL "Type mismatch affecting row number " + rowNum + " and column type '"
BeanWrapper bw = new BeanWrapperImpl(mappedObject); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); String column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase(); PropertyDescriptor pd = this.mappedFields.get(column); if (pd != null) { try { Object value = JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType()); if (debugEnabled && rowNumber == 0) { logger.debug("Mapping column '" + column + "' to property '" + pd.getName()