有个问题,就是要知道具体是第几个字段是blob类型。若是对象,批量处理的时候并不容易兼容blob
package jdbc;
import org.junit.Test;
import java.io.*;
import java.sql.*;
/**
* @author 小邱
* @version 0.0.1
* @description PreparedStatement操作Blob类型数据(图片视频等数据)
* @since 2021/9/28 16:27
*/
public class PreparedStatementBlobTest {
//查询Blob数据
@Test
public void test2() throws SQLException, IOException {
Connection connection = JdbcUtils.getConnection();
String sql = "select username,photo from customers where username = ? ";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setObject(1,"小明");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
System.out.println(rs.getString(1));
Blob photo = rs.getBlob(2);
InputStream photoBinaryStream = photo.getBinaryStream();
FileOutputStream outputStream = new FileOutputStream(new File("abd.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len=photoBinaryStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
}
JdbcUtils.close(connection,ps,rs);
}
}