测试存储文本数据和二进制数据(Clob和Blob)

package jdbc;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class i_Clob {

	/**
	 * 介绍CLOB
	 * 1.用于存储大量的文本数据
	 * 2.大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的方式来处理的的。
	 *   而非一般的字段,一次即可独处数据。

	 * 测试:
	 * 1.文本、字符串读入数据库CLOB字段。
	 * 2.将CLOB字段提取出来。
	 * @param args
	 */
	public static void main(String[] args) {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Reader r=null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3307/TestJdbc","root","123456");
			
			/******************文本读入*****************
		    ps=conn.prepareStatement("insert into t_user (username,myinfo) values(?,?)");
		    ps.setString(1, "Test");		    
		    //1.文本读入
		    //ps.setClob(2,new FileReader(new File("d:/a.txt")));//将文本文件输入到数据库中
		    //2.字符串读入(字符串-->字节流-->输入流-->字符流)
		    //ps.setClob(2,new BufferedReader(new InputStreamReader(new ByteArrayInputStream("aaa".getBytes()))));  
		    ps.executeUpdate();
		    */
			
			//*****************文本读出*****************
			ps=conn.prepareStatement("select * from t_user where id=?");
			ps.setObject(1, 81086);		
			rs=ps.executeQuery();
			while(rs.next()){
				Clob c=rs.getClob("myinfo");
				r=c.getCharacterStream();
				int temp=0;
				while((temp=r.read())!=-1){
					System.out.print((char)temp);
				}
			}
		   
			
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(r!=null){
				try {
					r.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null){
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	
		}
	}

}
package jdbc;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class j_Blob {

	/**
	 * 	 介绍BLOB
	 * 1.用于存储大量的二进制数据(如:图像)
	 * 2.同上
	 * 
	 * 测试:利用流 读入\读出 图片
	 * @param args
	 */
	public static void main(String[] args) {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		InputStream is=null;
		OutputStream os=null;
		
		try {
			
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3307/TestJdbc","root","123456");
			
			/**********************读入图片*********************
		    ps=conn.prepareStatement("insert into t_user (username,image) values(?,?)");
		    ps.setString(1,"Test");		
		    ps.setBlob(2,new FileInputStream("D:/blobtest.jpg"));
		    ps.execute();
		    */
		    
			//读出图片
			ps=conn.prepareStatement("select * from t_user where id=?");
			ps.setObject(1, 81088);		
			rs=ps.executeQuery();
			while(rs.next()){
				Blob c=rs.getBlob("image");
				is=c.getBinaryStream();
				os=new FileOutputStream("d:/b.jpg");
				int temp=0;
				while((temp=is.read())!=-1){
					os.write(temp);
				}
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null){
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	
		}
	}

}

猜你喜欢

转载自blog.csdn.net/qq_41877184/article/details/93399120