本文實例講述了JDBC編程實現(xiàn)文件、圖片的存儲方法。分享給大家供大家參考,具體如下:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/* 實現(xiàn)的功能: 用數據庫存儲文本數據,并且讀取出來放在當前項目里 分析: 難度不是很大,關鍵是掌握文件流,數據庫的操作不是很多,但是文件流的讀寫比較多 日期:20131003 作者:煙大陽仔 */ public class Ckb_test { public static void main(String[] args) throws SQLException, IOException { // TODO Auto-generated method stub read(); } static void create() throws SQLException, IOException { Connection conn=null; PreparedStatement prest=null; ResultSet resultset=null; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設計模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語句 String sql="insert into ckb_test(text) values(?)"; prest=conn.prepareStatement(sql); File file=new File("src/cn/com/JDBC/JdbcUtils.java"); Reader reader=new BufferedReader(new FileReader(file)); prest.setCharacterStream(1, reader, (int)file.length()); //4.執(zhí)行語句 int i=prest.executeUpdate(); reader.close(); System.out.println("i="+i); } finally { JdbcUtils.free(resultset, prest, conn); } } static void read() throws SQLException, IOException { Connection conn=null; Statement st=null; ResultSet resultset=null; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設計模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語句 st=conn.createStatement(); //4.執(zhí)行語句 resultset=st.executeQuery("select text from ckb_test"); //5.處理結果 while(resultset.next()) { Clob clob=resultset.getClob(1); Reader reader=clob.getCharacterStream(); //reader=resultset.getCharacterStream(1); File file=new File("JdbcUtils.java"); Writer writer=new BufferedWriter(new FileWriter(file)); char[] buff=new char[1024]; for(int i=0;(i=reader.read(buff))>0;) { writer.write(buff,0,i); } writer.close(); reader.close(); } } finally { JdbcUtils.free(resultset, st, conn); } } } /* 實現(xiàn)的功能: 用數據庫存儲圖片數據,并且讀取出來放在當前項目里 分析: 難度不是很大,關鍵是掌握字節(jié)流,數據庫的操作不是很多,但是文件流的讀寫比較多,注意更改圖片的目錄 日期:20131003 作者:煙大陽仔 */ public class PictureBlob { public static void main(String[] args) throws SQLException, IOException { read(); } static void create() throws SQLException, IOException { Connection conn= null ; PreparedStatement prest= null ; ResultSet resultset= null ; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設計模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語句 String sql= "insert into blob_test(big_bit) values(?)" ; prest=conn.prepareStatement(sql); File file= new File( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\cxg.jpg" ); InputStream in= new BufferedInputStream( new FileInputStream(file)); prest.setBinaryStream( 1 , in, ( int )file.length()); //4.執(zhí)行語句 int i=prest.executeUpdate(); in.close(); System.out.println( "i=" +i); } finally { JdbcUtils.free(resultset, prest, conn); } } static void read() throws SQLException, IOException { Connection conn= null ; Statement st= null ; ResultSet resultset= null ; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設計模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語句 st=conn.createStatement(); //4.執(zhí)行語句 resultset=st.executeQuery( "select big_bit from blob_test" ); //5.處理結果 while (resultset.next()) { Blob blob=resultset.getBlob( 1 ); InputStream in=blob.getBinaryStream(); //reader=resultset.getCharacterStream(1); File file= new File( "1.jpeg" ); OutputStream out= new BufferedOutputStream( new FileOutputStream(file)); byte [] buff= new byte [ 1024 ]; for ( int i= 0 ;(i=in.read(buff))> 0 ;) { out.write(buff, 0 ,i); } out.close(); in.close(); } } finally { JdbcUtils.free(resultset, st, conn); } } } |
希望本文所述對大家Java程序設計有所幫助。