/*
---------------表结构------------
表名:student2
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(4) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| stupic | blob | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
*/
package com.ibm.jdbc;
import java.io.*;
import java.sql.*;
public class StoreBLOB {
public static void main(String[] args) {
//连接MySQl数据库
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
//关闭流
if(in!=null) in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭相关连接
DBManager.close(ps, con);
}
}
}
取出操作
package com.ibm.jdbc;
import java.sql.*;
import java.io.*;
public class GetBLOB {
publ