学习JDBC(第四天)操作BLOB类型字段

操作BLOB类型字段

我们先了解一下什么是BLOB类型字段,看下图

在这里插入图片描述
在这里插入图片描述

BLOB类型就是在数据库中插入图片,视频等信息。

Mysql中四种BLOB类型(图片一般用MediumBlob,视频用LongBlob)

在这里插入图片描述

对代码进行了解

//主要代码块
public class BlobTest {

	//向数据表customers中插入Blob类型字段
	
	public void testInsert() throws Exception{
		Connection coon=JDBCUtils.getConnection();
		String sql="insert into customers(name,email,birth,photo)values(?,?,?,?)";
		PreparedStatement ps=coon.prepareStatement(sql);
		
		ps.setObject(1, "名字");
		ps.setObject(2, "qq.com");
		ps.setObject(3, "2001-1-16");
		FileInputStream is=new FileInputStream(new File("sp20230719_071013_046.png"));
		ps.setBlob(4, is);
		
		ps.execute();
		JDBCUtils.closeResource(coon, ps);
	}

}

new File中为图片路径

JDBCUtils中代码块

package com.atue3.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.atguigu2.connection.ConnectionTest;
import com.mysql.cj.xdevapi.Statement;

//操作数据库的工具类
public class JDBCUtils {
	//获取数据库连接
	public static Connection getConnection() throws Exception
	{
		InputStream is=ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");        
		               //ClassLoader.getSystemClassLoader()
		Properties pros=new Properties();
		pros.load(is);
		
		String user=pros.getProperty("user");
		String password=pros.getProperty("password");
		String url=pros.getProperty("url");
		String driverClass=pros.getProperty("driverClass");
		
		
		Class.forName(driverClass);
		
		Connection coon=DriverManager.getConnection(url,user,password);
		
		return coon;
		
	}
	
	//关闭资源的操作
	public static void closeResource(Connection coon,PreparedStatement ps)
	{
		try {
			if(ps!=null)
				ps.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		try {
			if(coon!=null)
				coon.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	public static void closeResource(Connection coon,PreparedStatement ps,ResultSet rs)
	{
		try {
			if(ps!=null)
				ps.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		try {
			if(coon!=null)
				coon.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		try {
			if(rs!=null)
				rs.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	

}

接下来我们学习从数据表中读取Blob类型数据

public void testQuery() throws Exception{
		Connection coon=null;
		PreparedStatement ps=null;
		InputStream is=null;
		FileOutputStream fos=null;
		//图片获取用流来获取
		ResultSet rs=null;
		try {
			coon = JDBCUtils.getConnection();
			String sql="select id,name,photo from customer where id=?";
			ps = coon.prepareStatement(sql);
			ps.setInt(1, 21);
			is = null;
			fos = null;
			rs = ps.executeQuery();
			if(rs.next())
			{
				int id=rs.getInt(1);
				String name=rs.getString(2);
				Customer cust=new Customer(id,name);
				System.out.println(cust);
				//以上为前面学习


				//将Blob类型字段下载下来,以文件方式保存到本地
				Blob photo=rs.getBlob("photo");
				is=photo.getBinaryStream();
				fos=new FileOutputStream("---本地路径----");//图片保存到此路径
				byte[] buffer=new byte[1024];
				int len;
				while((len=is.read(buffer))!=-1)
				{
					fos.write(buffer,0,len);
				}
			}
		} catch(Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(is!=null)
					is.close();
			}catch(IOException e)
			{
				e.printStackTrace();
			}
			try {
				if(fos!=null)
					fos.close();
			}catch(IOException e)
			{
				e.printStackTrace();
			}
			
			JDBCUtils.closeResource(coon, ps,rs);
		}
		
	}

JDBCUtils为中间部分共用代码块,这里就不再强调

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值