从JAVA读取BLOB
来源:互联网 发布:安卓变苹果状态栏软件 编辑:程序博客网 时间:2024/05/24 01:26
如果將要檔案寫入資料庫,您可以在欄位上使用BLOB或CLOB資料型態,BLOB全名Binary Large Object,用於儲存大量的二進位資料,CLOB全名Character Large Object,用於儲存大量的文字資料。 在JDBC中也提供了Blob與Clob兩個類別分別代表BLOB與CLOB資料,JDBC並沒有提供直接存入BLOB或CLOB的對應介面(像是setBlob()、setClob()等),但我們可以使用PreparedStatement的setBinaryStream()、setObject()、setAsciiStream()、setUnicodeStream()等方法來代替,例如我們可以如下取得一個檔案,並將之存入資料庫中:
// 取得檔案 File file = new File("./logo_phpbb.jpg"); int length = (int) file.length(); InputStream fin = new FileInputStream(file); // 填入資料庫 PreparedStatement pstmt = conn.prepareStatement( "INSERT INTO files VALUES(?, ?)"); pstmt.setString(1, "米小國Logo"); pstmt.setBinaryStream (2, fin, length); pstmt.executeUpdate(); pstmt.clearParameters(); pstmt.close(); fin.close();如果要從資料庫中取得BLOB或CLOB資料,我們可以如下進行:
Blob blob = result.getBlob(2); // 取得BLOB Clob clob = result.getClob(2) // 取得CLOB
Blob擁有getBinaryStream()、getBytes()等方法,可以取得二進位串流或byte等資料,同樣的,Clob擁有getCharacterStream()、getSubString()等方法,可以取得字元串流或子字串等資料,您可以查看API文件來獲得更詳細的訊息。 下面這個程式示範基本的檔案存入資料庫並取出另存新檔:
import java.io.*; import java.sql.*; public class DBTest { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/upload?useUnicode=true&characterEncoding=Big5"; String user = "caterpillar"; String password = "123456"; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); // 取得檔案 File file = new File("./logo_phpbb.jpg"); int length = (int) file.length(); InputStream fin = new FileInputStream(file); // 填入資料庫 PreparedStatement pstmt = conn.prepareStatement( "INSERT INTO files VALUES(?, ?)"); pstmt.setString(1, "米小國Logo"); pstmt.setBinaryStream (2, fin, length); pstmt.executeUpdate(); pstmt.clearParameters(); pstmt.close(); fin.close(); // 從資料庫取出檔案 Statement stmt = conn.createStatement(); ResultSet result = stmt.executeQuery("SELECT * FROM files"); result.next(); String description = result.getString(1); Blob blob = result.getBlob(2); // 寫入檔案 System.out.println("檔案描述:" + description); FileOutputStream fout = new FileOutputStream("./logo_phpbb_2.jpg"); fout.write(blob.getBytes(1, (int)blob.length())); fout.flush(); fout.close(); stmt.close(); conn.close(); } catch(ClassNotFoundException e) { System.out.println("找不到驅動程式"); e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } }
- 从JAVA读取BLOB
- 从JAVA读取BLOB
- Java从Oracle读取Blob显示图片在页面
- Java从数据库中读取Blob对象图片并显示
- JAVA读取BLOB
- java读取数据库blob型
- grails从数据库读取blob显示图片
- 从oracle中读取blob字段
- 从oracle中读取blob图片资源
- JAVA读取Oracle数据库BLOB字段
- java 读取oracle的blob/clob
- JAVA读取Oracle数据库BLOB字段
- java读取oracle中Clob和Blob
- java读取oracle数据库中blob字段
- JAVA读取MySQL中的blob字段
- Java 连接 MySQL读取 Blob 生成图片
- java读取图片插入blob,并取出
- java 读取clob,blob转换为字符串
- JS技术大全
- 哪有那么多云计算
- 数据库的备份语句
- 如何实现大小写完全匹配的查询
- C# 计算时间差
- 从JAVA读取BLOB
- JS通用库函数
- ABAP出口无法保存之解决方法
- Vista 下支付宝控件、证书的相关使用问题
- lan-free&server-less
- 少走弯路的十条忠告
- 雅虎C#面试题
- 在ASP中判断SQL语句是否执行成功
- JavaScript实现RGB 与16进制互转