Skip to content

Commit 8ff9cf1

Browse files
committed
https://jira.baeldung.com/browse/BAEL-7958
1 parent 1f4b944 commit 8ff9cf1

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package storefileblob;
2+
3+
import java.io.IOException;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.ByteArrayOutputStream;
7+
import java.sql.ResultSet;
8+
import java.sql.Connection;
9+
import java.sql.DriverManager;
10+
import java.sql.PreparedStatement;
11+
import java.sql.Statement;
12+
import java.sql.SQLException;
13+
import java.io.FileOutputStream;
14+
import java.io.InputStream;
15+
16+
public class JdbcConnection {
17+
18+
public static Connection connect() throws SQLException {
19+
Connection connection = DriverManager.getConnection("jdbc:h2:./test", "sa", "");
20+
return connection;
21+
22+
}
23+
24+
public static byte[] convertFileToByteArray(String filePath) throws IOException {
25+
File file = new File(filePath);
26+
try (FileInputStream fileInputStream = new FileInputStream(file); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
27+
byte[] buffer = new byte[1024];
28+
for (int len; (len = fileInputStream.read(buffer)) != -1; ) {
29+
byteArrayOutputStream.write(buffer, 0, len);
30+
}
31+
return byteArrayOutputStream.toByteArray();
32+
}
33+
}
34+
35+
public static boolean writeBlobToFile(String query, int paramIndex, int id, String filePath) throws IOException, SQLException {
36+
try (Connection connection = connect(); PreparedStatement statement = connection.prepareStatement(query)) {
37+
statement.setInt(paramIndex, id);
38+
try (ResultSet resultSet = statement.executeQuery(); FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath))) {
39+
while (resultSet.next()) {
40+
InputStream input = resultSet.getBinaryStream("picture");
41+
byte[] buffer = new byte[1024];
42+
int bytesRead;
43+
while ((bytesRead = input.read(buffer)) > 0) {
44+
fileOutputStream.write(buffer, 0, bytesRead);
45+
}
46+
}
47+
return true;
48+
}
49+
}
50+
}
51+
52+
public boolean createSchema() throws SQLException {
53+
String sql = """
54+
CREATE TABLE IF NOT EXISTS warehouses (
55+
id INTEGER PRIMARY KEY,
56+
name text NOT NULL,
57+
capacity REAL,
58+
picture BLOB
59+
);""";
60+
try (Connection connection = connect(); Statement stmt = connection.createStatement()) {
61+
stmt.execute(sql);
62+
return true;
63+
}
64+
}
65+
66+
public boolean insertFile(int id, String name, int capacity, String picture) throws SQLException, IOException {
67+
String insertSql = """
68+
INSERT INTO warehouses(id,name,capacity,picture) VALUES(?,?,?,?)
69+
""";
70+
try (Connection conn = connect()) {
71+
if (conn != null) {
72+
PreparedStatement stmt = conn.prepareStatement(insertSql);
73+
stmt.setInt(1, id);
74+
stmt.setString(2, name);
75+
stmt.setDouble(3, capacity);
76+
stmt.setBytes(4, convertFileToByteArray(picture));
77+
stmt.executeUpdate();
78+
return true;
79+
}
80+
}
81+
return false;
82+
}
83+
84+
public boolean insertFileAsStream(int id, String name, int capacity, String filePath) throws SQLException, IOException {
85+
String insertSql = """
86+
INSERT INTO warehouses(id,name,capacity,picture) VALUES(?,?,?,?)
87+
""";
88+
try (Connection conn = connect()) {
89+
if (conn != null) {
90+
PreparedStatement stmt = conn.prepareStatement(insertSql);
91+
stmt.setInt(1, id);
92+
stmt.setString(2, name);
93+
stmt.setDouble(3, capacity);
94+
File file = new File(filePath);
95+
try (FileInputStream fis = new FileInputStream(file)) {
96+
stmt.setBinaryStream(4, fis, file.length());
97+
stmt.executeUpdate();
98+
return true;
99+
}
100+
101+
}
102+
}
103+
return false;
104+
}
105+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package storefileblob;
2+
3+
import org.junit.Test;
4+
import org.junit.jupiter.api.MethodOrderer;
5+
import org.junit.jupiter.api.Order;
6+
import org.junit.jupiter.api.TestMethodOrder;
7+
8+
import java.io.IOException;
9+
import java.sql.SQLException;
10+
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
13+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
14+
public class JdbcConnectionUnitTest {
15+
16+
String selectBlob = """
17+
SELECT picture FROM warehouses WHERE id=?
18+
""";
19+
20+
@Test
21+
@Order(1)
22+
public void givenDatabaseSchema_whenJdbcConnectionIsEstablished_thenCreateSchema() throws SQLException {
23+
JdbcConnection jdbcConnection = new JdbcConnection();
24+
assertTrue(jdbcConnection.createSchema());
25+
}
26+
27+
@Test
28+
@Order(3)
29+
public void givenBlobFile_whenInsertingTheBlobFileIntoTheWarehouseTable_thenSuccessful() throws SQLException, IOException {
30+
JdbcConnection jdbcConnection = new JdbcConnection();
31+
jdbcConnection.createSchema();
32+
assertTrue(jdbcConnection.insertFile(4, "Liu", 3000, "warehouse.png"));
33+
}
34+
35+
@Test
36+
@Order(2)
37+
public void givenBlobFile_whenInsertingTheFileIntoTheDatabaseAsStream_thenSuccessful() throws SQLException, IOException {
38+
JdbcConnection jdbcConnection = new JdbcConnection();
39+
jdbcConnection.createSchema();
40+
assertTrue(jdbcConnection.insertFileAsStream(6, "Liu", 3000, "warehouse.png"));
41+
}
42+
43+
@Test
44+
@Order(4)
45+
public void givenAnEntityWithBlob_whenReadingBlobAndConvertingItToFile_thenSuccessful() throws SQLException, IOException {
46+
JdbcConnection jdbcConnection = new JdbcConnection();
47+
assertTrue(JdbcConnection.writeBlobToFile(selectBlob, 1, 6, "retrieve-warehouse.png"));
48+
}
49+
50+
}
139 KB
Loading

0 commit comments

Comments
 (0)