BLOB类型和TEXT类型

BLOB类型和TEXT类型_第1张图片

BLOB类型和TEXT类型_第2张图片BLOB类型和TEXT类型_第3张图片

public class BlobTest {
	@Test
    public void test1() throws Exception {
    	String sql="insert into img(img) values (?)";
    	Connection conn=JdbcUtil.getConn();
    	PreparedStatement pst=conn.prepareStatement(sql);
    	pst.setBlob(1, new FileInputStream("C:/Users/Desktop/img/b1.jpg"));
    	pst.executeUpdate();
    	JdbcUtil.close(null, pst, conn);
    }
	public void test2() throws SQLException, Exception {
		String sql="select * from img where id=?";
		Connection conn=JdbcUtil.getConn();
    	PreparedStatement pst=conn.prepareStatement(sql);
    	pst.setInt(1, 3);
    	ResultSet rs=pst.executeQuery();
    	if(rs.next()) {
    		Blob blob=rs.getBlob("img");
    		InputStream in=blob.getBinaryStream();
    		Files.copy(in, Paths.get("C:/newwow"));
    		
    	}
    	JdbcUtil.close(rs, pst, conn);
	}
}

你可能感兴趣的:(jdbc)