您的位置:首页 > 数据库 > Oracle

Read blob from oracle using Java

2015-06-06 15:16 423 查看

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
56
57

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ReadBlob {
public static void main(String args[])
{
System.out.println("Oracle Connect START.");
Connection conn = null;
String url = "jdbc:oracle:thin:@address:portNo:";
String dbName = "DB Name";
String driver = "oracle.jdbc.OracleDriver";
String userName = "userName";
String password = "password";
ResultSet rs = null;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);

Statement stmt = conn.createStatement();
rs =stmt.executeQuery("select blob_column from tableName where somecondition");
Blob lob = null;
while (rs.next()) {
lob=rs.getBlob("blob_column");
}

InputStream in = lob.getBinaryStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream outputStream = new FileOutputStream("blobImage.png");

int bufferSize = 1024;
int length = (int) lob.length();

byte[] buffer = new byte[bufferSize];

while((length = in.read(buffer)) != -1)
{
out.write(buffer,0,length);
}
out.writeTo(outputStream);
in.close();
conn.close();

Process p1 =Runtime.getRuntime().exec("mspaint blobImage.png");

}catch (Exception e) {
e.printStackTrace();
}

}
}

Here we will learn how to read blob data from Oracle. In this code we read image and saved the same on local system and then execute mspaint from windows to show the saved images.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: