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();
	}
}

 

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