`

Save/Write/Read image from/to a database table

 
阅读更多

The sample table is created as below to have a blob field to store file.

CREATE TABLE t1 (c1 INT PRIMARY KEY NOT NULL, c2 BLOB(5M));

Then use the below code snippet to insert an image file as follows.
 
PreparedStatement pstmt = conn.prepareStatement ("INSERT INTO t1 VALUES (?,?)");
pstmt.setInt (1, 100);
File fBlob = new File ( "image1.gif" );
FileInputStream is = new FileInputStream ( fBlob );
pstmt.setBinaryStream (2, is, (int) fBlob.length() );
pstmt.execute ();
...
 

Retrieving a BLOB or in other words retrieving the image file stored in the database as follows:
 
Statement stmt = conn.createStatement ();
ResultSet rs= stmt.executeQuery("SELECT * FROM t1");
while(rs.next()) {
int val1 = rs.getInt(1);
InputStream val2 = rs.getBinaryStream(2);
...
} rs.close();

分享到:
评论

相关推荐

    BobBuilder_app

    I deferred from testing the get test over 100 million record as it would require a huge array in memory to store the Guid keys for finding later, that is why there is a NT (not tested) in the table. ...

    RxLib控件包内含RxGIF,全部源码及DEMO

    TFormStorage allows you to read and write virtually any component published property to an INI file or the system Registry with virtually no code. Works with 3rd party and your own custom controls as ...

    VB2005把图片存到SQL里

    image.Save(ms, ImageFormat.Jpeg) Return ms.ToArray() End Using End Function ``` 3. 执行SQL命令: ```vb.net command.ExecuteNonQuery() ``` 4. 关闭数据库连接: ```vb.net connection.Close() ``` 要从...

    ZendFramework中文文档

    9.5.2. Create a date from database 9.5.3. Create dates from an array 9.6. Constants for General Date Functions 9.6.1. Using Constants 9.6.2. List of All Constants 9.6.3. Self-Defined OUTPUT ...

    Programming Excel With Vba And .net.chm

    Table of Contents | Index Programming Excel with VBA and .NET Preface Part I: Learning VBA Chapter 1. Becoming an Excel Programmer Section 1.1. Why Program? Section 1.2. Record and Read ...

    ASP.NET实现图片以二进制的形式存入数据库

    stream.Write(image, 0, image.Length); Bitmap bitmap = new Bitmap(stream); context.Response.ContentType = "image/jpeg"; bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat...

Global site tag (gtag.js) - Google Analytics