java读取图像文件存入oracle中blob字段源代码

最近因为要做点东西很少写文章了。
尤其是技术类的文章。
在网上看了很多关于Java对blob字段的操作。
自己也尝试着写了个,
自己也修改了部分。
代码写得很乱。

数据库:oracle 10G  XE
数据源驱动:jdbc12.jar
文件名:WriteBlob
数据库中建立一个为clobtest的表,内有两个字段,name (varchar2(20)),content(blob)。
 1package dbdriver;
 2
 3/**
 4 * 2008-09-28
 5 * @author duduli
 6 * email: lxyzj2000@gmail.com
 7 */

 8import java.sql.*;
 9import java.io.*;
10import oracle.sql.*;
11
12public class WriteBlob {
13
14    public static void main(String[] args) {
15        try {
16            String fName2 = "";
17            String fileName = "E:\\jianxin.bmp";
18//E盘下游个jianxin.bmp的图像文件
19            File f = new File(fileName);
20            String fName = f.getName();  
21            int i = fName.lastIndexOf('.');   
22            if (i > 0 && i < fName.length()-1){   
23                   fName2 = fName.substring(0,i);   
24                }
   
25            System.out.println(fName2);
26//获得文件名,出去后缀的文件名。
27            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
28            Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE""system""duduli   ");
29            conn.setAutoCommit(false);
30            BLOB blob = null;
31            PreparedStatement pstmt = conn.prepareStatement("insert into blobtest(name,content) values(?,empty_blob())");
32            pstmt.setString(1, fName2);
33            pstmt.executeUpdate();
34            pstmt.close();
35            pstmt = conn.prepareStatement("select content from blobtest where name= ? for update");
36            pstmt.setString(1, fName2);
37            ResultSet rset = pstmt.executeQuery();
38            if (rset.next()) {
39                blob = (BLOB) rset.getBlob(1);
40            }

41
42            FileInputStream fin = new FileInputStream(f);
43            System.out.println("file size = " + fin.available());
44            pstmt = conn.prepareStatement("update blobtest set content=? where name=?");
45            OutputStream out = blob.getBinaryOutputStream();
46            byte[] data = new byte[(int) fin.available()];
47            fin.read(data);
48            out.write(data);
49            fin.close();
50            out.close();
51            pstmt.setBlob(1, blob);
52            pstmt.setString(2, fName2);
53            pstmt.executeUpdate();
54            pstmt.close();
55            conn.commit();
56            conn.close();
57                }
 catch (SQLException e) {
58                    System.err.println(e.getMessage());
59                    e.printStackTrace();
60                }
 catch (IOException e) {
61                    System.err.println(e.getMessage());
62                }

63    }

64}

posted on 2008-10-02 21:08 duduli 阅读(5794) 评论(3)  编辑  收藏 所属分类: java

评论

# re: java读取图像文件存入oracle中blob字段源代码[未登录] 2008-10-03 18:35 java宅男

你好,可以给我留下你的联系方式吗?
我的Email
chenglu@yeah.net~
跟着高手学学  回复  更多评论   

# re: java读取图像文件存入oracle中blob字段源代码 2008-10-03 21:53 免费小说

感觉满复杂。。。。  回复  更多评论   

# re: java读取图像文件存入oracle中blob字段源代码[未登录] 2008-10-04 19:30 aaa

麻烦您能再写个从数据库获得图片的帖子  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2008年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

公告

welcome to my place.

常用链接

留言簿(5)

我参与的团队

随笔分类

随笔档案

新闻分类

石头JAVA摆地摊儿

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

@duduli