Java調用SQL Server存儲過程


1、調用普通存儲過程
(1)創建存儲過程
CREATE Procedure [dbo].[GetContactListByName]  /*根據聯系人姓名獲取聯系人信息*/
@Name nvarchar(50)
As
begin
    select Contact.Id,Contact.Name,Phone,Email,QQ,GroupName from Contact,ContactGroup
 where Contact.GroupId=ContactGroup.Id and Name like '%'+@Name+'%'  order by Contact.Id desc
end
(2)Java代碼
final String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
final String DATABASE_URL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=AddressList";
final String DATABASE_USRE = "sa";
final String DATABASE_PASSWORD = "1234";
try {
    Class.forName(DRIVER_CLASS);
    Connection connection=DriverManager.getConnection(DATABASE_URL,DATABASE_USRE,DATABASE_PASSWORD);
    CallableStatement callableStatement=connection.prepareCall("{call GetContactListByName(?)}");
    callableStatement.setString(1, name);
    ResultSet resultSet=callableStatement.executeQuery();
    while(resultSet.next()){
        int id=resultSet.getInt(1);
        String string=resultSet.getString(2);
        System.out.println(id+","+string);
    }
} catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}
注意:如果存儲過程無參數,則不需要寫括號,如
CallableStatement callableStatement=connection.prepareCall("{call GetAllContactGroup}");
        
2、調用包含返回值及輸出參數的存儲過程
(1)創建存儲過程
USE [AddressList]
GO
CREATE PROCEDURE [dbo].[GetGroupById]  /*根據分組編號獲取分組信息*/
 @GroupName nvarchar(50) OUTPUT,   /*輸出參數*/
 @Memo nvarchar(200) OUTPUT,      /*輸出參數*/
 @id int
AS
BEGIN
 select @GroupName=GroupName,@Memo=Memo from ContactGroup where id=@id
 if @@Error<>0
  RETURN -1   /*返回值*/
 else
  RETURN 0   /*返回值*/
END
(2)Java代碼
    CallableStatement callableStatement=connection.prepareCall("{?=call GetGroupById(?,?,?)}");
    //返回值
    callableStatement.registerOutParameter(1, Types.INTEGER);
    //輸出參數
    callableStatement.registerOutParameter(2, Types.VARCHAR);
    //輸出參數
    callableStatement.registerOutParameter(3, Types.VARCHAR);
    //輸入參數
    callableStatement.setInt(4, 2);
    callableStatement.execute();
    //獲得返回值
    int returnValue=callableStatement.getInt(1);
    //獲得輸出參數
    String groupName=callableStatement.getString(2);
    //獲得輸出參數
    String memo=callableStatement.getString(3);
    System.out.println(returnValue);
    System.out.println(groupName);
    System.out.println(memo);
} catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}
3、調用包含輸入輸出參數的存儲過程。
(1)創建存儲過程
USE [AddressList]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[test]
 @GroupName nvarchar(50) output
AS
BEGIN
 select @GroupName=GroupName from ContactGroup where GroupName like '%'+@GroupName+'%'
END
(2)Java代碼
CallableStatement callableStatement=connection.prepareCall("{call test(?)}");
callableStatement.setString(1, name);
callableStatement.registerOutParameter(1, Types.VARCHAR);
callableStatement.execute();
String string=callableStatement.getString(1);
System.out.println(string);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM