JDBC : 数据库的分页语句 && 调用函数和存储过程

一、数据库的分页语句

在编写Web应用程序等系统时,会涉及到与数据库的交互,如果数据库中数据量很大的话,一次检索所有的记录,会占用系统很大的资源,因此常常采用分页语句:需要多少数据就只从数据库中取多少条记录。以下是Sql Server,Oracle和MySQL的分页语句(从数据库表中的第M条数据开始取N条记录): 

SQL Server 

从数据库表中的第M条记录开始取N条记录,利用Top关键字(如果Select语句中既有top,又有order by,则是从排序好的结果集中选择):   

SELECT *  
FROM ( SELECT Top N *  FROM (SELECT Top (M + N - 1) * FROM 表名称 Order by 主键 desc) t1 ) t2  
Order by 主键 asc

例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:     

SELECT *  FROM ( SELECT TOP 20 *  
FROM (SELECT TOP 29 * FROM Sys_option order by sys_id desc) t1) t2  
Order by sys_id asc

Oralce数据库

从数据库表中第M条记录 开始检索N条记录


例如从表employees(主键为employee_id)中从11条记录还是检索20条记录,语句如下:


MySQL数据库

My sql数据库最简单,是利用mySQL的LIMIT函数,LIMIT [offset,] rows从数据库表中M条记录开始检索N条记录的语句为:   

SELECT [列名列表] FROM 表名称 LIMIT M,N  

例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:   

select * from sys_option limit 10,20 

二、调用存储函数 和 存储过程

创建函数

获取指定部门的工资和员工人数

create or replace function get_dept_salary(v_dept number , v_num out number)
return number
is 
       v_sal number(20,2) := 0;
       cursor salary_cursor is select salary from employees where department_id = v_dept;
begin
       v_num := 0;
       for c in salary_cursor loop
           v_sal := v_sal + c.salary;
           v_num := v_num + 1;
       end loop;
       return v_sal;
end;

创建存储过程

获取指定部门的工资

create or replace procedure get_salary(v_dept number , v_num out number)
is 
     cursor salary_cursor is select salary from employees where department_id = v_dept;
begin
       v_num := 0;
       for c in salary_cursor loop
           v_num := v_num + c.salary;
       end loop;
      dbms_output.put_line(v_num);
end;

调用函数:

/**
	 *  使用JDBC 调用函数
	 */
	@Test
	public void test1() {
		
		Connection conn = null;
		CallableStatement callableStatement = null;
		try {
			conn = JDBC_Tools.getConnection();
			//1.用Connection 对象的 prepareCall() 方法获取CallableStatement 实例
			//prepareCall() 方法需要传入 SQL 字符串,严格书写格式
			String sql = "{?= call get_dept_salary(?,?)}";
			callableStatement = conn.prepareCall(sql);
			
			//2.通过CallableStatement 的 registerOutParameter() 方法设置OUT 参数
			callableStatement.registerOutParameter(1, Types.NUMERIC);
			callableStatement.registerOutParameter(3, Types.NUMERIC);
			
			//3.通过 CallableStatement 的 setXxx() 方法设置IN 或 IN OUT 参数的值,若为空,setNull()方法
			callableStatement.setInt(2, 80);
			
			//4.调用 execute() 方法执行
			callableStatement.execute();
			
			//5.如果调用的是带返回值
			double salary = callableStatement.getDouble(1);
			long nums = callableStatement.getLong(3);
			System.out.println("salary : "+salary+" nums : "+nums);
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally{
			JDBC_Tools.relaseSource(conn, callableStatement);
		}
	}
调用存储过程
/**
	 * 使用JDBC 调用存储过程
	 */
	@Test
	public void test2() {
		
		Connection conn = null;
		CallableStatement callableStatement = null;
		try {
			conn = JDBC_Tools.getConnection();

			String sql = "{call get_salary(?,?)}";
			callableStatement = conn.prepareCall(sql);
			callableStatement.registerOutParameter(2, Types.NUMERIC);
			callableStatement.setInt(1, 80);

			callableStatement.execute();
			double salary = callableStatement.getDouble(2);
		
			System.out.println("salary : "+salary);
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally{
			JDBC_Tools.relaseSource(conn, callableStatement);
		}
	}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值