欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mysql存取图片 博客分类: Java MySQLSQLJava 

程序员文章站 2024-02-05 17:58:28
...
存入:
		Connection conn = DBFactory.getTourie();

		 FileInputStream str = new FileInputStream("d:/aa.jpg");
				
		 String sql= "insert into test_img(filename,content)   values(?,?) ";
				
		 PreparedStatement pstmt= conn.prepareStatement(sql);
		 pstmt.setString(1,"aaa");
		 pstmt.setBinaryStream(2,str,str.available());
		 pstmt.execute();
				
		 pstmt.close();


读取:

String   sql   =   "select content from  test_img   where   id=1 ";   
		PreparedStatement   pstmt =null;   
		ResultSet  rs=null;   
			pstmt=conn.prepareStatement(sql); 
			
			rs=pstmt.executeQuery(sql);  
			
			while(rs.next())   {   
				InputStream   in   =   rs.getBinaryStream(1); 
				FileOutputStream fos = new FileOutputStream("e://aa.jpg");
				int temp = in.read();
				while(temp != -1) {
					fos.write(temp);
					temp = in.read();
				}
				fos.close();
				in.close();
			}
			rs.close();
			pstmt.close();
			
			conn.close();
		
相关标签: MySQL SQL Java