jdbc中读取图片文件

	public static void printpic(){
		String sql="select id,name,photo from xuan_student ";
		try {
			Connection conn=ConnectionUtils.openConnection();
				Statement stmt=conn.createStatement();
			ResultSet rs=stmt.executeQuery(sql);
			if(rs.next()){
				FileOutputStream fos=new FileOutputStream(new File("asd.jpg"));
				InputStream ins=rs.getBinaryStream("photo");
				byte[] buffer=new byte[4*1024];
				int length=0;	
				while((length=ins.read(buffer))!=-1){
					fos.write(buffer,0,length);
				}
			}
				stmt.close();
				conn.close();
			} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
			e.printStackTrace();
		}
	}

你可能感兴趣的:(sql,jdbc)