1. 程式人生 > >Oracle:分頁

Oracle:分頁

SQL: 

Select ename from emp where ename like '_M%'第二個字母
Select to_char(sysdate, 'yyyy') a from dual //2018
select deptno, job, avg(sal) from emp group by job, deptno部門職員平均工資
select * from emp dept
select * from emp,dept where emp.deptno=dept.deptno

偽列:

select * from (select a.*, rownum mr from hcnote a order by noteid asc) where  mr>=1 and mr<=6

改進:

select * from(select a.*,rownum mr from(
select * from hcnote where notetxt like '%我%'  order by noteid desc)a )where  mr>=1 and mr<=2

對於排序而言的。

java:

package com.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.entity.User;
import com.util.DBhelper;

public class Dao {

	public int getMax(String str){
		int nn=0;
		Connection con=null;
		PreparedStatement pre=null;
		ResultSet res=null;
		try {
		con=DBhelper.getCon();
		 String sql="select count(*) from emp where ENAME like '%"+str+"%' " ;
			pre=con.prepareStatement(sql);
			res=pre.executeQuery();
		 if (res.next()) {
			nn=res.getInt(1);
		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			
			DBhelper.myClose(con, pre,res);
		}
		return nn;
		
		
	}
	
	public List<User> GetAll(int name,String str){
		List<User> ml=new ArrayList<User>();
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		int pages=3;
		int st=(name-1)*pages+1;
		int end=name*pages;
		try {
			con=DBhelper.getCon();
			String sql="select * from(select a.*,rownum mr from(select *" +
			" from emp where ENAME like '%"+str+"%' order by EMPNO desc)a )where mr>="+st+" and mr<="+end;

			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				User n=new User();
				n.setUname(rs.getString(1));
				n.setUpass(rs.getString(2));
				ml.add(n);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			DBhelper.myClose(con, ps, rs);
		}
		return ml;
	}
	
}

儲存:

declare 
gid varchar2(33):=&sno;
gsal scott.emp.sal%type;
begin
  select sal into gsal from emp
  where empno=gid;
  
  if(gsal>3000) then
  dbms_output.put_line('要納稅');
  elsif(gsal=3000) then
  dbms_output.put_line('不用');
  else 
  dbms_output.put_line('不要納稅');

end if;

end;

--迴圈輸出1-100
declare 
i number(9):=1;
begin
  <<myloop>>
  loop
    dbms_output.put_line(i);
    i:=i+1;
    exit myloop when i=101;
  end loop;
end;

--輸出1-100的和
declare 
i number(8):=1;
sumi number(8):=0;
begin
 while i<=100
    loop
      sumi:=sumi+i;
      i:=i+1;
    end loop;  
   dbms_output.put_line(sumi);
end;

create view emphc as

create or replace procedure pname(pno number)
as
gname varchar2(22);
begin
  select ename into gname from emp
  where empno=pno;
    dbms_output.put_line('姓名:'||gname);
  exception
     when no_data_found then
       dbms_output.put_line('找不到');

end;


begin
 pname(7369);
end;