Java调用SQL Server存储过程

时间:2021-11-08 05:56:03

1.建立SQL Server存储过程

CREATE PROCEDURE prepareStatement
@USR_CODE varchar(128)
AS
BEGIN
select * from [user] u where u.[USR_CODE]=@USR_CODE
END

 

2.在SQL Server中测试存储过程

EXEC prepareStatement 'test'

3.Java代码

public List<Role> getRoleByUserCode(String userCode){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Role> list = new ArrayList<Role>();

try {
conn = dataSource.getConnection();
//ps = conn.prepareStatement("select * from [user] u where u.[USR_CODE]=?");
ps = conn.prepareStatement("{call dbo.prepareStatement(?)}");
ps.setString(1, userCode);
rs = ps.executeQuery();

以上

但愿诸兄看明白了..


参考文章:

1.存储过程入门与提高

2.SqlServer中存储过程中将Exec的执行结果赋值给变量输出

3.Java调用SQL Server的存储过程详解