Ⅰ、SQL语句创建存储过程:
CREATE PROCEDURE getPHUser_ID
@name varchar(20),
@pwd varchar(20),
@ID int output
as
if exists (select * from PetHospitalUser where pHUserName=@name and pHUserPassword=@pwd)
begin
select @ID = pHUserID from PetHospitalUser where pHUserName=@name
end
go
Ⅱ、SQL语句执行存储过程:
declare @id int
exec getPHUser_ID '222','222',@id output
print @id
go
Ⅲ、JDBC调用存储过程:
外部调用下面方法,为此方法传递bean的实例用整型变量接受即可!
//调用存储过程通过输入的用户姓名和密码获得用户ID
public static int getPHUserID(PHUserBean user) {
try {
conn = getConn();
casmt =
conn.prepareCall("{call getPHUser_ID (?,?,?)}");
casmt.setString(1,
user.getPHUserName());
casmt.setString(2,
user.getPHUserPassword());
casmt.registerOutParameter(3,
java.sql.Types.INTEGER);
casmt.execute();
count = casmt.getInt(3);
} catch (SQLException ex) {
System.out.println("调用存储过程出错" + ex.getMessage());
return count;
} finally {
try {
if (casmt != null) {
casmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex1) {
System.out.println("关闭失败");
}
}
return count;
}