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+ }
0 commit comments