Hvis du vil have den værdi, der returneres via en OUT-parameter for en lagret procedure, så bruger du ikke et ResultSet, du bruger CallableStatement-parameteren, der er knyttet til OUT-parameteren for den lagrede procedure. For eksempel til testtabellen
CREATE TABLE `allsections_list` (
`SECTION_ID` int(11) NOT NULL,
`SECTION_NAME` varchar(50) DEFAULT NULL,
PRIMARY KEY (`SECTION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
indeholdende prøvedataene
SECTION_ID SECTION_NAME
---------- ---------------
1 one_section
2 another_section
og den lagrede procedure
CREATE PROCEDURE `getSECTION_NAME`(IN myID INT, OUT myName VARCHAR(50))
BEGIN
SELECT SECTION_NAME INTO myName FROM allsections_list WHERE SECTION_ID = myID;
END
derefter følgende Java-kode
try (CallableStatement myFirstCs = conn.prepareCall("{call getSECTION_NAME(?,?)}")) {
myFirstCs.setInt(1, 2); // set IN parameter "myID" to value 2
myFirstCs.registerOutParameter(2, Types.VARCHAR);
myFirstCs.execute();
String sectionName = myFirstCs.getString(2); // get value from OUT parameter "myName"
System.out.println(sectionName);
}
udskrifter
another_section