PL/SQL的类型和JDBC操作数据库

来源:互联网 发布:sqlserver 数据库实体 编辑:程序博客网 时间:2024/06/05 06:36

PL/SQL的标量类型:

   字符,数字,时间,布尔,%type五中类型的

Sql代码  收藏代码
  1. --标量:数据库中预定义类型的变量  
  2. --定义一个变长字符串   
  3. v_ename varchar2(10);   
  4.   
  5. --定义一个小数,范围 -9999.99~9999.99   
  6. v_sal number(6,2);   
  7.   
  8. --定义一个小数并给一个初始值为5.4 :=是pl/sql的赋值号   
  9. v_sal2 number(6,2):=5.4;   
  10.   
  11. --定义一个日期类型的数据   
  12. v_hiredate date;   
  13.   
  14. --定义一个布尔变量,不能为空,初始值为false   
  15. v_valid boolean not null default false;   
  16.   
  17. --定义一个常量,只能赋值一次,不可被修改  
  18. c_num  constant number(5) :=100;  

 

--下面以输入id,显示账号和密码。
--说明变量的使用,看看如何编写。

创建过程

Sql代码  收藏代码
  1. create or replace procedure pro_1(v_userid number) is  
  2. --根据table_pro表中的username来声明变量的数据类型  
  3. v_username table_pro.username%type;  
  4. v_userpwd table_pro.userpwd%type;  
  5. begin   
  6. select username, userpwd into v_username,v_userpwd from table_pro where userid = v_userid;  
  7.   
  8. dbms_output.put_line('账号:'||v_username||'密码:'||v_userpwd);  
  9. end;  

 

打开输入输出

Sql代码  收藏代码
  1. set serveroutput on  

 

调用过程,查询userid为1的账号的密码

Sql代码  收藏代码
  1. exec pro_1(1);  

 

运行结果:

SQL> exec pro_1(1);
 
账号:王佳密码:11

 

 

------------------------------------------------复合类型------------pl/sql表和pl/sql记录------

pl/sql记录;将所有的数据放在记录中,类似java中的方法中的属性

 

定义包; is record表示是pl/sql的表记录

Sql代码  收藏代码
  1. --定义一个函数过程,根据输入的员工编号,返回员工的账号和密码  
  2. create or replace package package_1 is  
  3. type table_type_record is record(  
  4. v_username table_pro.username%type,  
  5. v_userpwd table_pro.userpwd%type  
  6. );  
  7. end;  

 定义过程来调用包;

Sql代码  收藏代码
  1. --由于需要返回多个类型的值,需要定义两个变量接收  
  2. create or replace procedure pro_6(v_userid number) is  
  3. v_record package_1.table_type_record;  
  4. begin  
  5.   select username,userpwd into v_record.v_username,v_record.v_userpwd from table_pro where userid = v_userid;  
  6.    
  7. dbms_output.put_line('账号:'||v_record.v_username||'密码:'||v_record.v_userpwd);  
  8. end;  

 调用

exec pro_6(1);

 

SQL> exec pro_6(1);
 
账号:王佳密码:11

 

 

PL/SQL表;将字段存储在数组中,数组的开始可以为任意的一个数

获取table_pro表中的username字段

Sql代码  收藏代码
  1. SQL> create  or replace procedure pro_4(v_userid number)is  
  2.   
  3.     --定义一个Pl/SQL表类型:类似于数组类型  
  4. --数组类型名是emp_table_ename 存放的数据类型是emp.ename%type  
  5.   
  6. --index by binary_integer下标自增长  
  7.   2  type table_type_username is table of table_pro.username%type index by binary_integer;  
  8.   3  --定义变量数组  
  9.   4  v_username table_type_username;  
  10.   5  begin  
  11.   6   select username into v_username(0) from table_pro where userid=v_userid;  
  12.   7  dbms_output.put_line(v_username(0));  
  13.   8  end;  
  14.   9  --以上会报错,多条数据需要使用参照变量  

 

SQL> exec pro_4(1);
 
王佳
 

以上方法只可以输出一条;两条呢;

使用游标可以解决上面的问题

 

---------------------------------------游标类型 ref cursor

输出多个字段时候使用游标

游标的创建,创建名字为emp_cursor_type的游标

Sql代码  收藏代码
  1. type emp_cursor_type is ref cursor;  

打开游标 open +游标名+ for

Sql代码  收藏代码
  1. open emp_cursor_type for  

 

游标获取部门的所有名字和姓名

Sql代码  收藏代码
  1. create or replace procedure pro_7(v_deptno number) is  
  2. --定义一个游标类型  
  3. type emp_cursor_type is ref cursor;  
  4. --定义游标变量,默认是关闭的  
  5. mycursor emp_cursor_type;  
  6.   
  7. --定义变量接收游标中的数据  
  8. v_ename emp.ename%type;  
  9. v_sal emp.sal%type;  
  10.   
  11. begin  
  12.   --打开游标并执行查询语句,将查询到的结果集交给游标管理  
  13.   open mycursor for select ename,sal from emp where deptno=v_deptno;  
  14.   --循环取出游标中的数据,  
  15.   loop  
  16.          --取游标的数据  
  17.          fetch mycursor into v_ename,v_sal;  
  18.          --当游标中没有数据的时候就结束循环  
  19.          exit when mycursor%notfound;  
  20.          dbms_output.put_line('姓名:'||v_ename||' 工资:'||v_sal);        
  21.   end loop;  
  22. end;  

 


 

JDBC调用过程实现增删改查

1,添加数据

,1,1增加数据的过程

Sql代码  收藏代码
  1. SQL> create or replace procedure pro_add(v_username varchar2,v_userpwd varchar2) is  
  2.   2    
  3.   3  begin  
  4.   4  insert into table_pro values(seq_pro.nextval,v_username,v_userpwd);  
  5.   5  end;  
  6.   6  /  

 

1,2在JDBC中实现相应的方法,过程有几个参数就写几个?

Sql代码  收藏代码
  1. //添加数据  
  2.     public void getAdd(String name,String pwd){  
  3.         try{  
  4.         Connection conn = ConnDB.getCon();  
  5.         //预编译  
  6.         String sql ="{call pro_add(?,?)}";  
  7.       
  8.         CallableStatement csmt = conn.prepareCall(sql);  
  9.         csmt.setString(1, name);  
  10.         csmt.setString(2, pwd);  
  11.         csmt.execute();  
  12.         csmt.close();  
  13.         }catch(Exception ef){  
  14.             ef.printStackTrace();  
  15.         }  
  16.     }  
  17.       
  18. }  

 运行结果;userid前面已经执行了三次,所以现在是4

     USERID USERNAME   USERPWD
----------- ---------- ----------
          4 百合不是茶 baihe

 

2,删除 ,先定义过程再到jdbc中操作过程

 

3,修改 ;先定义过程再到jdbc中操作过程

 

 

4,查询;在jdbc中定义输入输出语句

in输入

out输出

定义过程

Sql代码  收藏代码
  1. SQL> create or replace procedure pro_select(v_userid in number,v_username out varchar2,v_userpwd out varchar2) is  
  2.   2  begin  
  3.   3     select username,userpwd  into v_username,v_userpwd from table_pro where userid = v_userid;  
  4.   4  end;  
  5.   5  /  
  6.    

 jdbc的调用

Java代码  收藏代码
  1. // 获取全部的  
  2.     public void getAll(int id) {  
  3.         try {  
  4.             Connection conn = ConnDB.getCon();  
  5.             // 执行过程  
  6.             String sql = "{call pro_select(?,?,?)}";  
  7.             // 编译sql语句  
  8.             CallableStatement csmt = conn.prepareCall(sql);  
  9.             csmt.setInt(1, id);  
  10.             csmt.registerOutParameter(2, java.sql.Types.VARCHAR);  
  11.             csmt.registerOutParameter(3, java.sql.Types.VARCHAR);  
  12.             // 查询时间的操作  
  13.             // csmt.registerOutParameter(3, java.sql.Types.DATE);  
  14.             // DATE date = (DATE)csmt.getObject(4);  
  15.             // SimpleDateFormat sp = new SimpleDateFormat("yyyy/MM/dd");  
  16.             // String date = sp.format(date);  
  17.             csmt.execute();  
  18.             String name = csmt.getString(2);  
  19.             String pwd = csmt.getString(3);  
  20.             System.out.println("账号:" + name + "密码:" + pwd);  
  21.         } catch (Exception ef) {  
  22.             ef.printStackTrace();  
  23.         }  
  24.     }  

  

表中的数据

     USERID USERNAME   USERPWD
----------- ---------- ----------
          4 百合不是茶 baihe
          5 百合不是茶 baihe
          1 王佳       11
          2 王         1
          3 佳         2

运行的结果;

 demo.getAll(2);

账号:王密码:1

 

------------------------JDBC操作游标获取

创建过程

Sql代码  收藏代码
  1. 查询  
  2. create or replace package package_cursor is  
  3.    type   table_cursor is ref cursor;  
  4. end;  
  5.   
  6. create or replace procedure pro_select(v_userid in number,table_cursor out package_cursor.table_cursor ) is  
  7. begin   
  8.   open table_cursor for select username,userpwd  from table_pro where userid = v_userid;  
  9. end;  

 

jdbc操作

Java代码  收藏代码
  1. // 获取游标的数据  
  2.     public void getCursor(int id) {  
  3.         try {  
  4.             Connection conn = ConnDB.getCon();  
  5.             // 执行过程  
  6.             String sql = "{call pro_select(?,?)}";  
  7.         CallableStatement csmt = conn.prepareCall(sql);  
  8.         csmt.setInt(1, id);  
  9.         csmt.registerOutParameter(2,oracle.jdbc.OracleTypes.CURSOR);  
  10.         csmt.execute();  
  11.         //获取数据,游标类型可以强转成ResultSet  
  12.         ResultSet rs = (ResultSet)csmt.getObject(2);  
  13.         while(rs.next()){  
  14.             String name = rs.getString(1);  
  15.             String pwd = rs.getString(2);  
  16.             System.out.println("账号:" + name + "密码:" + pwd);  
  17.         }  
  18.         } catch (Exception ef) {  
  19.             ef.printStackTrace();  
  20.         }  
  21.     }  

 

运行的结果;

demo.getAll(2);

账号:王密码:1

0 0
原创粉丝点击