参考地址: http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chapter/stored-procedures.html#stored-procedure-replication-faq
// 存储过程
DELIMITER $$
DROP PROCEDURE IF EXISTS `fans`.`voteexecute`$$
CREATE
PROCEDURE `fans`.`voteexecute`(
_uuid varchar(100),
_playerid varchar(50),
_userid varchar(50),
_maxnumber int ,
_datestr varchar(50),
out votecount int,
out updateflag int)
BEGIN
declare _votenumber int default 0;/**定义局部变量*/
declare _voteplayer_id varchar(100) default '0';
set updateflag=0;/**赋值*/
select voteuser.votenumber into _votenumber from voteuser where voteuser.userid = _userid and DATE_FORMAT(voteuser.createtime, '%Y-%m-%d') = _datestr;
select _votenumber;/**类似打印日志*/
if _votenumber=0 then
insert into voteuser (id,userid,playerid,votenumber,createtime) values (_uuid,_userid,_playerid,0,sysdate());
end if;
if _votenumber <_maxnumber then
select voteplayer.id into _voteplayer_id from voteplayer where voteplayer.playerid=_playerid order by voteplayer.createtime desc limit 0,1;
if _voteplayer_id!='0' then
update voteplayer set voteplayer.votes = voteplayer.votes+1 where voteplayer.id=_voteplayer_id;
update voteuser set voteuser.votenumber = voteuser.votenumber+1 where voteuser.userid=_userid and DATE_FORMAT(voteuser.createtime, '%Y-%m-%d');
insert into voteuserrecord (id,userid,playerid,createtime) values (_uuid,_userid,_playerid,sysdate());
set updateflag=1;
end if;
end if;
select voteplayer.votes into votecount from voteplayer where voteplayer.playerid=_playerid order by voteplayer.createtime desc limit 0,1;/** select ... into votecount 其中votecount是需要传出的值*/
END$$
DELIMITER ;
SQLyog中调用:
call voteexecute('12','103352951',5,'2012-08-09', @a ,@b);//调用
select @a ,@b;//查看输入结果
JAVA中调用:
try {
Connection conn=MySqlDataSourceFactory.getInstance().getConnection();//获取conn
try{
CallableStatement cs = conn.prepareCall("{call voteexecute(?,?,?,?,?,?,?)}");
cs.setString(1,UUID.randomUUID().toString());
cs.setString(2,playerId);
cs.setString(3,userId);
cs.setInt(4,GlobalVariable.VOTE_MAX);
cs.setString(5,DateFormatUtils.format(new Date(),"yyyy-MM-dd"));
cs.registerOutParameter(6, Types.INTEGER);//输出参数
cs.registerOutParameter(7, Types.INTEGER);//输出参数
cs.execute();
int outInt_1 = cs.getInt(6);
int outInt_2= cs.getInt(7);
cs.close();
}catch (Exception e){
e.printStackTrace();
}
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}