6、处理大数据对象(CLOB 数据小说+BLOG 数据图片,电影,音乐)

大数据对象处理主要有 CLOB(character large object)和 BLOB(binary large object)两种类型的字段

第一节:处理 CLOB 数据

在 CLOB中可以存储大字符数据对象,比如长篇小说;

第二节:处理 BLOG 数据

在 BLOB 中可以存放二进制大数据对象,比如图片,电影,音乐;

实例1:通过流的方式把CLOB数据插入数据表内

1、图书Books模型

public class Books {

	private int id;
	private String bookName;
	private String author;
	private float price;
	private File content;
	
	
	public Books() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Books(String bookName, String author, float price, File content) {
		super();
		this.bookName = bookName;
		this.author = author;
		this.price = price;
		this.content = content;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public File getContent() {
		return content;
	}
	public void setContent(File content) {
		this.content = content;
	}
	
	
}

2、工具类

public class DbUtil {

	private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
	private static String dbUserName="root";
	private static String dbPassword="root";
	private static String jdbcName="com.mysql.jdbc.Driver";
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public Connection getCon() throws Exception{
		Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);		
		return con;
	}
	
	/**
	 * 关闭连接
	 * @param con
	 * @throws Exception
	 */
	public void close(Statement stmt,Connection con)throws Exception{
		if(stmt!=null){
			stmt.close();
			if(con!=null){
				con.close();
			}
		}
		
	}
}

3、测试类

public class jdbcTest {
	
	private static DbUtil dbUtil=new DbUtil();
	private static Books books=new Books();
	
	private static int addBook(Books books)throws Exception{
		Connection con=dbUtil.getCon();//获取连接
		String sql="insert into books values(null,?,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, books.getBookName());
		pstmt.setString(2, books.getAuthor());
		pstmt.setFloat(3, books.getPrice());
		-------输入流获取文件--------------------------------------
		File context=books.getContent();//获取文件
		InputStream inputStream=new FileInputStream(context);
		pstmt.setAsciiStream(4, inputStream,context.length());//给第5个?设置值
		----------------------------------------------------
		int result=pstmt.executeUpdate();
		dbUtil.close(pstmt, con);
		return result;
	}
	public static void main(String[] args) throws Exception {
	
		File context=new File("E:/luguo.txt");
		Books books=new Books("从你的全世界路过","张嘉佳",39.9f,context);
		addBook(books);
	}
	
}

运行结果:
在这里插入图片描述
在这里插入图片描述

实例2:通过输出流读取文本

3、测试类

public class jdbcTest {
	
	private static DbUtil dbUtil=new DbUtil();
	private static Books books=new Books();
	
	private static void getBook(int id)throws Exception{
		Connection con=dbUtil.getCon();//获取连接
		String sql="select * from books where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setInt(1, id);
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()){
			String bookName=rs.getString("bookName");
			String author=rs.getString("author");
			float price=rs.getFloat("price");
			Clob c=rs.getClob("content");//根据给定的参数名称,检索指定 JDBC BLOB 参数作为 Java 编程语言中的 Clob 对象的值。
			String context=c.getSubString(1, (int)c.length());
			System.out.println("书名:"+bookName);
			System.out.println("作者:"+author);
			System.out.println("价格:"+price);
			System.out.println("内容:"+context);
		}
		dbUtil.close(pstmt, con);
	}
	public static void main(String[] args) throws Exception {
		getBook(24);
	}
	
}

运行结果:
在这里插入图片描述

实例3:处理BLOG数据(二进制),通过输入流插入图片到数据表

1、图书Books模型

public class Books {

	private int id;
	private String bookName;
	private String author;
	private float price;
	private File content;
	private File illustration;
	
	public Books() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Books(String bookName, String author, float price, File content) {
		super();
		this.bookName = bookName;
		this.author = author;
		this.price = price;
		this.content = content;
	}
	
	
	public Books(String bookName, String author, float price, File content, File illustration) {
		super();
		this.bookName = bookName;
		this.author = author;
		this.price = price;
		this.content = content;
		this.illustration = illustration;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public File getContent() {
		return content;
	}
	public void setContent(File content) {
		this.content = content;
	}
	public File getIllustration() {
		return illustration;
	}
	public void setIllustration(File illustration) {
		this.illustration = illustration;
	}
	
	
}

2、工具类

public class DbUtil {

	private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
	private static String dbUserName="root";
	private static String dbPassword="root";
	private static String jdbcName="com.mysql.jdbc.Driver";
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public Connection getCon() throws Exception{
		Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);		
		return con;
	}
	
	/**
	 * 关闭连接
	 * @param con
	 * @throws Exception
	 */
	public void close(Statement stmt,Connection con)throws Exception{
		if(stmt!=null){
			stmt.close();
			if(con!=null){
				con.close();
			}
		}
		
	}
}

3、测试类

public class jdbcTest {
	
	private static DbUtil dbUtil=new DbUtil();
	private static Books books=new Books();
	
	private static int addBooks(Books books)throws Exception{
		Connection con=dbUtil.getCon();//获取连接
		String sql="insert into books values(null,?,?,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, books.getBookName());
		pstmt.setString(2, books.getAuthor());
		pstmt.setFloat(3, books.getPrice());
		
		File context=books.getContent();
		InputStream inputStream=new FileInputStream(context);
		pstmt.setAsciiStream(4, inputStream,context.length());
		
		File illustration=books.getIllustration();//获取图片文件
		InputStream inputStream2=new FileInputStream(context);
		pstmt.setBinaryStream(5, inputStream2,illustration.length());//二进制,给第六个?赋值
		
		int result=pstmt.executeUpdate();
		dbUtil.close(pstmt, con);
		return result;
	}
	public static void main(String[] args) throws Exception {
		File context=new File("E:/luguo.txt");
		File illustration=new File("E:/luguo.jpg");
		Books books=new Books("从你的全世界路过","张嘉佳",39.9f,context,illustration);
		addBooks(books);
	}
	
}

运行结果:
在这里插入图片描述

实例4:通过输出流读取BLOG数据,把图片导入到C盘查看(页面的话直接输出到页面)

3、测试类

 public class jdbcTest {
	
	private static DbUtil dbUtil=new DbUtil();
	private static Books books=new Books();
	
	private static void getBook(int id)throws Exception{
		Connection con=dbUtil.getCon();//获取连接
		String sql="select * from books where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setInt(1, id);
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()){
			String bookName=rs.getString("bookName");
			String author=rs.getString("author");
			float price=rs.getFloat("price");
			
			Clob c=rs.getClob("content");//根据给定的参数名称,检索指定 JDBC BLOB 参数作为 Java 编程语言中的 Clob 对象的值。
			String context=c.getSubString(1, (int)c.length());
			
			Blob b=rs.getBlob("illustration");
			FileOutputStream out=new FileOutputStream(new File("C:/luguo.jpg"));
			out.write(b.getBytes(1, (int)b.length()));
			out.close();
			
			System.out.println("书名:"+bookName);
			System.out.println("作者:"+author);
			System.out.println("价格:"+price);
			System.out.println("内容:"+context);
		}
		dbUtil.close(pstmt, con);
	}
	public static void main(String[] args) throws Exception {
		getBook(26);
	}
	
}

运行结果:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统适用行业: 视频拍摄制作(微电影、广告片、宣传片) 、商品展示短视频(食品/鞋服/美妆/母婴等) 、创意广告片(剧情大片/宣传片)、视频拍摄(企业宣传片、广告片、创意视频) 、美食加工过程短视频、商品展示短视频、商品展示短视频(食品/鞋服/美妆/母婴等) TVC广告、影视剧制作、创意TVC广告片、视频制作拍摄(宣传片、产品视频、TVC、广告、动画课程)等 功能简介:  站群 一个后台管理N个网站数据互通,每个网站可以单独绑定域名,单独设置SEO信息,单独设置LOGO,单独广告等   视频打赏 用户可以对视频上传作者进行各种礼物打赏,打赏所得金额作者可拿到对应的提成收入,所得收入可人民币提现   三级分销 三级分销商可以拥有独立的分销商网站独立的管理后台,并且可以自定义绑定域名,可统计每天收益基本设置等   多线路播放 支持多服务器线路播放视频,不同的会员级别可指定观看不同的播放线路解决宽带负荷,从而提高会员的积极消费   视频上传提成 用户在线上传的视频,如该视频被其他用户比如10个金币消费了,发布视频者可得到如3个金币的奖励   视频试看限制 PC端可自定义设置视频试看时间超出时间提示购买VIP,手机采取每天试看次数超出次数提示购买VIP   自动转码 结合云转码软件实现站长/用户上传视频支持任意格式转码,生成视频图片、名称、播放地址、完全自动   每天签到 用户每天签到可得到系统自动奖励的金币,24小时允许签到一次,从而提高用户对网站的黏贴度哦   分享奖励 用户登陆会员中心获取宣传地址分享到QQ、论坛、微信等,不同IP被点击一次奖励指定的消费金币   卡密充值 用户可凭卡密在会员中心进行充值获取金币,后台可生成卡密的金币数直接到发卡平台销售   播放器广告 整合Ckplay播放器,后台可控制加入播放前载入广告,暂停播放广告可开启/关闭广告   支付体系 整合流行支付接口,比如微信支付,支付宝支付等以及第三方支付平台   APP应用 封装安卓APP应用以及苹果APP应用,封装手机端数据同步自动更新   会员在线上传 用户可通过电脑、手机端在线上传视频以及发布图片小说等,实时分享到网站   上传视频奖励 用户上传视频一经站长后台审核通过,该用于将得到系统奖励的金币   防盗链加密 结合云转码可现实防盗链,从而不用担心自己视频资源被盗链,除此之外播放地址我们还做了加密使资源更安全

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值