Mysql+BLOB(二进制文件mps jpg)类型插入读取,使用java链接数据库

1.创建表格

(1)因为插入的时候有 “Data too long for column 'xxx' at row 1”这种错误,用的longblob,又去查的博客,现在看来MediumBlob就差不多。

 TinyBlob类型 :255B的数据;

 Blob类型  最大能容纳65KB的

 MediumBlob类型  最大能容纳16MB的数据

 LongBlob类型  最大能容纳4GB的数据

2.插入,查询

3.一种二进制流,一种直接getBlob().

错误分析:

(1)一开始从数据库下载出来的图片不能打开,是因为我自己在代码中多加了两行其他的代码,本来是二进制流,被我改成了字节流。删除刘珂矣

(2)重启完mysql8.0.19之后,出现Public Key Retrieval is not allowed这种错误,将url = "jdbc:MySQL://localhost:3306/hotel?&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";

(3)又出现了 Packet for query is too large ,在my.ini 中加入max_allowed_packet = 1024*1024*10

package servletDemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCBLOB{
	//二进制文件 blob  音乐 图片
	private static final String URL = "jdbc:MySQL://localhost:3306/hotel?&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
	private static final String USERNAME = "root";
	private static final String PWD = "root";
	
	public static void update(){//增删改
		Connection connection = null;//因为有可能还么有用到connection就报错了 那么这个对象是空的就不能使用close方法
		PreparedStatement pstmt = null;//同理
		try{
			//1导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2与DB建立连接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3发送sql语句,执行(增删改)
			//pstmt需要预编译sql
			String sql = "insert into my_image values (?,?)";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, 3);
			File file = new File("D:\\殷志源壁纸原图.jpg");
			InputStream in = new FileInputStream(file);
			pstmt.setBinaryStream(2,in, (int)file.length());
			//转换流可以设置编码
//			Reader reader = new InputStreamReader(in,"UTF-8");
//			pstmt.setCharacterStream(2, reader, (int)file.length());
			int count = pstmt.executeUpdate();//返回表示增删改 了几条数据
			//4处理结果集
			if(count>0){
				System.out.println("操作成功!");
			}
//			reader.close();
			in.close();
		}catch (ClassNotFoundException e) {//Class.forName()的异常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()异常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是无论有没有异常都需要关闭连接
			try{
				if(pstmt!=null){//执行sql语句
					pstmt.close();
				}
				if(connection!=null){//建立java代码和数据库的连接
					connection.close();
				}
			}catch(SQLException e) {//前面两个close 都会抛SQLException异常
				e.printStackTrace();
			}
		}		
	}
	public static void query(){//增删改
		Connection connection = null;//因为有可能还么有用到connection就报错了 那么这个对象是空的就不能使用close方法
		PreparedStatement pstmt = null;//同理
		ResultSet rs = null;
		try{
			//1导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2与DB建立连接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3发送sql语句,执行(增删改)
			//pstmt需要预编译sql
			String sql = "select image from my_image where id_image = ?";
			// c.发送sql,执行(查)
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, 3);
				
			rs = pstmt.executeQuery() ;
			if(rs.next())
			{
				InputStream in = rs.getBinaryStream("image") ;
				OutputStream out = new FileOutputStream("src/ejw01.jpg") ;
					
				byte[] chs = new byte[100] ;
				int len = -1;
				while(  (len = in.read(chs)) !=-1 ) {
					out.write( chs,0,len  );
				}
				out.close();
				in.close();
			}
		}catch (ClassNotFoundException e) {//Class.forName()的异常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()异常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是无论有没有异常都需要关闭连接
			try{
				if(pstmt!=null){//执行sql语句
					pstmt.close();
				}
				if(connection!=null){//建立java代码和数据库的连接
					connection.close();
				}
			}catch(SQLException e) {//前面两个close 都会抛SQLException异常
				e.printStackTrace();
			}
		}		
	}
		
	public static void main(String [] args){
//		update();
		query();
	}
}

 

package servletDemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCBLOBNEW{
	//二进制文件 blob  音乐 图片
	private static final String URL = "jdbc:MySQL://localhost:3306/hotel?&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
	private static final String USERNAME = "root";
	private static final String PWD = "root";
	
	public static void update(){//增删改
		Connection connection = null;//因为有可能还么有用到connection就报错了 那么这个对象是空的就不能使用close方法
		PreparedStatement pstmt = null;//同理
		try{
			//1导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2与DB建立连接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3发送sql语句,执行(增删改)
			//pstmt需要预编译sql
			String sql = "insert into my_image values (?,?)";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, 4);
 			File file = new File("D:\\殷志源壁纸原图.jpg");
			InputStream in = new FileInputStream(file);
			pstmt.setBlob(2, in);				
			int count = pstmt.executeUpdate();//返回表示增删改 了几条数据
			//4处理结果集
			if(count>0){
				System.out.println("操作成功!");
			}
			in.close();
		}catch (ClassNotFoundException e) {//Class.forName()的异常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()异常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是无论有没有异常都需要关闭连接
			try{
				if(pstmt!=null){//执行sql语句
					pstmt.close();
				}
				if(connection!=null){//建立java代码和数据库的连接
					connection.close();
				}
			}catch(SQLException e) {//前面两个close 都会抛SQLException异常
				e.printStackTrace();
			}
		}		
	}
	public static void query(){//增删改
		Connection connection = null;//因为有可能还么有用到connection就报错了 那么这个对象是空的就不能使用close方法
		PreparedStatement pstmt = null;//同理
		ResultSet rs = null;
		try{
			//1导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2与DB建立连接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3发送sql语句,执行(增删改)
			//pstmt需要预编译sql
			String sql = "select image from my_image where id_image = ?";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, 4);
				
			rs = pstmt.executeQuery() ;
			if(rs.next()){
				Blob img = rs.getBlob("image");//得到Blob对象
	            //开始读入文件
	            InputStream in =  img.getBinaryStream();
	            OutputStream out = new FileOutputStream("src/ejw02.jpg");
	            byte [] buffer = new byte[1024];
	            int len = -1;
	            while((len = in.read(buffer)) != -1){
	                   out.write(buffer, 0, len);
	            }
	            out.close();
	            in.close();
			}	 
		}catch (ClassNotFoundException e) {//Class.forName()的异常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()异常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是无论有没有异常都需要关闭连接
			try{
				if(pstmt!=null){//执行sql语句
					pstmt.close();
				}
				if(connection!=null){//建立java代码和数据库的连接
					connection.close();
				}
			}catch(SQLException e) {//前面两个close 都会抛SQLException异常
				e.printStackTrace();
			}
		}		
	}
		
	public static void main(String [] args){
//		update();
		query();
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章