/** * 练习插入大数据BLOB * 1、TinyBlob 最大255字节 * 2、Blob 最大65k * 3、MediumBlob 最大16M * 4、LongBlob 最大4G * * */ public class JDBC_15 { public static void setBlob(String sql,Object ...args){ Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; try { //通过自定义的JDBC工具类获取connection连接 connection=JDBCUtils.getConnection(); //等会要输出主键primarykey preparedStatement=connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); for (int i = 0; i < args.length; i++) { preparedStatement.setObject(i+1,args[i]); } preparedStatement.executeUpdate(); resultSet=preparedStatement.getGeneratedKeys(); while(resultSet.next()){ System.out.println("插入的主键是:"+resultSet.getObject(1)); } }catch (Exception e){ e.printStackTrace(); }finally { JDBCUtils.closeJDBC(connection,preparedStatement,resultSet); } }
}
BLOB :Large objects (大数据) 是用来存储大量的二进制和文本数据的一种数据类型 一个LOB字段可存储多达4GB的数据 MySQL的BLOB的类型 1、TinyBlob 最大255字节 2、Blob 最大65k 3、MediumBlob 最大16M 4、LongBlob 最大4G ****如何使用JDBC向数据库插入LOB 插入blob类型的数据必须使用preparedStatement,因为blob无法拼接 preparedStatement.setBlob(int index,InputStream in); ****如何读取数据库中的lob Blob pic=resultSet.getBlob(int index); inputStream in=pic.getBinaryStream(); 然后用输出流输出