Java实现MySQL图片存取操作

文章出处:http://blog.csdn.net/thc1987/archive/2009/03/10/3972201.aspx

存入操作
/*
---------------表结构------------
表名: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 {
	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();	//将光标指向第一行
			//从rs中读取stupic放进InputStream对象中
			in=rs.getBinaryStream("stupic");
			//申明byte数组,用来存放图片流
			byte[] b=new byte[40000];
			in.read(b);	//从InputStream对象中读取数据放进byte数组中
			//实例化OutputStream对象,在D盘创建一个图片文件
			out=new FileOutputStream("d:/222.jpg");
			//将文件输出,内容则为byte数组里面的数据
			out.write(b);
			out.flush();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(in!=null)
					in.close();
				if(out!=null)
					out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DBManager.close(rs, st, con);//关闭相关连接
		}
	}
}


需要说明的是,上面是原稿,下面我进行一下修改改为Java+mysql版本的:
首先来创建数据库:(在Mysql中创建)

mysql>  create database test;
mysql>  use test;
mysql>  create table student2  (
mysql>  id int(4) primary key not  null,
mysql>  name varchar(20),  
mysql>  stupic blob
mysql>  );



至此,mysql数据库,建立完毕。把mysql的驱动加上去。下面是java代码的实现:我在原来的代码基础上,稍作修改,就实现了。这里还得在D盘放一张111.jpg的图片,然后运行StoreBLOB.java,再运行GetBLOB.java,最后再查看D盘,多了一张222.jpg.
我自己改的,
存入操作:
package com.ibm.jdbc;
 import java.io.*;  
import java.sql.*;  
public class StoreBLOB {
	public static void main(String[] args) {
		//连接MySQl数据库
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String user="root";
		String password="root";
		String url="jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true"; 
		Connection con = null;
		try {
			con = DriverManager.getConnection(url);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		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();
			}
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			//关闭相关连接
			//DriverManager.close(ps, con);
			
		}
		
	}

}


取出操作
package com.ibm.jdbc;
import java.sql.*;
import java.io.*;

public class GetBLOB {
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (IllegalAccessException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		String user="root";
		String password="root";
		String url="jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true"; 
		Connection con = null;
		try {
			con = DriverManager.getConnection(url);
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		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();	//将光标指向第一行
			//从rs中读取stupic放进InputStream对象中
			in=rs.getBinaryStream("stupic");
			//申明byte数组,用来存放图片流
			byte[] b=new byte[40000];
			in.read(b);	//从InputStream对象中读取数据放进byte数组中
			//实例化OutputStream对象,在D盘创建一个图片文件
			out=new FileOutputStream("d:/222.jpg");
			//将文件输出,内容则为byte数组里面的数据
			out.write(b);
			out.flush();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(in!=null)
					in.close();
				if(out!=null)
					out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				rs.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			//DriverManager.close(rs, st, con);//关闭相关连接
		}
	}

}

你可能感兴趣的:(java,sql,mysql,jdbc,IBM)