转载自:http://blog.csdn.net/thc1987/article/details/3972201
存入操作
-
-
-
-
-
-
-
-
-
-
-
-
-
- package com.ibm.jdbc;
-
- import java.io.*;
- import java.sql.*;
- public class StoreBLOB {
- public static void main(String[] args) {
-
- Connection con=DBManager.getConnection();
- PreparedStatement ps=null;
- InputStream in=null;
- try {
-
- in=new FileInputStream("d:/111.jpg");
- ps=con.prepareStatement("insert into student2 values(?,?,?)");
- ps.setInt(1,2);
- ps.setString(2, "Tom");
- ps.setBinaryStream(3, in, in.available());
- ps.executeUpdate();
- } catch (IOException e) {
-
- e.printStackTrace();
- }catch (SQLException e) {
-
- e.printStackTrace();
- }
- finally
- {
- try {
-
- if(in!=null) in.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
-
- DBManager.close(ps, con);
-
- }
-
- }
- }
取出操作
- package com.ibm.jdbc;
-
- import java.sql.*;
- import java.io.*;
- public class GetBLOB {
- public static void main(String[] args) {
- Connection con=DBManager.getConnection();
- Statement st=null;
- ResultSet rs=null;
- InputStream in=null;
- OutputStream out=null;
-
- try {
- st=con.createStatement();
- rs=st.executeQuery("select stupic from student2 where id=2");
- rs.next();
-
- in=rs.getBinaryStream("stupic");
-
- byte[] b=new byte[40000];
- in.read(b);
-
- out=new FileOutputStream("d:/222.jpg");
-
- out.write(b);
- out.flush();
-
- } catch (SQLException e) {
-
- e.printStackTrace();
- }
- catch (IOException e) {
-
- e.printStackTrace();
- }
- finally
- {
- try {
- if(in!=null)
- in.close();
- if(out!=null)
- out.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- DBManager.close(rs, st, con);
- }
- }
- }
Java实现MySQL图片存取操作
原文:http://www.cnblogs.com/hInstance/p/3558582.html