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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34928644/article/details/82789063

准备

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()方法即可获取自动增长字段的值
		 */
		//关键 1
		st.execute(sql, Statement.RETURN_GENERATED_KEYS);
		//关键 2
		ResultSet 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(?,?)";
		
		//关键 1
		PreparedStatement pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
		// 补全
		pst.setString(1,"admin");
		pst.setString(2,"pwd");
		
		pst.executeUpdate(); //注意这里是空参方法
		//关键 2
		ResultSet 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')";
			// 批量处理关键 1
			st.addBatch(sql);
		}
		// 把所有id都+100
		String sql = "update tb_user set id = id+100";
		st.addBatch(sql);
		
		// 批量处理关键 2
		int[] 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);
			//关键步骤 1
			pst.addBatch(); //注意 与上面不同 这里是空参!!!
		}
		// PreparedStatement 也可以这样添加一条sql语句到批量处理中
		sql = "delete from tb_user where id%2=1"; // 删除id值为偶数的记录
		// 但是要注意的是: 以下面这种方式 sql 语句必须是完整的 不能有 '占位符' !!!
		pst.addBatch(sql);
		
		// 关键步骤2
		int[] 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

	@Test
	public 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 演示

	@Test
	public 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]; //1KB
			int 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

	@Test
	public void saveText() throws Exception {
		Connection con = ConnUtil.getConnection();
		String sql = " insert into note(note) values(?)";
		PreparedStatement pst = con.prepareStatement(sql);
		//当前项目中源文件:JDBCDemo.java
		File 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 演示。

	@Test
	public 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();
	}

源码

源码链接

猜你喜欢

转载自blog.csdn.net/qq_34928644/article/details/82789063