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

PL/SQL的标量类型:

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

--标量:数据库中预定义类型的变量
--定义一个变长字符串 
v_ename varchar2(10); 

--定义一个小数,范围 -9999.99~9999.99 
v_sal number(6,2); 

--定义一个小数并给一个初始值为5.4 :=是pl/sql的赋值号 
v_sal2 number(6,2):=5.4; 

--定义一个日期类型的数据 
v_hiredate date; 

--定义一个布尔变量,不能为空,初始值为false 
v_valid boolean not null default false; 

--定义一个常量,只能赋值一次,不可被修改
c_num  constant number(5) :=100;

 

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

创建过程

create or replace procedure pro_1(v_userid number) is
--根据table_pro表中的username来声明变量的数据类型
v_username table_pro.username%type;
v_userpwd table_pro.userpwd%type;
begin 
select username, userpwd into v_username,v_userpwd from table_pro where userid = v_userid;

dbms_output.put_line('账号:'||v_username||'密码:'||v_userpwd);
end;

 

打开输入输出

set serveroutput on

 

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

exec pro_1(1);

 

运行结果:

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

 

 

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

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

 

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

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

 定义过程来调用包;

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

 调用

exec pro_6(1);

 

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

 

 

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

获取table_pro表中的username字段

SQL> create  or replace procedure pro_4(v_userid number)is

    --定义一个Pl/SQL表类型:类似于数组类型
--数组类型名是emp_table_ename 存放的数据类型是emp.ename%type

--index by binary_integer下标自增长
  2  type table_type_username is table of table_pro.username%type index by binary_integer;
  3  --定义变量数组
  4  v_username table_type_username;
  5  begin
  6   select username into v_username(0) from table_pro where userid=v_userid;
  7  dbms_output.put_line(v_username(0));
  8  end;
  9  --以上会报错,多条数据需要使用参照变量

 

SQL> exec pro_4(1);
 
王佳
 

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

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

 

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

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

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

type emp_cursor_type is ref cursor;

打开游标 open +游标名+ for

open emp_cursor_type for

 

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

create or replace procedure pro_7(v_deptno number) is
--定义一个游标类型
type emp_cursor_type is ref cursor;
--定义游标变量,默认是关闭的
mycursor emp_cursor_type;

--定义变量接收游标中的数据
v_ename emp.ename%type;
v_sal emp.sal%type;

begin
  --打开游标并执行查询语句,将查询到的结果集交给游标管理
  open mycursor for select ename,sal from emp where deptno=v_deptno;
  --循环取出游标中的数据,
  loop
         --取游标的数据
         fetch mycursor into v_ename,v_sal;
         --当游标中没有数据的时候就结束循环
         exit when mycursor%notfound;
         dbms_output.put_line('姓名:'||v_ename||' 工资:'||v_sal);      
  end loop;
end;

 


 

JDBC调用过程实现增删改查

1,添加数据

,1,1增加数据的过程

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

 

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

//添加数据
	public void getAdd(String name,String pwd){
		try{
		Connection conn = ConnDB.getCon();
		//预编译
		String sql ="{call pro_add(?,?)}";
	
		CallableStatement csmt = conn.prepareCall(sql);
		csmt.setString(1, name);
		csmt.setString(2, pwd);
		csmt.execute();
		csmt.close();
		}catch(Exception ef){
			ef.printStackTrace();
		}
	}
	
}

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

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

 

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

 

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

 

 

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

in输入

out输出

定义过程

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

 jdbc的调用

// 获取全部的
	public void getAll(int id) {
		try {
			Connection conn = ConnDB.getCon();
			// 执行过程
			String sql = "{call pro_select(?,?,?)}";
			// 编译sql语句
			CallableStatement csmt = conn.prepareCall(sql);
			csmt.setInt(1, id);
			csmt.registerOutParameter(2, java.sql.Types.VARCHAR);
			csmt.registerOutParameter(3, java.sql.Types.VARCHAR);
			// 查询时间的操作
			// csmt.registerOutParameter(3, java.sql.Types.DATE);
			// DATE date = (DATE)csmt.getObject(4);
		    // SimpleDateFormat sp = new SimpleDateFormat("yyyy/MM/dd");
			// String date = sp.format(date);
			csmt.execute();
			String name = csmt.getString(2);
			String pwd = csmt.getString(3);
			System.out.println("账号:" + name + "密码:" + pwd);
		} catch (Exception ef) {
			ef.printStackTrace();
		}
	}

  

表中的数据

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

运行的结果;

 demo.getAll(2);

账号:王密码:1

 

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

创建过程

查询
create or replace package package_cursor is
   type   table_cursor is ref cursor;
end;

create or replace procedure pro_select(v_userid in number,table_cursor out package_cursor.table_cursor ) is
begin 
  open table_cursor for select username,userpwd  from table_pro where userid = v_userid;
end;

 

jdbc操作

// 获取游标的数据
	public void getCursor(int id) {
		try {
			Connection conn = ConnDB.getCon();
			// 执行过程
			String sql = "{call pro_select(?,?)}";
		CallableStatement csmt = conn.prepareCall(sql);
		csmt.setInt(1, id);
		csmt.registerOutParameter(2,oracle.jdbc.OracleTypes.CURSOR);
		csmt.execute();
		//获取数据,游标类型可以强转成ResultSet
		ResultSet rs = (ResultSet)csmt.getObject(2);
		while(rs.next()){
			String name = rs.getString(1);
			String pwd = rs.getString(2);
			System.out.println("账号:" + name + "密码:" + pwd);
		}
		} catch (Exception ef) {
			ef.printStackTrace();
		}
	}

 

运行的结果;

demo.getAll(2);

账号:王密码:1

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值