-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargeImageStore.java
50 lines (39 loc) · 1.31 KB
/
LargeImageStore.java
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
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 javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class LargeImageStore {
private static Connection conn;
private static PreparedStatement pstmt;
public static void main(String[] args) {
if(conn == null) {
try {
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/demo","postgres","tiger");
String q = "INSERT INTO images(pic) values(?)";
pstmt = conn.prepareStatement(q);
JFileChooser jfc = new JFileChooser(); // gives a gui screen to choose image
jfc.showOpenDialog(null);
File file =jfc.getSelectedFile();
FileInputStream fis = new FileInputStream(file);
pstmt.setBinaryStream(1, fis, fis.available());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Sucess"); // pop up message on screen
} catch (ClassNotFoundException | SQLException | IOException e) {
e.printStackTrace();
}finally {
try {
if(conn != null)conn.close();
if(pstmt != null) pstmt.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
}