java数据读取和存储过程,【存储过程】从数据库中读取数据保存到文件中

由于初期规划不好,项目管理的action都存入到数据库中了,而实际上应该以配置文件的形式保存的,所以现在想改过来。一条条复制是不可能的,几百条记录,可以用java编写个小程序或者其他语言编写个脚本来实现,都很简单,但因为这两天在学存储过程,所以就试着写一个了(数据库的过程、函数和触发器等真的很好玩啊!)。

其实这个过程只有两个要点:

①如何从数据库中读取出所有action而且没有重复(因为之前很多失误操作,写入很多重复的记录);

②如何把读出来的集合转换成properties的格式并写入文件。

①的查询语句:

select C_ACTION,B_POWER from p_link where I_ID in

(select max(I_ID) from p_link group by C_ACTION);

②循环处理读出来的集合,拼装成一个大串,一次性写入文件:(使用光标来遍历集合)

repeat

fetch c_cursor into tmp_action,tmp_power;

if not i_done then

set c_content = concat(c_content, tmp_action, '=', tmp_power, char(13));

end if;

until i_done end repeat;

最终可得到一行行结构为 XXX=XXX 键值对。

详细代码如下:

drop procedure if exists get_all_links;

delimiter $$

create procedure get_all_links()

begin

declare c_content text;

declare i_done int default 0;

declare tmp_action varchar(100);

declare tmp_power tinyint;

declare c_cursor cursor for

select C_ACTION,B_POWER from p_link where I_ID in

(select max(I_ID) from p_link group by C_ACTION);

declare continue handler for sqlstate '02000' set i_done = 1;

open c_cursor;

set c_content = '';

repeat

fetch c_cursor into tmp_action,tmp_power;

if not i_done then

set c_content = concat(c_content, tmp_action, '=', tmp_power, char(13));

end if;

until i_done end repeat;

close c_cursor;

select c_content into outfile 'D:\\action.properties';

end

$$

delimiter ;

call get_all_links();

drop procedure get_all_links;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值