分页和数据存储及读取

select * from stu limit 3,3;

limit  x y

x:代表索引 从哪里开始查

y:长度 查询多少条数据


分页的sql语句:

页面的索引index  页面从1开始,所以要-1;如果从0开始就不用减

select * from customer limit(index-1)*10,10;


存储二进制的数据

把图片或音频视频存到数据库里面,一般是把地址或文件名存到数据库里面。

有一些情况必须存到数据库里面:

演示二进制数据:

package com.heima.jdbc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

import com.heima.utils.JdbcUtils;
//演示二进制的数据存储及读取
//CREATE TABLE t1
//(
//	id INT PRIMARY KEY ,
//	image BLOB 
//)
public class BlobTest {

	@Test
	public void testSave() throws Exception{
		//获得链接对象
		Connection conn = JdbcUtils.getConnection() ;
		//创建预处理命令对象
		PreparedStatement pstmt = conn.prepareStatement("insert into t1 values(?,?)") ;
		//指定?的值
		pstmt.setInt(1, 1) ;
		File file = new File("src/2.gif") ;
		InputStream in = new FileInputStream(file) ;
		pstmt.setBinaryStream(2,in, (int)file.length()) ;
		
		//执行sql语句
		pstmt.executeUpdate() ;
		//释放资源
		JdbcUtils.release(null, pstmt, conn) ;
	}
	
	@Test
	public void testQuery() throws Exception{
		//获得链接对象
		Connection conn = JdbcUtils.getConnection() ;
		//创建预处理命令对象
		PreparedStatement pstmt = conn.prepareStatement("select * from t1") ;
		//执行sql语句
		ResultSet rs = pstmt.executeQuery() ;
		if(rs.next()){
			int id = rs.getInt("id") ;
			InputStream is = rs.getBinaryStream("image") ;
			//需要再建一个文件
			File f = new File("src/5.jpg") ;
			OutputStream os = new FileOutputStream(f) ;
			byte[] bs = new byte[1024] ;
			int b = 0 ;
			while((b = is.read(bs)) != -1){
				os.write(bs,0,b) ;
			}
			is.close() ;
			os.close() ;
		}
		//释放资源
		JdbcUtils.release(rs, pstmt, conn) ;
	}

}


演示大文本数据:
package com.heima.jdbc;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

import com.heima.utils.JdbcUtils;
//演示大文本数据存储及读取
//CREATE TABLE t2
//(
//	id INT PRIMARY KEY ,
//	txt TEXT 
//)
public class ClobTest {

	@Test
	public void testSave() throws Exception{
		//获得链接对象
		Connection conn = JdbcUtils.getConnection() ;
		//创建预处理命令对象
		PreparedStatement pstmt = conn.prepareStatement("insert into t2 values(?,?)") ;
		//指定?的值
		pstmt.setInt(1, 1) ;
		File file = new File("src/a.txt") ;
		FileReader fr = new FileReader(file) ;
	    pstmt.setCharacterStream(2,fr,(int)file.length()) ;
		
		//执行sql语句
		pstmt.executeUpdate() ;
		//释放资源
		JdbcUtils.release(null, pstmt, conn) ;
	}
	
	@Test
	public void testQuery() throws Exception{
		//获得链接对象
		Connection conn = JdbcUtils.getConnection() ;
		//创建预处理命令对象
		PreparedStatement pstmt = conn.prepareStatement("select * from t2") ;
		//执行sql语句
		ResultSet rs = pstmt.executeQuery() ;
		if(rs.next()){
			int id = rs.getInt("id") ;
			Reader reader = rs.getCharacterStream("txt") ;
			//需要再建一个文件
			File f = new File("src/b.txt") ;
			BufferedWriter bw = new BufferedWriter(new FileWriter(f)) ;
			BufferedReader br = new BufferedReader(reader) ;
			String s = "" ;
			while((s = br.readLine()) != null){
				bw.write(s) ;
				bw.newLine() ;
			}
			br.close() ;
			bw.close() ;
			
		}
		//释放资源
		JdbcUtils.release(rs, pstmt, conn) ;
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值