參考網址:http://www.cnblogs.com/jway1101/p/5815658.htmlhtml
首先是建表語句,須要實現將表創建好。java
CREATE TABLE `blobtest` ( `primary_id` varchar(32) NOT NULL, `bank_id` varchar(32) NOT NULL, `bank_name` varchar(64) NOT NULL, `blob_data` blob NOT NULL, PRIMARY KEY (`primary_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Blob類型數據存取測試用表'
而後是數據庫鏈接的工具類:mysql
package blobtest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class JDBCUtil { /*使用靜態代碼塊完成驅動的加載*/ static { try { String driverName = "com.mysql.jdbc.Driver"; Class.forName(driverName); } catch (Exception e) { e.printStackTrace(); } } /*提供鏈接的方法*/ public static Connection getConnection() { Connection con = null; try { //鏈接指定的MMySQL數據庫,三個參數分別是:數據庫地址、帳號、密碼 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf8", "root", "w513723"); } catch (Exception e) { e.printStackTrace(); } return con; } /*關閉鏈接的方法*/ public static void close(ResultSet rs, Statement stmt, Connection con) { try { if (rs != null) rs.close(); } catch (Exception ex) { ex.printStackTrace(); } try { if (stmt != null) stmt.close(); } catch (Exception ex) { ex.printStackTrace(); } try { if (con != null) con.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
接下來是真正的插入數據庫的java代碼:sql
package blobtest; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ReadWriteBlobwithMysql { private Connection con; private Statement stmt; public Statement getStmt() { return stmt; } public void setStmt(Statement stmt) { this.stmt = stmt; } public ResultSet getRs() { return rs; } public void setRs(ResultSet rs) { this.rs = rs; } ResultSet rs; public Connection getCon() { return con; } public void setCon(Connection con) { this.con = con; } public void insert(Connection con) throws SQLException { String fileName = "E:\\JavaProject\\HelloWorld\\src\\blobtest\\test.html"; File file = new File(fileName); try { FileInputStream fis = new FileInputStream(file); String sql = "insert into blobtest values('12','0000','平安銀行',?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setBlob(1, fis,file.length()); prest.execute(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public ReadWriteBlobwithMysql(Connection con) { this.setCon(con); try { stmt = con.createStatement(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { Connection con = JDBCUtil.getConnection(); ReadWriteBlobwithMysql dao = new ReadWriteBlobwithMysql(con); try { // dao.createTable(); dao.insert(con); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtil.close(dao.getRs(), dao.getStmt(), dao.getCon()); } } }
使用select語句查看一下執行結果,第二條是使用程序插入的,其餘的是使用sql語句直接插入的:數據庫
下面是增長了從數據庫讀取blob數據的java代碼,增長了queryBlob(。。。)函數:函數
package blobtest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ReadWriteBlobwithMysql { private Connection con; private Statement stmt; public Statement getStmt() { return stmt; } public void setStmt(Statement stmt) { this.stmt = stmt; } public ResultSet getRs() { return rs; } public void setRs(ResultSet rs) { this.rs = rs; } ResultSet rs; public Connection getCon() { return con; } public void setCon(Connection con) { this.con = con; } public void insert(Connection con) throws SQLException { String fileName = "E:\\JavaProject\\HelloWorld\\src\\blobtest\\test.html"; File file = new File(fileName); try { FileInputStream fis = new FileInputStream(file); String sql = "insert into blobtest values('12','0000','平安銀行',?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setBlob(1, fis, file.length()); prest.execute(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void queryBlob(String id, Connection con) throws IOException { String fileName = "E:\\JavaProject\\HelloWorld\\src\\blobtest\\test1.html"; String sql = "select * from blobtest where primary_id= ?"; try { PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, id); ResultSet rs = prest.executeQuery(); while (rs.next()) { Blob bl = rs.getBlob("blob_data");// 數據保存在表的blob_data字段中,這裏取出這裏保存的數據。 InputStream is = bl.getBinaryStream(); // 查看blob,能夠經過流的形式取出來。 注意必定要是用流的方式讀取出來 BufferedInputStream buffis = new BufferedInputStream(is); // 保存到buffout BufferedOutputStream buffout = new BufferedOutputStream(new FileOutputStream(fileName)); byte[] buf = new byte[1024]; int len = buffis.read(buf, 0, 1024); while (len > 0) { buffout.write(buf); len = buffis.read(buf, 0, 1024); } buffout.flush(); buffout.close(); buffis.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public ReadWriteBlobwithMysql(Connection con) { this.setCon(con); try { stmt = con.createStatement(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { Connection con = JDBCUtil.getConnection(); ReadWriteBlobwithMysql dao = new ReadWriteBlobwithMysql(con); try { // dao.createTable(); // dao.insert(con); dao.queryBlob("12",con); } catch (IOException e) { e.printStackTrace(); } finally { JDBCUtil.close(dao.getRs(), dao.getStmt(), dao.getCon()); } } }