Skip to content

Commit 39e5863

Browse files
authored
[blob-jdbcTemplate] insert blob using jdbcTemplate (#19020)
1 parent 7e96e6a commit 39e5863

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.jdbc.blob;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
@SpringBootApplication
6+
public class InsertBlobUsingJdbcTemplateApplication {
7+
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# DataSource Configuration
2+
spring.datasource.url=jdbc:h2:mem:testdb
3+
spring.datasource.driverClassName=org.h2.Driver
4+
spring.datasource.username=user
5+
spring.datasource.password= # Leave this empty
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE DOCUMENT
2+
(
3+
ID INT PRIMARY KEY,
4+
FILENAME VARCHAR(255) NOT NULL,
5+
DATA BLOB
6+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE DOCUMENT;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.baeldung.spring.jdbc.blob;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.InputStream;
7+
import java.nio.charset.StandardCharsets;
8+
import java.sql.Types;
9+
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.jdbc.core.JdbcTemplate;
14+
import org.springframework.jdbc.core.SqlParameterValue;
15+
import org.springframework.jdbc.core.support.SqlBinaryValue;
16+
import org.springframework.jdbc.core.support.SqlLobValue;
17+
import org.springframework.jdbc.support.lob.DefaultLobHandler;
18+
import org.springframework.test.context.TestPropertySource;
19+
import org.springframework.test.context.jdbc.Sql;
20+
21+
@Sql(value = "/com/baeldung/spring/jdbc/blob/create-document-table.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
22+
@Sql(value = "/com/baeldung/spring/jdbc/blob/drop-document-table.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
23+
@SpringBootTest(classes = InsertBlobUsingJdbcTemplateApplication.class)
24+
@TestPropertySource(locations = { "classpath:com/baeldung/spring/jdbc/blob/application.properties" })
25+
class InsertBlobUsingJdbcTemplateUnitTest {
26+
27+
@Autowired
28+
private JdbcTemplate jdbcTemplate;
29+
30+
private static final String CONTENT = "I am a very very long content.";
31+
32+
@Test
33+
void whenUsingSetBytes_thenCorrect() {
34+
byte[] bytes = CONTENT.getBytes(StandardCharsets.UTF_8);
35+
//@formatter:off
36+
jdbcTemplate.update("INSERT INTO DOCUMENT (ID, FILENAME, DATA) VALUES (?, ?, ?)",
37+
1,
38+
"bigfile.txt",
39+
bytes
40+
);
41+
//@formatter:on
42+
43+
byte[] stored = jdbcTemplate.queryForObject("SELECT DATA FROM DOCUMENT WHERE ID = 1", (rs, rowNum) -> rs.getBytes("data"));
44+
assertEquals(CONTENT, new String(stored, StandardCharsets.UTF_8));
45+
}
46+
47+
@Test
48+
void whenUsingSetBinaryStream_thenCorrect() {
49+
InputStream stream = new ByteArrayInputStream(CONTENT.getBytes(StandardCharsets.UTF_8));
50+
51+
//@formatter:off
52+
jdbcTemplate.update("INSERT INTO DOCUMENT (ID, FILENAME, DATA) VALUES (?, ?, ?)",
53+
2,
54+
"bigfile.txt",
55+
stream
56+
);
57+
//@formatter:on
58+
59+
byte[] stored = jdbcTemplate.queryForObject("SELECT DATA FROM DOCUMENT WHERE ID = 2", (rs, rowNum) -> rs.getBytes("data"));
60+
assertEquals(CONTENT, new String(stored, StandardCharsets.UTF_8));
61+
62+
}
63+
64+
@Test
65+
void whenUsingLobHandler_thenCorrect() {
66+
byte[] bytes = CONTENT.getBytes(StandardCharsets.UTF_8);
67+
68+
//@formatter:off
69+
jdbcTemplate.update("INSERT INTO DOCUMENT (ID, FILENAME, DATA) VALUES (?, ?, ?)",
70+
new Object[] { 3, "bigfile.txt", new SqlLobValue(bytes, new DefaultLobHandler()) },
71+
new int[] { Types.INTEGER, Types.VARCHAR, Types.BLOB }
72+
);
73+
//@formatter:on
74+
75+
byte[] stored = jdbcTemplate.queryForObject("SELECT DATA FROM DOCUMENT WHERE ID = 3", (rs, rowNum) -> rs.getBytes("DATA"));
76+
assertEquals(CONTENT, new String(stored, StandardCharsets.UTF_8));
77+
}
78+
79+
@Test
80+
void whenUsingSqlBinaryValue_thenCorrect() {
81+
byte[] bytes = CONTENT.getBytes(StandardCharsets.UTF_8);
82+
//@formatter:off
83+
jdbcTemplate.update("INSERT INTO DOCUMENT (ID, FILENAME, DATA) VALUES (?, ?, ?)",
84+
4,
85+
"bigfile.txt",
86+
new SqlParameterValue(Types.BLOB, new SqlBinaryValue(bytes))
87+
);
88+
//@formatter:on
89+
90+
byte[] stored = jdbcTemplate.queryForObject("SELECT DATA FROM DOCUMENT WHERE ID = 4", (rs, rowNum) -> rs.getBytes("DATA"));
91+
assertEquals(CONTENT, new String(stored, StandardCharsets.UTF_8));
92+
}
93+
94+
}

0 commit comments

Comments
 (0)