日期:2014-05-16  浏览次数:20497 次

hibernate调用SQL存储过程进行数据库备份

存储过程:

CREATE PROCEDURE .expbackDatabase1
(
@path varchar(128)
)
AS
begin
BACKUP DATABASE DocProc_IMS_nbyz to
end
GO

java语句:

?

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class CSystemCleanDaoImpl extends CDaoBase{

/**
* 备份当前生产库
* @return
* @throws SQLException
* @throws HibernateException
*/
public boolean expDataBaseUp(String path) throws DaoException, HibernateException, SQLException{
?? Session session = this.getSession();
??? Transaction t = session.beginTransaction();
??? Connection con = session.connection();???
??? String procedure = "{call expbackDatabase1(?)}";
??? CallableStatement cstmt = con.prepareCall(procedure);
??? cstmt.setString(1, path);??
??? cstmt.executeUpdate();???
??? t.commit();
??? session.close();
?? return false;
}
}

?

?

?

调用有返回参数的存储过程:

public String expSeqNo(String opcode,String opdate,int billtype,String seqNo) throws DaoException, HibernateException, SQLException{
?? String no="";
?? Session session = this.getSession();
??? Transaction t = session.beginTransaction();
????
????? Connection con = session.connection();?????
??? //第四个?是out类型的返回参数,要注册一下这个out返回类型的参数

??? String pro = "{call sp_GetSeqNo(?,?,?,?)}";
??
??? CallableStatement cleancs = con.prepareCall(pro);
??? cleancs.setString(1, opcode);??
??? cleancs.setString(2, opdate);??
??? cleancs.setInt(3, billtype);???

//注册out类型的返回参数
??? cleancs.registerOutParameter(4, java.sql.Types.INTEGER);
??? cleancs.executeUpdate();

//接收out类型的返回参数值
??? no=cleancs.getInt(4)+"";
???
??? t.commit();
??? con.close();
??? session.close();
?? return no;
}