package com.allan; import java.sql.*; import java.io.*; public class Storeblobfile { public static void main(String[] args) { try{ FileInputStream file = new FileInputStream("C:\\shanshui.jpg"); Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root"); PreparedStatement ps = conn.prepareStatement("insert into user values(?,?,?)"); ps.setString(1,"blob"); ps.setInt(2,23); ps.setBinaryStream(3, file, file.available()); ps.executeUpdate(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select file from user where name = 'blob'"); while(rs.next()){ Blob blob = rs.getBlob(1); InputStream in = blob.getBinaryStream(); FileOutputStream fout = new FileOutputStream("C:\\copy.jpg"); int b = -1; while((b=in.read())!=-1){ fout.write(b); } } }catch(Exception e){ System.out.println(e.getMessage()); } } } //如果有一blob类型的列“content”,要将content中的数据取出来放到String中: Blob blob = rs.getBlob("content"); int bolblen = (int) blob.length(); byte[] data = blob.getBytes(1, bolblen); String content = new String(data);
mysql-java读取mysql二进制数据
猜你喜欢
转载自x125858805.iteye.com/blog/2280048
今日推荐
周排行