当前位置: 代码迷 >> 综合 >> MySQL---当Java遇上MySQL③---自动增长字段 、批量处理 、LOB类型数据的存取
  详细解决方案

MySQL---当Java遇上MySQL③---自动增长字段 、批量处理 、LOB类型数据的存取

热度:59   发布时间:2023-10-21 16:40:56.0

准备

见MySQL---当Java遇上MySQL②

自动增长字段

获取自动增长字段的值

演示 Statement 获取

	@Test //为演示直观 异常直接抛了public void saveAutoIncrement1() throws Exception {Connection con = ConnUtil.getConnection();Statement st = con.createStatement();String sql = "insert into tb_user(username,password) values('刘备','8888')";/** 采用 Statement.RETURN_GENERATED_KEYS 参数* 配合getGeneratedKeys()方法即可获取自动增长字段的值*///关键 1st.execute(sql, Statement.RETURN_GENERATED_KEYS);//关键 2ResultSet keys = st.getGeneratedKeys();while(keys.next()) {int id = keys.getInt(1); System.out.println( "id:" + id );}con.close();}

演示 PreparedStatement 获取

	@Test //为演示直观 异常直接抛了public void saveAutoIncrement2() throws Exception {Connection con = ConnUtil.getConnection();String sql = "insert into tb_user(username,password) values(?,?)";//关键 1PreparedStatement pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);// 补全pst.setString(1,"admin");pst.setString(2,"pwd");pst.executeUpdate(); //注意这里是空参方法//关键 2ResultSet keys = pst.getGeneratedKeys();while(keys.next()) {int id = keys.getInt(1); System.out.println( "id:" + id );}con.close();}

演示批量处理

演示 Statement 批量处理

	@Test //为演示直观 异常直接抛了public void batchDemo1() throws Exception {Connection con = ConnUtil.getConnection();Statement st = con.createStatement();for (int i = 1; i <= 5; i++) {String sql = "insert into tb_user(username,password) values('刘备"+i+"','8888')";// 批量处理关键 1st.addBatch(sql);}// 把所有id都+100String sql = "update tb_user set id = id+100";st.addBatch(sql);// 批量处理关键 2int[] executeBatch = st.executeBatch();//打印批量处理后 返回的受影响的行数for (int i : executeBatch) {System.out.print(i+" ");}con.close();}

演示 PreparedStatement 批量处理

	@Test //为演示直观 异常直接抛了public void batchDemo2() throws Exception {Connection con = ConnUtil.getConnection();String sql = "insert into tb_user(username,password) values(?,?)";PreparedStatement pst = con.prepareStatement(sql);for( int i = 1; i <= 5; i++ ) {pst.setString(1, "Jack"+i);pst.setString(2, "pwd"+i);//关键步骤 1pst.addBatch(); //注意 与上面不同 这里是空参!!!}// PreparedStatement 也可以这样添加一条sql语句到批量处理中sql = "delete from tb_user where id%2=1"; // 删除id值为偶数的记录// 但是要注意的是: 以下面这种方式 sql 语句必须是完整的 不能有 '占位符' !!!pst.addBatch(sql);// 关键步骤2int[] executeBatch = pst.executeBatch();//打印批量处理后 返回的受影响的行数for (int i : executeBatch) {System.out.print(i+" ");}}

LOB类型数据的存取

LOB 代表大对象数据,包括 BLOB 和 CLOB 两种类型,前者用于存储大块的二进制数据,如图片数据,视频数据等,而后者用于存储长文本数据,如论坛的帖子内容,产品的详细描述等。

演示BLOB数据的存储和读取

  • 大数据量的二进制数据类型有四种:TinyBlob 、Blob 、 MediumBlob 、 LongBlob。
  • 对应的最大数据长度  255B  64kB   16MB   4GB。

准备

/* 建表语句 */
CREATE TABLE img(id INT PRIMARY KEY AUTO_INCREMENT,img MEDIUMBLOB
);

存储BLOB数据

注意 存储的时候 只能使用 PreparedStatemnet

	@Testpublic void saveBLOB() throws Exception {Connection con = ConnUtil.getConnection();String sql = " insert into img(img) values(?)";PreparedStatement pst = con.prepareStatement(sql);//d盘中a目录下的图片File file = new File("d:/a/1.jpg");InputStream in = new FileInputStream(file);//关键点pst.setBinaryStream(1, in); //技术入口!!!pst.executeUpdate();con.close();}

读取BLOB

读取的时候 PreparedStatemnet 和 Statemnet 都可以,所以读取时就用 Statemnet 演示

	@Testpublic void readBLOB() throws Exception {Connection con = ConnUtil.getConnection();Statement st = con.createStatement();ResultSet resultSet = st.executeQuery("select * from img where id = 1");while( resultSet.next() ) {int id = resultSet.getInt("id");System.out.println("id : "+id);//关键点!!!InputStream in = resultSet.getBinaryStream("img");//创建存放位置 File file = new File("d:/a/blob.jpg");FileOutputStream out = new FileOutputStream(file);//进行流对拷byte[] b = new byte[1024]; //1KBint len = in.read(b);while ( len != -1 ) {out.write(b, 0, len);len = in.read(b);}//关流,防止内存泄漏in.close();out.close();}//关闭连接---释放资源。con.close();}

演示CLOB数据的存储和读取

  • 大数据量的字符数据类型同样有四种:TinyText 、Text 、MediumText 、LongText。
  • 对应的最大数据长度  255B  64kB   16MB   4GB。

准备

/* 建表语句 */
CREATE TABLE note(id INT PRIMARY KEY AUTO_INCREMENT,note TEXT
);

存储CLOB

注意 存储的时候 只能使用 PreparedStatemnet

	@Testpublic void saveText() throws Exception {Connection con = ConnUtil.getConnection();String sql = " insert into note(note) values(?)";PreparedStatement pst = con.prepareStatement(sql);//当前项目中源文件:JDBCDemo.javaFile file = new File("./src/cn/hncu/jdbc/JDBCDemo.java");InputStream in = new FileInputStream(file);//关键点pst.setAsciiStream(1, in); //技术入口!!!pst.executeUpdate();con.close();}

读取CLOB

读取的时候 PreparedStatemnet 和 Statemnet 都可以,所以读取时就用 Statemnet 演示。

	@Testpublic void readText() throws Exception {Connection con = ConnUtil.getConnection();Statement st = con.createStatement();ResultSet resultSet = st.executeQuery("select * from note where id = 1");while( resultSet.next() ) {int id = resultSet.getInt("id");System.out.println("id : "+id);//关键点!!!InputStream in = resultSet.getBinaryStream("note");BufferedReader br = new BufferedReader( new InputStreamReader( in, "utf-8" ) );String str = br.readLine();while( str != null ) {System.out.println( str );str = br.readLine();}//防止内存泄漏 关流in.close();}//关闭连接---释放资源。con.close();}

源码

源码链接