forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDerbyWriteImage.java
More file actions
51 lines (35 loc) · 1.31 KB
/
DerbyWriteImage.java
File metadata and controls
51 lines (35 loc) · 1.31 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
package com.zetcode;
import com.zetcode.utils.DBUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DerbyWriteImage {
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 fileName = "src/main/resources/sid.jpg";
File img = new File(fileName);
try (FileInputStream fin = new FileInputStream(img)) {
pst = con.prepareStatement("INSERT INTO IMAGES(ID, DATA) VALUES(1, ?)");
pst.setBinaryStream(1, fin, (int) img.length());
pst.executeUpdate();
}
} catch (SQLException | IOException ex) {
Logger.getLogger(DerbyWriteImage.class.getName()).log(Level.SEVERE, null, ex);
} finally {
DBUtils.closeStatement(pst);
DBUtils.closeConnection(con);
}
}
}