oracle 基本的语句写法

grant select on emp to xiaoming // 把某个用户上的dep 表的select 权限授予给用户xiaoming

grant all on emp to xiaoming // 就是把对emp的所有权限都给xiaoming

select * from scott.emp // 通过xiaoming用户查找scott用户下的emp表


revoke select on emp from xiaoming //撤销emp表 给xiaoming的select 的权限


grant select on emp to xiaoming with grant option; //授权给小明,并且授权此权限的传递权限

grant select on scott to wanghong ; //通过xiaoming用户登录,然后可以把被授予emp的option 权限,对select进行传递授权给另外的用户,如wanghong, 记住授予了什么权限就只能传递什么权限。


conn system/manager; //通过system 用户登录/密码为manager

create profile aaa1 limit failed_login_attemps 3 password_lock_time 2; //建立一个profile 名字为aaa1 ,表示当用户试图 登录 3次,没有成功,则将被锁定2 天。

alter user xiaoming profile aaa1 ; //表示把 用户xiaoming 中的profile 修改为aaa1; 将有了aaa1上面的定下的规则

alter user xiaoming account unlock; //当输入密码错误,用户被锁 ,解锁的命令就这样。


create profile bbb limit password_history limit password_life_time 10 password_grace_time 2 password_reuse_time 10 //当过10 天后,必修改密码,宽限2 天,过10 天后可以修改重复密码



-----------------------------
表的建立


表建好后的修改

alter table student add(classId number(2)); // 表示添加一个表的字段,

alter session set nls_date_format='yyyy-mm-dd' //修改日期格式


insert into student (xh,xm,bj) values(2,'zhanghua','3班') // 选择要插入的个别 值;

select * from student where bj is null; //查询班级 为空的 ; bj is not null;//非空


update student set bj='3班' where xm='xiaoming';


delete from student // 删除表的数据

savepoint aa // 保存回滚点 aa;

rollback to aa // 回滚到 aa 点;


truncate table student ; //比delete 的速度快,但是相比他 ,不能恢复 ;



desc student ; //查看表的结构

select distinct xm from student; //查找独立的姓名;

select nvl(nl,12) from student; // 表示年龄为空,则 年龄为12;

select xm ,bj from student where sr> '1-1月-1982'; // 查询生日 大于 1982,1,1 的学生。

select xm,bj from student where xm like '__o%' // 姓名第三个为o 的。


select xm,bj from student order by nl,xh desc; // 学生先按照 年龄升序,然后学号降序;也可以按照别名排序;

select xm "姓名" , bj "班级" from student order by "姓名" ; //按照别名排序;

select max(nl),min(nl) from student ;// 学生的最高年龄和最低年龄;

select ename ,sal from emp where sal=(select max(sal) from emp); //通过最大的薪水 ,找到是谁;


select arg(sal),max(sal),deptno f

rom emp group by deptno // 部门分组,的每个部门的平均工资,最高工资

select arg(sal),max(sal) ,deptno ,job from emp group by deptno,job;


select arg(sal),max(sal),deptno from emp group by deptno having avg(sal)>2000 ;// 通过分组,查询出来的平均工资大于2000 的,显示出来;


group by ,having ,order by 是这样的顺序

select arg(sal),max(sal) ,deptno ,job from emp group by deptno,job having avg(sal)>2000 order by avg(sal) desc; //分组, 条件限制, 排序


select a1.ename,a1.sal,a2.grade from emp a1,salgrade a2 where a1.sal between a2.losal and a2.hisal; // 工资在 losal和hisal 之间的范围内

in // 表示 在 -- 中有的
select sal from emp where sal in (2000,3000,4000);

select ename ,sal deptno from emp where sal > all(select sal from emp where deptno=30); //在emp中,找出比部门为30 的薪水 都要高的,

select ename ,sal deptno from emp where sal > any(select sal from emp where deptno=30); //在emp中,只要找出比部门为30 的薪水 任何一个 要高的就行了,


select ename ,sal deptno from emp where (deptno,job)=(select deptno,job from emp where ename='STHMIS'); //在emp中,只要找出姓名为smith的人相同的部门编号和职位的记录




--------------------------oracle 分页-------------------------------------

select a1.* ,rownum rn from (select * from emp) a1;


select * from (select a1.* ,rownum rn from (select * from emp) a1 where rownum<=10) where rn>=6

select * from ( select rownum as r, tab.* from ( select rownum, t.* from vw_billhead3128 t where 1 = 1 ) tab ) row_ where row_.r > 20 and row_.r <= 40



---------------------------------------------------

create table myemp2 (id,ename,sal) as select empno,ename,sal from emp; //通过另外的表的数据 建立一个表,结构,数据都导进去了。



insert into myemp (id,ename,deptno) select empno,ename,deptno from emp where deptno=3002 //将一个表了满足条件的导入的另外一个表里去


update emp set (job,sal,comm)=(select job,sal,comm from emp where ename='SMITH') where ename='SCOTT' //按照条件更新语句 ,并且更新多个数据
//---------------------------------------合并-------------------------

select ename,sal,job from emp where job='Manager' union select ename,sal,job from emp where sal>2500 ;// 取并集, union all --取并集的和集, minus ---减去包含 的集合 ,intersect --取交集




--------------------------java ---连接oracle 数据库的方式

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dblocal1", "1");
Statement sm = conn.createStatement();
ResultSet rs = sm.executeQuery("select * from message_route");

while(rs.next()){
System.out.println("批量发送队列:"+rs.getString(5));
}

rs.close();
s

m.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

-----------------------------------------------------------------------


insert into stud values('2200','ph',to_date('11-12-13','yy-mm-dd')) // 时间改变格式的方式


-------------------------------------------事务的处理-------

try{
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dblocal1", "1");
conn.setAutoCommit(false);
------
------
-----
https://www.360docs.net/doc/dd198602.html,mit();
}catch (Exception e){
conn.rollback();
}


-------------------------------------------------只读事务---------------
set transaction read only; // 设置此对象对所有表的只读性。就在此点的数据不变,其他的用户可以操作他们的对应的表。


---------------upper,lower,length,substr(char,int,int)----------replace------------------------------------------
select upper(substr(ename,1,1))||lower(substr(ename,2,length(ename)-1)) from emp // 表示把姓名的首字母大写,其他字母小写。


select replace(ename,'a','替换成b') from emp // 把姓名为a 的替换成 ‘替换成b'


-----------------round(n,[m]),trunc(n,[m]),mod(m,n),floor(n),ceil(n)-----
select rount(comm,1) from emp where ename='shunping' // 对comm 进行四舍五入 保留一位小数

select trunc(comm,1) from emp where ename='shunping' // 截取到小数位为1 ,不管以后有几位都是舍去

select floor(comm) from emp where ename='shunping' // 表示向下取整数。如 44.44 则是44 , 44.68 则是44
select ceil(comm) from emp where ename='shunping' // 表示向上取整数。如 44.44 则是45 , 44.68 则是45

select mod(10,2) from dual // 在做oracle测试 时 ,又来举例 //mod(10,3) = 1 ,取余数


-----------------------------------add_months(hiredate,6)------------
select * from emp where sysdate>add_months(diredate,300) // diredate+300 <系统时间 的条件

select * from trunc(sysdate_hiredate) "入职时间 from emp; // 入职时间

select * from emp where last_day(hiredate)-2=hiredate; // 入职的当月的最后一天—2 = 他的入职时间

--------------------------------------------to_char(date,'yyyy-mm-dd hh24:mi:ss)-----

select ename ,to_char(hiredate,'YYYY-mm-dd hh12:mi:ss) from emp // 时间格式的转换成字符串

select ename ,to_char(hiredate,'YYYY-mm-dd hh12:mi:ss) ,to_char(sal,L99999.99) from emp // L :本地货币符号


select * from emp where to_char(hiredate,'yyyy')=1988 ; // 表示是1988年入职的


---------------------------------------------------------sys_context ------系统函数的用法----

select sys_context('USERENV','db_name') from dual // 当前用户正在使用哪个数据库



----------------------------------导入,导出----------------------

exp userid=scott/tiger@myorcl tables=(emp,s

tudent) file=d:\w.dmp

exp userid=scott/tiger@myorcl tables=(emp,student) file=d:\w.dmp rows=n //只导出表的结构

exp userid=system/manager@myorcl tables=(scott.emp,scott.student) file=d:\w.dmp //导出下一级的用户的数据

exp userid=scott/tiger@myorcl tables=(emp,student) file=d:\w.dmp direct=y //当表特别大,直接导出,速度块

exp userid=scott/tiger@myorcl owns=scott file=d:\w.dmp // 导出是 scott用户的方案

exp userid=system/manager@myorcl owner(system,scott) file=d:\w.dmp // 导出两个用户的数据方案

exp userid=system/manager@myorcl full=y inctype=complete file=d:\w.dmp // inctype=complete 增量备份, full=y 完全导出

imp userid=scott/manager@myorcl tables=(emp) file=d:\w.dmp // 导入表emp到用户为scott 中

imp userid=scott/tiger@myorcl tables=(emp,student) file=d:\w.dmp rows=n //只导入表的结构

imp userid=scott/tiger full=y file=d:\w.dmp



-----------------------------------------------------------数据字典------------------

select table_name from user_tables; // 用户拥有的表

select table_name from all_tables;// 所有,此用户能查看的表

select table_name from dba_tables ; // 查询 必须拥有 dba角色的权限

desc dba_users;

select username from dba_users; // 查询用户 : dba_sys_privs :显示用户具有的系统的权限 。
dba_role_privs: 角色权限 。dba_tab_privs :对象权限。 system_privilege_map:系统权限。dba_roles:所有角色

select * form dba_sys_privs where prantee='CONNECT'; //一个角色拥有 的系统权限

或 select * from role_sys_privs where role='CONNECT'; //一个角色拥有 的系统权限

//某个用户具有什么样的角色
select * from dab_role_privs where grantee='用户名'; //某个用户具有的角色

select * from dba_tab_privs where grantee='CONNECT'; // 一个角色拥有的 对象权限

select * from global_name //现在用的是哪个数据库




----------------------------------------------建立表空间---------------------

Create tablespace data01 datafile 'd:\test\data01.dbf' size 20m uniform size 180k //建立表空间 uniform size 表示区的大小。 表分为 : 表-》段-》区-》块





create table mypart(depton number2),dname varchar2(14),loc varchar2(13)) tablespace data01 ;// 创建表,在表空间里面。

alter tablespace 表空间名 offline; // 表空间 不可用

alter tablespace 表空间 online // 表空间 联机 ,可用

alter tablespace sp001 read only;
alter tablespace sp001 read write; // 表空间可读可写;

select * from all_tables where tablespace_name='表空间名'; // 查询表空间名下的所有表
select tablespace_name ,table_name from user_tables where table_name='emp' // 查找表名 在哪个表空间

drop tablespace '表空间名' // 删除表空间


alter tablespace sp001 add datafile 'd:\sp002.dbf' size 300m ;

//当空间不足时,增加表空间


//-----------------------如果磁盘被毁坏了,移动数据文件的方法------------

select tablespace_name from dba_data_files where file_name='d:\sp001.dbf'; //确定数据文件所在的表空间

alter tablespace sp001 offline; //是表空间为脱机状态

host move d:\test\sp001.dbf c:\test\sp001.dbf //使用命令移动数据文件到指定的目标位置

alter tablespace sp001 rename datafile 'd:\sp001.dbf to 'c:\sp001.dbf'; // 物理移动后,必须对表空间的逻辑进行修改

alter tablespace sp001 online; // 改变sp001的状态。

---------------------------------------------建表--------------------

create table goods (goodsId char(8) primary key,
goodsName varchar2(30),
unitprice number(10,2) check(unitprice>0),
category varchar2(8),
provider varchar2(30));


create table customer(customerId char(8) primary key,
name varchar2(50) not null,--不为空
address varchar2(50),
email varchar2(50) unique,
sex char(2) default '男' check (sex in ('男','女')),
cardId char(18));



create table purchase(customerId char(8) references customer(customerId),--外键 引用
goodsId char(8) references goods(goodsId),
nums number(10) check (nums between 1 and 30));


alter table goods modify goodsName not null; // 商品名不允许为空


alter table customer add constraint cardunique unique(cardId); //增加 约束 ,身份证 不能相同

alter table customer add constraint addresscheck check (address in('东城','西城'));//增加约束 ,满足要求的地方

alter table customer drop constraint 约束名; //删除 约束
alter table customer drop primary key ; //删除主键

select * from user_constraint where table_name='表名'; //查看表的所有约束

select column_name,position from user_cons_columns where constraint_name='约束名' //显示约束的列




------------------------索引------------------------------------
create index nameIndex on customer(name); // 创建索引 通过name
create index nameIndex on customer(name,catgory)

select index_name,index_type from user_indexs where table_name='student'; 显示当前用户的索引信息。

select table_name,column_name from user_ind_columns where index_name='IND_ENAME' // 可以显示索引对应的列的信息


grant update on emp(sal) to monkey // 把跟新 emp(sal) 字段的权限给了monkey

grant index on scott.emp to blake // 把索引权限给 blake

----------------------role 角色操作----------------------------------
create role addrole not identified; // z增加角色 addrole ,不要验证

create role addrole identified by password; 增加角色 addrole ,要验证

grant create session to 角色名 with admin option; // 授权给角色

grant 角色名 to 用户 //分配角色给用户

drop role 角色名 // 角色被删除

select * from dba_roles; // 显示角色
select granted_role,

default_role from dba_role_privs where grantee='用户名'; // 想知道用户拥有那个角色


--------------------------------存储过程-----------------------

create table mytest(name varchar2(30),password varchar2(30));

create procedure sp_pro1 is
begin
-- 执行部分
insert into mytest values ('penghui','peng');
end;
/


create or replace procedure sp_pro1 is --replace 如果有sp_pro1的名字 则 替换他 的意思
begin
-- 执行部分
insert into mytest values('penghui','peng');
end;
/

如果有错误,则 show error; //查看错误


如何调用存储过程
1,exec 过程名(参数1,参数2,...);
2, call 过程名(参数1,参数2,...);


命名规范:
1,变量--- :v_sal // v_
2,常量 ----: c_rate // c_
3,游标 ---:emp_cursor; // _cursor
4,例外 -- : e_error; // e_



--- 结构示意:
declear -- 定义部分

begin -- 执行部分
exception --例外部分
end: -- 结束部分



-- 块的实例
set serveroutput on --打开输出选项
begin
dbms_output.put_line('hello,world');
end;


declare
v_ename varchar2(5);-- 定义字符串变量
begin
select ename into v_ename from emp where empno=&no; -- &no :表示要从控制台输入。into v_ename : 把查询结果给 v_ename;
dbms_output.put_line('雇员名:'|| v_ename);
end;
/


declare
v_ename varchar2(5);-- 定义字符串变量
begin
select ename into v_ename from emp where empno=&no; -- &no :表示要从控制台输入。into v_ename : 把查询结果给 v_ename;
dbms_output.put_line('雇员名:'|| v_ename);
end;
/



declare
v_ename varchar2(5);-- 定义字符串变量
v_sal number(7,2);
begin
select ename,sal into v_ename,v_sal from emp where empno=&no; -- &no :表示要从控制台输入。into v_ename : 把查询结果给 v_ename;
dbms_output.put_line('雇员名:'|| v_ename || '工资:' || v_sal);
end;
/

-- 例外 处理
declare
v_ename varchar2(5);-- 定义字符串变量
v_sal number(7,2);
begin
select ename,sal into v_ename,v_sal from emp where empno=&no; -- &no :表示要从控制台输入。into v_ename : 把查询结果给 v_ename;
dbms_output.put_line('雇员名:'|| v_ename || '工资:' || v_sal);

exception
when no_data_found then
dbms_output.put_line('你的编号输入错误!!');

end;
/


// 过程中调用参数

create procedure sp_pro3(spName varchar2,newSal number) is
begin
-- 执行部分
update emp set sal=newSal where ename=spName;
end;
/

// 调用存储过程:
exec sp_pro3('wangming',333);


--------------------在java 中调用存储过程。

connection conn = DriverManager.getconnection('''''_);

//创建 CallableStatement
CallableStatement cs = conn.prepareCall("{call sp_pro3(?,?)}");
cs.setString(1,"SMITH");
cs.setInt(2,10);

// 执行
cs.execute();
cs.close();
conn.close();

-----------------------函数---


create function sp_fun2(spName varchar2) retrun number yearSal number(7,2);
begin
select sal*12+nvl(comm,0)*12 into yearSal from emp where ename=spName;
return yearSal;

end;
/


var abc number;
call sp_fun2('scott') into:abc;

-------------------------------- 包创建--
create package sp_package is
procedure update_sal(name varchar2,newsal number);
function annual_income(name varchar2) return number;
end;


-- 给 包 sp_package 实现包体
create package body sp_package is
procedure update_sal(name varchar2,newsal number) is
begin
update emp set sal=newsal where ename=name;
end;
function annual_income(name varchar2)
return number is annual_salary number;
begin
select sal*12+nvl(comm,0) into annual_salary from emp
where ename=name;
return nanual_salary;
end;
end;



call sp_package.update_sal('scott',120);



declare
c_tax_rate number(3,2):=0.03;
-- 用户名
v_ename varchar2(5);
v_sal number(7,2);
v_tax_sal number(7,2);
begin
--执行
select ename,sal into v_ename,v_sal from emp where empno=&no;
v_tax_sal:=v_sal*c_tax_rate;
--输出
dbms_output.put_line('姓名是:'||v_ename||'工资:'||v_sal||'交税'||v_tax_sal
end;




declare
c_tax_rate number(3,2):=0.03;
-- 用户名
v_ename emp.ename%type; --表示类型大小与emp中ename字段一致
v_sal number(7,2);
v_tax_sal number(7,2);
begin
--执行
select ename,sal into v_ename,v_sal from emp where empno=&no;
v_tax_sal:=v_sal*c_tax_rate;
--输出
dbms_output.put_line('姓名是:'||v_ename||'工资:'||v_sal||'交税'||v_tax_sal
end;



//---------------创建一个记录类型

declare
type emp_record_type is record(name emp.ename%type,salary emp.sal%type,title emp.job%type)
sp_record emp_record_type;
begin
select ename,sal,job into sp_record from emp where empno=7788;
dbns_output.put_line('员工名'||sp_https://www.360docs.net/doc/dd198602.html,);
end;
/


----- 创建 表实例 , 数组
declare
type sp_table_type is table of emp.ename%type index by binary_integer;
sp_table sp_table_type;
begin
select ename into sp_table(0) from emp where empno=7788;
dbms_output.put_line('员工名'||sp_table(0));
end;
/

------------------------------------游标的定义-----------
declare

type sp_emp_cursor is ref cursor; --定义游标
--定义游标变量
test_cursor sp_emp_cursor;

v_ename emp.ename%type;
v_sal emp.sal%type;
begin
open test_cursor for select ename,sal from emp where deptno=&no;

--循环取出
loop
fetch test_cursor into v_ename,v_sal; --取出游标指向的一行数据。
--判断是否test_cursor 是否为空了
exit when test_cursor%notfound;-- 为空了,就跳出循环
dbms_output.put_line('名字:'||v_ename||'工资:'||v_sal)
end loop;

end;


----------------------------判断语句 if --then ---

create or replace procedure sp_pro6(spName varchar2) is
v_sal emp.sal%type;
begin
select sal into v_sal from emp where ename=spName;
if v_sal<2000 then
update em

p set sal=sal+sal*10% where ename=spName;
end if;
end;
/



create or replace procedure sp_pro6(spName varchar2) is
v_sal https://www.360docs.net/doc/dd198602.html,m%type;
begin
select comm into v_comm from emp where ename=spName;
if v_sal<>0 then
update emp set comm=comm+100 where ename=spName;
else
update emp set comm=comm+200 where ename=spName;
end if;
end;
/


----------------------多重条件分支 if-then - elsif -else --

----------------循环-------


create or replace procedure sp_pro6(spName varchar2) is
v_num number:=1;
begin
loop
insert into users values(v_num,spName);
exit when v_num=10;
v_num:=v_num+1;
end loop;

end;
/



create or replace procedure sp_pro6(spName varchar2) is
v_num number:=11;
begin
while v_num<=20 loop
insert into users values(v_num,spName);

v_num:=v_num+1;
end loop;

end;
/


//==================for 循环

for i in reverse 1..10 loop
insert into user values(i,'peng');
end loop;
end;
/




//--- goto 语句
goto mygoto;

---

--

<>



---存储过程 输入参数

create or replace procedure pro_book(spid in number,spname in varchar2 ,sphouse in varchar2) is
begin
insert into book values(spid,spname,sphouse);
end;


------在java 里调用的过程
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dblocal1", "1");
CallableStatement st = conn.prepareCall("{call pro_book(?,?,?)}");
st.setInt(1, 1);
st.setString(2, "绿豆");
st.setString(3, "火红的太阳");
st.execute();


st.close();
conn.close();


------------存储过程 输出参数

create or replace procedure pro_book1(spid in number,name out varchar2 ) is
begin
select bookname into name from book where bookid=spid;
end;

-----在java 里调用 的过程

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dblocal1", "1");
CallableStatement st = conn.prepareCall("{call pro_book1(?,?)}");
st.setInt(1, 1);
st.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);
st.execute();
String name = st.getString(2);
System.out.println("the book's name is "+name);

st.close();
conn.close();


-----------------存储过程,加两个输出参数的写法
create or replace procedure pro_book1(spid in number,name out varchar2 ,house out varchar2) is
begin
select bookname, bookhouse into name,house from book where bookid=spid;
end;



----

-- 返回结果集的过程
-- 1创建一个包,在该包中定义一个游标
create or replace package testpackage as
type test_cursor is ref cursor;
end testpackage;

--2 创建存储过程
create or replace procedure sp_book3
(spname in varchar2,sp_cursor out testpackage.test_cursor) is
begin
open sp_cursor for select * from book where bookname=spname;
end;


--3 如何在java中调用

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dblocal1", "1");
CallableStatement st = conn.prepareCall("{call sp_book3(?,?)}");
st.setString(1, "绿豆");
st.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
st.execute();


ResultSet rs = (ResultSet)st.getObject(2);
String name ="";
String house="";
while(rs.next()){

name = rs.getString(2);
house = rs.getString(3);
System.out.println("the book's name is "+name+" /n the house is "+house);

}




-------
----分页的存储过程

create or replace procedure fenye
(tablename in varchar2,
pagesize in number,
pagenow in number,
myrows out number,--总的记录数
mypagecount out number,--总页数
p_cursor out testpackage.test_cursor --返回的记录集
) is
--定义部分
--定义sql语句 字符串
v_sql varchar2(1000);
v_begin number:=(pagenow-1)*pagesize+1;
v_end number:=pagenow*pagesize;
begin
--执行部分
v_sql:='select * from (select rownum rm,a.* from (select * from '||tablename||' where 1=1)a)b where b.rm between '||v_begin||' and '||v_end;
-- 把游标和sql关联
open p_cursor for v_sql;
--计算myrows和mypagecount
--组织一个sql
v_sql:='select count(*) from '||tablename;
--执行sql,并把返回的值,赋给myrows;
execute immediate v_sql into myrows;
--计算mypagecount
if mod(myrows,pagesize)=0 then
mypagecount:=myrows/pagesize;
else
mypagecount:=myrows/pagesize+1;
end if;
--关闭游标
--close p_cursor;
end;



---java 的方法

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dblocal1", "1");
CallableStatement st = conn.prepareCall("{call fenye(?,?,?,?,?,?)}");
st.setString(1,"book");
st.setInt(2,3);
st.setInt(3,1);
st.registerOutParameter(4, oracle.jdbc.OracleTypes.INTEGER);
st.registerOutParameter(5, oracle.jdbc.OracleTypes.INTEGER);
st.registerOutParameter(6, oracle.jdbc.OracleTypes.CURSOR);
st.execute();

int rows = st.getInt(4);
int pages= st.getInt(5);
ResultSet rs = (ResultSet)st.getObject(6);
System.out.println("the total rows is"+rows+" and pages is "+pages);
String name ="";
String house="";
while(rs.next()){

name = rs.getString(2);
house = rs.getString(3);
System.out.println("the book's name is "+name+" /n the house is "+house);

}

// System.out.println("the book's name is "+name+" /n the house is "+house);

st.close();
conn.close();





-----------------例外 的处理
exception
when case_not_found then -- 捕获异常
dbms_output.put_line('case语句没有与'||v_sal||'相匹配的条件');--执行捕获异常后的处理




异常有



invaild_cursor

invalid_number

too_many_rows

zero_divide 2/0 时

login_denide

not_logged_on
storage_error

timeout_on_resource



------------------------?// 例子
create or replace procedure ex_test(id number)
is

-- 定义一个例外
myex exception;
begin
-- 更新用户
update book set bookname='中国人' where bookid=id;
-- sql%notfound 这是表示没有update
-- raise myex 触发myex
if sql%notfound then
raise myex;
end if;
exception
when myex then
dbms_output.put_line('没有更新任何的用户');
end;


-------------------视图的创建

create view myview as select * from book where bookid<5;






// 删除所有的表
select 'drop table ' || table_name ||';'||chr(13)||chr(10) from user_tables;

// 删除所有的view
select 'drop view ' || view_name||';'||chr(13)||chr(10) from user_views;

// 删除所有的sequence
select 'drop sequence ' || sequence_name||';'||chr(13)||chr(10) from user_sequences;

// 删除所有的function

select 'drop function ' || object_name||';'||chr(13)||chr(10) from user_objects where object_type='FUNCTION';

// 删除所有的procedure

select 'drop procedure ' || object_name||';'||chr(13)||chr(10) from user_objects where object_type='PROCEDURE';

// 删除所有的package
select 'drop package ' || object_name||';'||chr(13)||chr(10) from user_objects where object_type='PACKAGE';



select * from dba_tablespaces -- 查看表空间

CREATE TABLESPACE "LTLOBDATA01"
LOGGING
DATAFILE 'F:\oradata\fasp\LTLOBDATA01.ORA' SIZE 500M
AUTOEXTEND
ON NEXT 10M MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO
-- 创建表空间

//给用户授予权限
grant connect,resource to username;

相关文档
最新文档