org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback Java Examples

The following examples show how to use org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JdbcBookRepository.java    From spring-boot with MIT License 6 votes vote down vote up
@Override
public void saveImage(Long bookId, File image) {

    try (InputStream imageInStream = new FileInputStream(image)) {

        jdbcTemplate.execute(
                "INSERT INTO book_image (book_id, filename, blob_image) VALUES (?, ?, ?)",
                new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
                    protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                        ps.setLong(1, 1L);
                        ps.setString(2, image.getName());
                        lobCreator.setBlobAsBinaryStream(ps, 3, imageInStream, (int) image.length());
                    }
                }
        );

    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example #2
Source File: DefaultImageDatabase.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Transactional
public void storeImage(
    final String name, final InputStream contentStream, final int contentLength, final String description)
    throws DataAccessException {

	getJdbcTemplate().execute(
			"INSERT INTO imagedb (image_name, content, description) VALUES (?, ?, ?)",
			new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {
				protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
					ps.setString(1, name);
					lobCreator.setBlobAsBinaryStream(ps, 2, contentStream, contentLength);
					lobCreator.setClobAsString(ps, 3, description);
				}
			}
	);
}
 
Example #3
Source File: SpringJdbcAccess.java    From snakerflow with Apache License 2.0 6 votes vote down vote up
public void saveProcess(final Process process) {
	super.saveProcess(process);
	if(process.getBytes() != null) {
		template.execute(PROCESS_UPDATE_BLOB, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
			
			protected void setValues(PreparedStatement ps, LobCreator lobCreator)
					throws SQLException, DataAccessException {
				try {
					lobCreator.setBlobAsBytes(ps, 1, process.getBytes());
					StatementCreatorUtils.setParameterValue(ps, 2, Types.VARCHAR, process.getId());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
 
Example #4
Source File: SpringJdbcAccess.java    From snakerflow with Apache License 2.0 6 votes vote down vote up
public void updateProcess(final Process process) {
	super.updateProcess(process);
	if(process.getBytes() != null) {
		template.execute(PROCESS_UPDATE_BLOB, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
			
			protected void setValues(PreparedStatement ps, LobCreator lobCreator)
					throws SQLException, DataAccessException {
				try {
					lobCreator.setBlobAsBytes(ps, 1, process.getBytes());
					StatementCreatorUtils.setParameterValue(ps, 2, Types.VARCHAR, process.getId());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
 
Example #5
Source File: FileUploadRepository.java    From alf.io with GNU General Public License v3.0 6 votes vote down vote up
default void upload(UploadBase64FileModification file, String digest, Map<String, String> attributes) {
    LobHandler lobHandler = new DefaultLobHandler();

    NamedParameterJdbcTemplate jdbc = getNamedParameterJdbcTemplate();

    jdbc.getJdbcOperations().execute("insert into file_blob (id, name, content_size, content, content_type, attributes) values(?, ?, ?, ?, ?, ?)",
        new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
            @Override
            protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                ps.setString(1, digest);
                ps.setString(2, file.getName());
                ps.setLong(3, file.getFile().length);
                lobCreator.setBlobAsBytes(ps, 4, file.getFile());
                ps.setString(5, file.getType());
                ps.setString(6, Json.GSON.toJson(attributes));
            }
        });
}
 
Example #6
Source File: UploadedResourceRepository.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
default int upload(Integer organizationId, Integer eventId, UploadBase64FileModification file, Map<String, String> attributes) {

        LobHandler lobHandler = new DefaultLobHandler();

        String query = "insert into resource_global (name, content_size, content, content_type, attributes) values(?, ?, ?, ?, ?)";
        if (organizationId != null && eventId != null) {
            query = "insert into resource_event (name, content_size, content, content_type, attributes, organization_id_fk, event_id_fk) values(?, ?, ?, ?, ?, ?, ?)";
        } else if(organizationId != null) {
            query = "insert into resource_organizer (name, content_size, content, content_type, attributes, organization_id_fk) values(?, ?, ?, ?, ?, ?)";
        }

        return getNamedParameterJdbcTemplate().getJdbcOperations().execute(query,
            new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
                @Override
                protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                    ps.setString(1, file.getName());
                    ps.setLong(2, file.getFile().length);
                    lobCreator.setBlobAsBytes(ps, 3, file.getFile());
                    ps.setString(4, file.getType());
                    ps.setString(5, Json.GSON.toJson(attributes));
                    if (organizationId != null) {
                        ps.setInt(6, organizationId);
                    }
                    if (eventId != null) {
                        ps.setInt(7, eventId);
                    }
                }
            }
        );
    }
 
Example #7
Source File: TestKit.java    From r2dbc-spi with Apache License 2.0 4 votes vote down vote up
@Test
default void clobSelect() {
    getJdbcOperations().execute("INSERT INTO clob_test VALUES (?)", new AbstractLobCreatingPreparedStatementCallback(new DefaultLobHandler()) {

        @Override
        protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
            lobCreator.setClobAsString(ps, 1, "test-value");
        }

    });

    // CLOB defaults to String
    Mono.from(getConnectionFactory().create())
        .flatMapMany(connection -> Flux.from(connection

            .createStatement("SELECT * from clob_test")
            .execute())
            .flatMap(result -> result
                .map((row, rowMetadata) -> row.get("value")))

            .concatWith(close(connection)))
        .as(StepVerifier::create)
        .expectNext("test-value").as("value from select")
        .verifyComplete();

    // CLOB consume as Clob
    Mono.from(getConnectionFactory().create())
        .flatMapMany(connection -> Flux.from(connection

            .createStatement("SELECT * from clob_test")
            .execute())
            .flatMap(result -> result
                .map((row, rowMetadata) -> row.get("value", Clob.class)))
            .flatMap(clob -> Flux.from(clob.stream())
                .reduce(new StringBuilder(), StringBuilder::append)
                .map(StringBuilder::toString)
                .concatWith(discard(clob)))

            .concatWith(close(connection)))
        .as(StepVerifier::create)
        .expectNext("test-value").as("value from select")
        .verifyComplete();
}