Java????SQL Server??洢???????
???????????? ???????[ 2014/4/11 9:38:33 ] ????????Java SQL ?洢
????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??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11