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