处理CLOB/BLOB类型(处理大型数据)

方式:

a、存储路径  

通过jdbc存储文件路径,然后根据IO操作处理

例如:储存:jdbc将F:\Program Files (x86)\WeGame\ifs.dll文件以字符串形式“F:\Program Files (x86)\WeGame”储存到数据库中

获取:1、获取该路径“F:\Program Files (x86)\WeGame”     2、IO

缺点:严格遵循路径,加入文件更换路径就会失效

b、

CLOB:大文本数据(小说——》数据)

BLOB:二进制

 clob:

存:

1、先通过pstmt的占位符替换小说内容

2、再通过pstmt.setCharacterStream(2, reader,(int)file.length());将上一步的占位符替换为小说流(第三个数需要是int类型,表长度)

File file = new File("C:\\Users\\14463\\Desktop\\新建文本文档.txt");
			InputStream in = new FileInputStream(file);//文件
			Reader reader = new InputStreamReader(in,"UTF-8");

 

取:Reader reader =rs.getCharacterStream("num2");获取字段名

                        Reader reader =rs.getCharacterStream("num2");
	    		//向文件写入数据 用file writer类
			FileWriter wirter = new FileWriter("G:\\Study\\nic\\txt.txt");
			//缓冲区
			char[] chs = new char[1024];
			int len =-1;
			while((len = reader.read(chs)) != -1) {
				wirter.write(chs,0,len);
				
			}		
			wirter.close( );
			reader.close();
		}                

方式:

a、存储路径  

通过jdbc存储文件路径,然后根据IO操作处理

例如:储存:jdbc将F:\Program Files (x86)\WeGame\ifs.dll文件以字符串形式“F:\Program Files (x86)\WeGame”储存到数据库中

获取:1、获取该路径“F:\Program Files (x86)\WeGame”     2、IO

缺点:严格遵循路径,加入文件更换路径就会失效

b、

CLOB:大文本数据(小说——》数据)

BLOB:二进制

 clob:

存:

1、先通过pstmt的占位符替换小说内容

2、再通过pstmt.setCharacterStream(2, reader,(int)file.length());将上一步的占位符替换为小说流(第三个数需要是int类型,表长度)

File file = new File("C:\\Users\\14463\\Desktop\\新建文本文档.txt");
			InputStream in = new FileInputStream(file);//文件
			Reader reader = new InputStreamReader(in,"UTF-8");

 

取:Reader reader =rs.getCharacterStream("num2");获取字段名

                        Reader reader =rs.getCharacterStream("num2");
	    		//向文件写入数据 用file writer类
			FileWriter wirter = new FileWriter("G:\\Study\\nic\\txt.txt");
			//缓冲区
			char[] chs = new char[1024];
			int len =-1;
			while((len = reader.read(chs)) != -1) {
				wirter.write(chs,0,len);
				
			}		
			wirter.close( );
			reader.close();
		}                

 

blob

存:

              Connection cont= DriverManager.getConnection(url, user, password);
			String Sql = "insert into blob values(?,?) ";	
			pstmt= cont.prepareStatement(Sql);
			pstmt.setInt(1, 1);
			File file = new File("G:\\CloudMusic\\mm.mp3");
			InputStream in = new FileInputStream(file);//文件
			pstmt.setBinaryStream(2, in,file.length());	
			int count = pstmt.executeUpdate();		
				if (count > 0) {
					System.out.println("操作成功");
				}

 取:

			Connection cont= DriverManager.getConnection(url, user, password);
			String Sql = "select mv from blob where id = ? ";	
			pstmt= cont.prepareStatement(Sql);
			pstmt.setInt(1, 1);
			rs = pstmt.executeQuery();
			if(rs.next()) {
			InputStream input  = rs.getBinaryStream("mv");

			OutputStream out = new FileOutputStream("文件");
			 
			byte[] chs = new byte[100];
			int len = -1;
			while ((len=input.read(chs)) != -1) {
			out.write(chs, 0, len);
			}
			out.close();
			input.close();
			}

 

posted @ 2020-04-29 17:53  迷茫超级兵  阅读(777)  评论(0编辑  收藏  举报