반응형
자바를 이용하여 PostgresqlDB 연동 및 이미지 삽입 코드 입니다.
C:\\ 안에 있는 test.jpg 를 불러와서 FileInputStream 클래스를 통해 DB에 이미지를 삽입하는 부분입니다.
궁금하신 점은 댓글로!
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
|
/** *****************************************************************
* 이미지 삽입쿼리
*
* @return
* ****************************************************************/
public int imginsert()
{
Connection con = null;
PreparedStatement pstmt = null;
int rs ;
try {
String url = "jdbc:postgresql://url/db명";
String user = "dbuser";
String pw = "dbpw";
FileInputStream fis = new FileInputStream(file);
con = DriverManager.getConnection(url,user,pw);
System.out.println("db연결완료");
String sql = "insert into img_test values(?,?)";
pstmt = con.prepareStatement(sql);
System.out.println("sql문");
pstmt.setBinaryStream(1, fis, (int) file.length());
rs = pstmt.executeUpdate();
}catch(Exception e) {
System.out.println(e.getMessage());
}
int iret = 1;
return iret;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
DB 안에 있는 이미지 파일을 불러와 출력하는 코드 입니다.
DB연결을 하고, DB 안에있는 이미지를 byte 배열 변수에 담았습니다.
InputStream 과 ByteArrayInputStream 을 통해 바이너리 데이터로 지정된 이미지 파일을
DB에서 가져온 이미지를 D:\\ 경로에 FileOutputStream 클래스를 통해 출력하고 있는 코드 입니다.
역시 궁금하신 점은 댓글로!
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
55
|
/** *****************************************************************
* 이미지 출력쿼리
*
* @return
* ****************************************************************/
public int imgSelect()
{
int iret = 2;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
String url = "jdbc:postgresql://URL/DB명";
String user = "dbuser";
String pw = "dbpw";
con = DriverManager.getConnection(url, user, pw);
String sql = "select * from img_test";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
System.out.println(rs);
while(rs.next()) {
DBDto2 dto = new DBDto2();
byte [] imgBytes = rs.getBytes (1);
dto.setImg_number(rs.getInt("img_number"));
dto.setImg_test(imgBytes);
byte[] imgdata = dto.getImg_test();
InputStream is = new ByteArrayInputStream(imgdata);
FileOutputStream fos = new FileOutputStream(u);
byte[] buff = new byte[1024];
int len;
fos.write(buff, 0, len);
};
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return iret;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
반응형
'JAVA' 카테고리의 다른 글
톰캣 서버 실행 에러(해결방법) (2) | 2019.07.09 |
---|---|
SVN 형상관리 서버 사용하기 (0) | 2019.07.03 |
자바 Timer 클래스를 사용하여 일정시간에 주기적인 작업하기 (1) | 2019.06.19 |
자바 JAR 파일 배포하기 (실행에러포함) (1) | 2019.06.19 |
기상청 데이터 PostgresqlDB 연동하기)(DTO,DAO) (0) | 2019.06.18 |