Use hibernate to call a stored procedure.
Public class StuInfo {
Private int id;
Private String stuName;
Private String stuNo;
Private int stuAge;
Private String stuId;
Private String stuSeat;
Private String stuAddress; setters (); getters ();}
Corresponding database table:
If exists (select * from sysobjects where name = 'stuinfo ')
Drop table stuInfo
Create table stuInfo/* create student information table **/
(
StuName varchar (20) not null, -- name, not empty
StuNo char (6) not null, -- Student ID, not empty
StuAge int not null, -- year long, int Is 4 by default Length
StuId numeric (18, 0 ),
StuSeat smallint, -- seat car, auto-incrementing
StuAddress text -- address can be blank
)
-- Add a column to stuInfo
Alter table stuInfo add id int identity (1, 1) primary key;
Create a stored procedure:
-- Stored Procedure
If exists (select name from sysobjects where name = 'proc _ stuinfo' and type = 'P ')
Drop proc proc_stuInfo
Go
Create proc proc_stuInfo
As
Select * from stuInfo
Go
-- Call a stored procedure
Exec proc_stuInfo;
Several Methods for calling stored procedures in hibernate.
First: name query
<SQL-query name = "getStuInfo" callable = "true">
<Return alias = "stuInfo" class = "com. hkrt. domain. StuInfo">
<Return-property name = "id" column = "id"/>
<Return-property name = "stuName" column = "stuName"/>
<Return-property name = "stuAge" column = "stuAge"/>
<Return-property name = "stuNo" column = "stuNo"/>
<Return-property name = "stuSeat" column = "stuSeat"/>
<Return-property name = "stuAddress" column = "stuAddress"/>
<Return-property name = "stuId" column = "stuId"/>
</Return>
{Call proc_stuInfo ()}
</SQL-query>
List li = session. getNamedQuery ("getStuInfo"). list ();
System. out. println (li. get (0); Type 2: jdbc
System. out. println ("jdbc call -------------");
Connection conn = session. connection ();
ResultSet rs = null;
CallableStatement call;
Try {
Call = conn. prepareCall ("{Call proc_stuInfo ()}");
Rs = call.exe cuteQuery ();
While (rs. next ()){
System. out. println (rs. getString (1 ));
System. out. println (rs. getString (2 ));
System. out. println (rs. getString (3 ));
System. out. println (rs. getString (4 ));
System. out. println (rs. getString (5 ));
System. out. println (rs. getString (6 ));
System. out. println (rs. getString (7 ));
System. out. println ("------------------");
}
} Catch (SQLException e ){
E. printStackTrace ();
}
Third: the simplest one
SQLQuery query = session. createSQLQuery ("{call proc_stuInfo ()}"). addEntity (StuInfo. class );
List list = query. list ();
System. out. println (list. get (0 ));
Note: In the third call, you must add addEntity (); otherwise, no data is returned.