????1????ò?????????洢????

??????? JDBC ??????????ò?????????洢?????????????? call SQL ??????С??????????? call ??????е????????????

{call procedure-name}

?????????????? SQL Server 2005 AdventureWorks ?????????д??????′洢?????

    CREATE PROCEDURE GetContactFormalNames 
    AS
    BEGIN
     SELECT TOP 10 Title + "" "" + FirstName + "" "" + LastName AS FormalName 
     FROM Person.Contact 
    END

??????洢????????????????????а????????????? Person.Contact ????????????????????????????????

???????????????У????????????? AdventureWorks ????????????????????? executeQuery ???????? GetContactFormalNames ?洢?????

    public static void executeSprocNoParams??Connection con?? ...{ 
     try ...{ 
     Statement stmt = con.createStatement????; 
    ResultSet rs = stmt.executeQuery??"{call dbo.GetContactFormalNames}"??; 
     while ??rs.next?????? ...{ 
    ??System.out.println??rs.getString??"FormalName"????; 
    } 
    rs.close????; 
    stmt.close????; 
    ?? } 
    catch ??Exception e?? ...{ 
    e.printStackTrace????; 
    } 
    }

????2?????????????????洢????

??????? JDBC ??????????????????洢????????????? SQLServerConnection ??? prepareCall ??????? call SQL ??????С????? IN ?????? call ??????е????????????

{call procedure-name[??[parameter][??[parameter]]...??]}

???????? call ??????????????? ???????????????? IN ?????????????????????洢????????????λ??????????? SQLServerPreparedStatement ??? setter ??????????????????????? setter ?????? IN ???????????????????

?????? setter ???????????????????????????????????????????????????????洢?????е?????λ?á????磬????洢??????????? IN ???????????????? 1??????洢????????????????????????????? 1????????????? 2??

?????????ε??e??? IN ??????洢????????????? SQL Server 2005 AdventureWorks ?????????е? uspGetEmployeeManagers ?洢???????洢?????????? EmployeeID ????????????????????????????????????????? EmployeeID ?????????侭??????б????????????洢????? Java ????

    public static void executeSprocInParams??Connection con?? ...{ 
     try ...{ 
     PreparedStatement pstmt = con.prepareStatement??"{call dbo.uspGetEmployeeManagers??????}"??; 
     pstmt.setInt??1?? 50??; 
     ResultSet rs = pstmt.executeQuery????; 
     while ??rs.next?????? ...{ 
     System.out.println??"EMPLOYEE:"??; 
     System.out.println??rs.getString??"LastName"?? + "?? " + rs.getString??"FirstName"????; 
     System.out.println??"MANAGER:"??; 
     System.out.println??rs.getString??"ManagerLastName"?? + "?? " + rs.getString??"ManagerFirstName"????; 
     System.out.println????; 
     } 
     rs.close????; 
     pstmt.close????; 
     } 
     catch ??Exception e?? ...{ 
     e.printStackTrace????; 
     } 
    }

????3?????????????????洢????

??????? JDBC ??????????????洢????????????? SQLServerConnection ??? prepareCall ??????? call SQL ??????С????? OUT ?????? call ??????е????????????

{call procedure-name[??[parameter][??[parameter]]...??]}

???????? call ??????????????? ???????????????? OUT ?????????????????洢???????????????λ?????? OUT ???????????????????д洢???????? SQLServerCallableStatement ??? registerOutParameter ????????????????????????

??????? registerOutParameter ????? OUT ???????????????? java.sql.Types ???????? JDBC ?????????????????????????? SQL Server ?????????????й? JDBC ?? SQL Server ????????????????????????? JDBC ?????????????????

???????????? OUT ?????? registerOutParameter ???????????????????????????????????????????????????????洢???????????????????λ?????????????????磬????洢??????????? OUT ???????????????? 1;????洢????????????????????????????? 1????????????? 2??