forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDerbyReadImage.java
More file actions
55 lines (39 loc) · 1.44 KB
/
DerbyReadImage.java
File metadata and controls
55 lines (39 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.zetcode;
import com.zetcode.utils.DBUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DerbyReadImage {
public static void main(String[] args) {
Connection con = null;
PreparedStatement pst = null;
String url = "jdbc:derby://localhost:1527/testdb";
String user = "app";
String password = "app";
try {
con = DriverManager.getConnection(url, user, password);
String query = "SELECT DATA FROM IMAGES WHERE ID = 1";
pst = con.prepareStatement(query);
ResultSet result = pst.executeQuery();
result.next();
try (FileOutputStream fos = new FileOutputStream("src/main/resources/sid.jpg")) {
Blob blob = result.getBlob("DATA");
int len = (int) blob.length();
byte[] buf = blob.getBytes(1, len);
fos.write(buf, 0, len);
}
} catch (SQLException | IOException ex) {
Logger.getLogger(DerbyReadImage.class.getName()).log(Level.SEVERE, null, ex);
} finally {
DBUtils.closeStatement(pst);
DBUtils.closeConnection(con);
}
}
}