/**java
建立存儲過程
sql
*/
.net
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 'the sum is '+ltrim(rtrim(str(@sum)))對象
/**get
java調用存儲過程io
*/
class
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);
//註冊存儲過程的第二個參數
c.registerOutParameter(2,java.sql.Types.INTEGER);
//執行存儲過程
c.execute();
//獲得存儲過程的輸出參數值
System.out.println (c.getInt(2));
conn.close();
}
}import