Java source code: [mw_shl_code=java,true]import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Types;
public class proc_test {
/** * @param args * IT Forum:www.itsvse.com */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; CallableStatement cstmt=null;
try { Set the driver type Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Create a connection object conn=DriverManager.getConnection( "jdbc:sqlserver://localhost:1433; database=sqlDB", "sa", "123"); sql statement String sql="{call proc_test(?,?,?)}"; Create an object that executes the stored procedure cstmt=conn.prepareCall(sql); Set the parameters of the stored procedure cstmt.setInt(1, 20); cstmt.setInt(2, 30); cstmt.registerOutParameter(3, Types.INTEGER); Execute stored procedures cstmt.execute(); Get the return value int num=cstmt.getInt(3); System.out.println(num);
} catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
} [/mw_shl_code]
SQL Server code: [mw_shl_code=sql,true]--Create database create database sqlDB go --switch databases use sqlDB go --Create stored procedures create proc proc_test @a int, @b int, @num int output as set @num=@a+@b
--Execute stored procedures declare @add int exec proc_test 1,2,@add output print @add [/mw_shl_code]
存储过程和配置文件.rar
(287.1 KB, Number of downloads: 0, Selling price: 2 Grain MB)
|