CodeGym /Java Course /All lectures for KO purposes /SQL Server ํ•จ์ˆ˜ ํ˜ธ์ถœ

SQL Server ํ•จ์ˆ˜ ํ˜ธ์ถœ

All lectures for KO purposes
๋ ˆ๋ฒจ 1 , ๋ ˆ์Šจ 819
์‚ฌ์šฉ ๊ฐ€๋Šฅ

CallableStatement

JDBC์—๋Š” ํ›จ์”ฌ ๋” ๋ณต์žกํ•œ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์œ„ํ•œ ๋˜ ๋‹ค๋ฅธ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ PreparedStatement ์—์„œ ์ƒ์†๋˜๋ฉฐ CallableStatement ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค .

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœ(ํ˜ธ์ถœ)ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ˜ธ์ถœ์˜ ํŠน์ง•์€ ResultSet ๊ฒฐ๊ณผ ์™ธ์—๋„ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ด๋Ÿฌํ•œ ์ €์žฅ ํ”„๋กœ์‹œ์ €์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ƒˆ๋กœ์šด ๊ฒƒ์ด ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? PreparedStatement ์—๋Š” ResultSet ๊ฒฐ๊ณผ ๋„ ์žˆ์œผ๋ฉฐ ์—ฌ๊ธฐ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ, ๋งž์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ €์žฅ ํ”„๋กœ์‹œ์ €์˜ ํŠน์ง•์€ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ €์žฅ ํ”„๋กœ์‹œ์ €๋Š” IN , OUT ๋ฐ INOUT ๋งค๊ฐœ๋ณ€์ˆ˜ ๋กœ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค . ํ•˜๋‚˜ ์ด์ƒ์˜ ResultSet ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค . Connection.prepareCall() ๋ฉ”์„œ๋“œ๋Š” CallableStatement ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค .

์—ฌ๊ธฐ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ a, b ๋ฐ c๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ € ADD๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ ˆ์ฐจ๋Š” a์™€ b๋ฅผ ๋”ํ•˜๊ณ  ๋”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜ c์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.

ํ˜ธ์ถœ์„ ์‹œ๋„ํ•  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

// Connect to the server
Connection connection = DriverManager.getConnection("jdbc:as400://mySystem");

// Create a CallableStatement object. It does preprocessing
// calling a stored procedure. Question marks
// indicate where the input parameters should be substituted, and where the output ones
// The first two parameters are input,
// and the third one is a day off.
CallableStatement statement = connection.prepareCall("CALL MYLIBRARY.ADD (?, ?, ?)");

// Setting up input parameters. Passing 123 and 234 to the procedure
statement.setInt (1, 123);
statement.setInt (2, 234);

// Registering the output parameter type
statement.registerOutParameter (3, Types.INTEGER);

// Run stored procedure
statement.execute();

// Get the value of the output parameter
int sum = statement.getInt(3);

// Close CallableStatement and Connection
statement.close();
connection.close();

์ž‘์—…์€ PreparedStatement ์™€ ๊ฑฐ์˜ ๋น„์Šท ํ•˜์ง€๋งŒ ๋‰˜์•™์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ADD ํ•จ์ˆ˜๋Š” ์„ธ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. CallableStatement ๊ฐ์ฒด ๋งŒ์ด ์ด๊ฒƒ์— ๋Œ€ํ•ด ์•„๋ฌด๊ฒƒ๋„ ๋ชจ๋ฆ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ registerOutParameter() ๋ฉ”์„œ๋“œ ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ด๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์•Œ๋ฆฝ๋‹ˆ๋‹ค .

registerOutParameter(parameter number, Parameter type)

๊ทธ๋Ÿฐ ๋‹ค์Œ execute() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•œ ๋‹ค์Œ getInt() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ธ ๋ฒˆ์งธ ๋งค๊ฐœ ๋ณ€์ˆ˜์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

์ผ๊ด„ ์ฒ˜๋ฆฌ ์š”์ฒญ

์‹ค์ œ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๋™์ผํ•œ ์œ ํ˜•์˜ ์ฟผ๋ฆฌ๋ฅผ ๋งŽ์ด ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์ž์ฃผ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค(์ด ๊ฒฝ์šฐ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์ฟผ๋ฆฌ๋Š” PreparedStatement ). ์˜ˆ๋ฅผ ๋“ค์–ด ์ˆ˜์‹ญ ๋˜๋Š” ์ˆ˜๋ฐฑ ๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ์š”์ฒญ์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋ฉด ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆฌ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ผ๊ด„ ์‚ฝ์ž… ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์š”์ฒญ๊ณผ ํ•จ๊ป˜ ์ผ๋ถ€ ๋ฒ„ํผ๋ฅผ ์ถ•์ ํ•œ ๋‹ค์Œ ์ฆ‰์‹œ ์‹คํ–‰ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์— ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

PreparedStatement stmt = con.prepareStatement(
        	"INSERT INTO jc_contact (first_name, last_name, phone, email) VALUES (?, ?, ?, ?)");

for (int i = 0; i < 10; i++) {
	// Fill in the request parameters
	stmt.setString(1, "FirstName_" + i);
    stmt.setString(2, "LastNAme_" + i);
    stmt.setString(3, "phone_" + i);
    stmt.setString(4, "email_" + i);
	// The request is not executed, but fits into the buffer,
	// which is then executed immediately for all commands
	stmt.addBatch();
}
// Execute all requests at once
int[] results = stmt.executeBatch();

execute() ๋ฉ”์„œ๋“œ ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋Œ€์‹  addBatch() ๋ฉ”์„œ๋“œ ๋กœ ์ผ๊ด„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค .

๊ทธ๋Ÿฐ ๋‹ค์Œ ์ˆ˜๋ฐฑ ๊ฐœ์˜ ์š”์ฒญ์ด ์žˆ์„ ๋•Œ executeBatch() ๋ช…๋ น์„ ํ˜ธ์ถœํ•˜์—ฌ ํ•œ ๋ฒˆ์— ์„œ๋ฒ„๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

๊ฑด๊ฐ•ํ•œ. executeBatch() ๋ฉ”์„œ๋“œ๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด์ธ int[]๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฐ์—ด์˜ ๊ฐ ์…€์—๋Š” ํ•ด๋‹น ์ฟผ๋ฆฌ์—์„œ ์ˆ˜์ •ํ•œ ํ–‰ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์น˜์—์„œ ์š”์ฒญ ๋ฒˆํ˜ธ 3์ด 5๊ฐœ โ€‹โ€‹ํ–‰์„ ๋ณ€๊ฒฝํ•œ ๊ฒฝ์šฐ ๋ฐฐ์—ด์˜ ์„ธ ๋ฒˆ์งธ ์…€์—๋Š” ์ˆซ์ž 5๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION