Call stored procedures in hibernate

Source: Internet
Author: User

The ssh1 (spring2.5.6 + hibernate3.4 + struts1.3.8) framework I set up requires a public method to call the stored procedure.
Principle: This is actually unrelated to ssh, mainly because this interface has been provided in java. SQL. Connection. We only get our Connection through the hibernate session.
The specific general interface encoding is as follows:
 
01 /**
02 * directly call the Stored Procedure
03 * @ param procString
04 * <a href = "www.2cto.com" class = "referer" target = "_ blank"> @ author </a> kongqz
05 * @ throws Exception
06 * @ date 2009-03 3
07 ***/
08 public void callProcedure (String procString, List <Object> params) throws Exception {
09 CallableStatement stmt = null;
10 try {
11 stmt = this. getSession (). connection (). prepareCall (procString );
12 if (params! = Null ){
13 int idx = 1;
14 for (Object obj: params ){
15 if (obj! = Null ){
16 stmt. setObject (idx, obj );
17} else {
18 stmt. setNull (idx, Types. NULL );
19}
20 idx ++;
21}
22}
23 stmt.exe cute ();
24} catch (SQLException e ){
25 e. printStackTrace ();
26 throw new Exception ("errors occurred when calling the stored procedure [SQL =" + procString + "]", e );
27
28}
29}
Here, I created two stored procedures in my mysql5 database:
Without parameters:
01 DELIMITER $
02
03 drop procedure if exists 'hdssh '. 'testproc' $
04
05 create procedure 'hdssh '. 'testproc '()
06/* LANGUAGE SQL
07 | [NOT] DETERMINISTIC
08 | {contains SQL | no SQL | reads SQL data | modifies SQL DATA}
09 | SQL SECURITY {DEFINER | INVOKER}
10 | COMMENT 'string '*/
11 BEGIN
12 delete from ss_roles;
13 insert into ss_roles (id, name, descn) values (1, '1', 'sc1' | now ());
14 insert into ss_roles (id, name, descn) values (2, '2', 'desc2' | now ());
15 END $
16
17 DELIMITER;
Parameters:
01 DELIMITER $
02
03 drop procedure if exists 'hdssh '. 'testprocparam' $
04
05 create procedure 'hdssh '. 'testprocparam' (in tmp varchar (255 ))
06/* LANGUAGE SQL
07 | [NOT] DETERMINISTIC
08 | {contains SQL | no SQL | reads SQL data | modifies SQL DATA}
09 | SQL SECURITY {DEFINER | INVOKER}
10 | COMMENT 'string '*/
11 BEGIN
12 delete from ss_roles;
13 insert into ss_roles (id, name, descn) values (1, '1', tmp );
14 insert into ss_roles (id, name, descn) values (2, '2', tmp );
15 END $
16
17 DELIMITER;
The above two mysql5 procedures are to insert some data into some tables.
The next step is to call the method.
Call without parameters:
1 try {
2 this. callProcedure ("{call hdssh. testProc}", params );
3} catch (Exception e ){
4 // TODO Auto-generated catch block
5 throw new Exception (e. getMessage ());
6}
Call with Parameters
1 List params = new ArrayList ();
2 params. add ("test string ");
3 this. callProcedure ("{call hdssh. testProcParam (?)} ", Param );
The above program is integrated into the existing system to complete the call from the hibernate Stored Procedure
Author: suo long

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.