============jdbc==========================================
--java代码:一个输出参数--
String connURL = null;
Connection conn = null;
connURL = "jdbc:sqlserver://192.168.1.157;instanceName=eqmis;DatabaseName=eqmis";
String sqldriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Class.forName(sqldriver);// 注册加载驱动程序
conn = DriverManager.getConnection(connURL, "sa","1.0");
CallableStatement proc = conn.prepareCall("{ call test(?) }"); //只有一个输出参数
proc.registerOutParameter(1,Types.VARCHAR);
proc.execute();
ystem.out.println(proc.getString(1));
--储存过程(mssql)一个输出参数--
if exists(select * from sysobjects where name='test')
drop proc test
go
create proc test
@mycarID char(19)output--输出参数
as
declare @r numeric(15,8),--接收随机数
@tempr varchar(20)--转换随机数
select @r=RAND((datepart(mm,getdate())*100000)+(DATEPART(SS,GETDATE())*1000)+
(DATEPART(MS,GETDATE())))
select @tempr=CONVERT(varchar(20),@r)
select @mycarID= '1010 3576'+' '+SUBSTRING(@tempr,3,4)+' '+SUBSTRING(@tempr,7,4)
return
go
-------java代码:一个输入参数和一个输出参数---
String connURL = null;
Connection conn = null;
connURL = "jdbc:sqlserver://192.168.1.157;instanceName=eqmis;DatabaseName=eqmis";
String sqldriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Class.forName(sqldriver);// 注册加载驱动程序
conn = DriverManager.getConnection(connURL, "sa","1.0");
CallableStatement proc = conn.prepareCall("{ call test(?,?) }");
proc.setString(1,"12345");
proc.registerOutParameter(2,Types.VARCHAR);//第二个是输出参数
proc.execute();
System.out.println(proc.getString(2));
--储存过程(mssql)一个输入参数和一个输出参数---
if exists(select * from sysobjects where name='test')
drop proc test
go
create proc test
@getIndex varchar(5),--输入参数
@mycarID char(19)output--输出参数
as
declare @r numeric(15,8),--接收随机数
@tempr varchar(20)--转换随机数
select @r=RAND((datepart(mm,getdate())*100000)+(DATEPART(SS,GETDATE())*1000)+
(DATEPART(MS,GETDATE())))
select @tempr=CONVERT(varchar(20),@r)
select @mycarID= @getIndex+' '+SUBSTRING(@tempr,3,4)+' '+SUBSTRING(@tempr,7,4)
return
go
=====jap:=================================
-------JPA调用储存过程(mssql)注意:EJB3不能调用以OUT参数返回值的存储过程。--
create proc testJpa2
@acode varchar(20)
as
begin
select * from pubuser where acode=@acode
end
go
-------jap:dao--------------------------
String jpql= "{call testJpa2(?)}";
Query obj = em.createNativeQuery(jpql);
bject[] params=new Object[]{'1'}
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
obj.setParameter(i + 1, params[i]);
}
}
obj.getResultList().toString();