Home  >  Article  >  Java  >  How to call stored procedure in java

How to call stored procedure in java

王林
王林Original
2019-12-06 15:12:096208browse

How to call stored procedure in java

How to implement the call to the stored procedure in Java:

1. Without output parameters

create procedure getsum
@n int =0<--此处为参数-->
as
declare @sum int<--定义变量-->
declare @i int
set @sum=0
set @i=0
while @i<=@n begin
set @sum=@sum+@i
set @i=@i+1
end
print &#39;the sum is &#39;+ltrim(rtrim(str(@sum)))

Recommended online video tutorials: javalearning

JAVA can be called, but the results of the stored procedure cannot be displayed in the JAVA program, because the parameter type int of the above stored procedure is passed in ( by value) method.

import java.sql.*;
public class ProcedureTest{
    public static void main(String args[]) throws Exception{   
        //加载驱动   
        DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());   
        //获得连接   
        Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
    //创建存储过程的对象         
    CallableStatement c=conn.prepareCall("{call getsum(?)}");                
    //给存储过程的参数设置值         
    c.setInt(1,100);   //将第一个参数的值设置成100                
    //执行存储过程         
    c.execute();        
    conn.close();
    }
}

2. Call in

alter procedure getsum
@n int =0,
@result int output
as
declare @sum int
declare @i int
set @sum=0
set @i=0
while @i<=@n begin
set @sum=@sum+@i
set @i=@i+1
end
set @result=@sum

java with output parameters:

How to call stored procedure in java

Related article tutorial recommendations: Introduction to java programming

The above is the detailed content of How to call stored procedure in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn