
์๋ฐ JDBC๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๊ฒฐํ ๋ ์ฌ์ฉํ ์์์ `connection.close()` ์ฒ๋ผ `close()` ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๋ซ์ ์ฃผ์ด์ผ ํ๋ค. ํ์ง๋ง try with resources ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๋ฉด ๋ณ๋๋ก ์์ํด์ ๋ฅผ ์ ํด ์ค๋ ๋๋ค. try ๊ตฌ๋ฌธ์์ ์์์ ์ ์ธํ๋ฉด ์๋์ผ๋ก ํด์ ๋๊ธฐ ๋๋ฌธ์ด๋ค.
์ด์ฒ๋ผ try-with-resources๋ ๋ฆฌ์์ค ๊ด๋ฆฌ๊ฐ ์ค์ํ ํ๋ก๊ทธ๋จ์์ ์ค๋ฅ๋ฅผ ๋ฐฉ์งํ๊ณ ์ฝ๋ ํ์ง์ ๋์ด๋ ๋งค์ฐ ์ ์ฉํ ๊ธฐ๋ฅ์ด๋ค. ํ์ผ ์
์ถ๋ ฅ, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ, ๋คํธ์ํฌ ์์ผ ๋ฑ ๋ค์ํ ์ํฉ์์ ํ์ฉ ๊ฐ๋ฅํ๋ค.
๊ธฐ๋ณธ์ฌ์ฉ๋ฒ
์ฝ๋์์ ๋ณผ ์ ์๋ฏ์ด try-with-resources์ ๊ธฐ๋ณธ ํ์์ try๋ฌธ ์์์ ์์์ ์ ์ธํ๋ ๊ฒ์ด๋ค.
try (๋ฆฌ์์ค_ํ์
๋ฆฌ์์ค_์ด๋ฆ = new ๋ฆฌ์์ค_ํ์
()) {
// ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋
} catch (์์ธ_ํ์
e) {
// ์์ธ ์ฒ๋ฆฌ ์ฝ๋
}
try (ResourceType resource = new ResourceType()) {
// ์์์ ์ฌ์ฉํ ์์
์ํ
} catch (Exception e) {
e.printStackTrace();
} // try ๋ธ๋ก์ด ๋๋๋ฉด resource.close()๊ฐ ์๋์ผ๋ก ํธ์ถ๋จ
์ฌ๋ฌ ๋ฆฌ์์ค๋ฅผ ํ ๋ฒ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
try ๋ฌธ ์์์ ์ฌ๋ฌ ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ ๋๋ ์ธ๋ฏธ์ฝ๋ก (;)์ผ๋ก ๊ตฌ๋ถํ์ฌ ์ ์ธํ๋ค.
try (
BufferedReader br1 = new BufferedReader(new FileReader("file1.txt"));
BufferedReader br2 = new BufferedReader(new FileReader("file2.txt"))
) {
// ๋ ํ์ผ์ ์ฒ๋ฆฌํ๋ ์ฝ๋
} catch (IOException e) {
e.printStackTrace();
}
try-with-resources ์์ด ์์ฑํ ์ฝ๋
์์์ ์๋์ผ๋ก ๋ซ์์ผ ํ๋ฉฐ, ์์ธ๊ฐ ๋ฐ์ํด๋ finally ๋ธ๋ก์์ ์์์ ๋ซ๋ ์ฒ๋ฆฌ๋ฅผ ํด์ผ ํ๋ค.
์ด๋ ๊ฒ ํ๋์ฉ ๋ซ์์ฃผ๋ ๊ฑธ ์๋ ์์ ๊ด๋ฆฌ (try-catch-finally)๋ผ๊ณ ํ๋ค. ์ด๋ฐ์์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ์ฝ๋๊ฐ ๋ ๊ธธ์ด์ง๊ณ ๋ณต์กํด์ง๋ค. ๋ ์ฌ์ฉํ ์์์ ํ๋์ฉ ์๋์ผ๋ก ๋ซ์์ผ ํ๊ธฐ ๋๋ฌธ์ ์ค์๋ก ์ธํด ์์ ๋์๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ์๋ค.
๊ทธ๋์ ์ด๋ฐ ๋ฐฉ์์ try-with-resources๋ฅผ ์ฌ์ฉํ ์ ์๋ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class ManualResourceManagementExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/exampledb";
String user = "username";
String password = "password";
String query = "SELECT * FROM students";
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection(url, user, password);
statement = connection.prepareStatement(query);
resultSet = statement.executeQuery();
while (resultSet.next()) {
System.out.println("Student ID: " + resultSet.getInt("id"));
System.out.println("Name: " + resultSet.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) resultSet.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
try-with-resources ์ฌ์ฉ ์ฝ๋
try-with-resources๋ try๋ฌธ ์์์ ์์์ ์ ์ธํ๊ณ ์ด๊ธฐํํ๋ฉด, try๋ธ๋ก์ด ๋๋ ํ ์์์ ์๋์ผ๋ก ๋ซ์์ฃผ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ์ด๊ฑธ๋ก ์์ ๋์๋ฅผ ๋ฐฉ์งํ๊ณ , ์ฝ๋ ๊ฐ๋
์ฑ์ ๋์ผ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ ์์ธ ์ฒ๋ฆฌํ ๋ ์ฝ๋ ๋ธ๋ญ์ด ๋ณต์กํด์ง์ง ์์์ ์ฝ๋๊ฐ ๋จ์ํด์ง๋ค.
- ์๋์ผ๋ก ๋ซ์ ์ ์๋ ์์ (AutoCloseable ์ธํฐํ์ด์ค ๊ตฌํ์ฒด)๋ง try-with-resources ์์ ์์ฑํ ์ ์๋ค.
์: Connection, PreparedStatement, ResultSet ๊ฐ์ JDBC ํด๋์ค๋ค.
try ๋ธ๋ก์์ ์ฌ์ฉํ ์์์ ๋ธ๋ก์ด ์ข
๋ฃ๋๋ฉด ์๋์ผ๋ก close() ๋ฉ์๋๊ฐ ํธ์ถ๋์ด ์ข
๋ฃ๋๋ ๋ฐฉ์์ด๋ค.
์ฌ๋ฌ ์์์ ๋์์ ์ ์ธํ ์๋ ์๊ณ , ์์์ ์ ์ธ๋ ์์๋๋ก ๋ซํ๋ค.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class TryWithResourcesExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/exampledb";
String user = "username";
String password = "password";
String query = "SELECT * FROM students";
try (
Connection connection = DriverManager.getConnection(url, user, password);
PreparedStatement statement = connection.prepareStatement(query);
ResultSet resultSet = statement.executeQuery()
) {
while (resultSet.next()) {
System.out.println("Student ID: " + resultSet.getInt("id"));
System.out.println("Name: " + resultSet.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
์ฅ์
- ์ฝ๋ ๊ฐ๊ฒฐ์ฑ: try-catch-finally๋ก ์๋์ผ๋ก ๋ซ๋ ์ฝ๋๋ฅผ ์์ฑํ ํ์ ์์.
- ์์ ๋์ ๋ฐฉ์ง: ์์ธ๊ฐ ๋ฐ์ํ๋๋ผ๋ ์์์ด ๋ฐ๋์ ๋ซํ.
- ๋ค์ค ์์ ์ฒ๋ฆฌ ๊ฐ๋ฅ: ์ฌ๋ฌ ์์์ ํ ๋ฒ์ ์ ์ธ ๊ฐ๋ฅํ๋ฉฐ, ์์๋๋ก ๋ซํ.
์ฃผ์์ฌํญ
- `Java 7` ์ด์์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ.
- `AutoCloseable` ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ง ์ฌ์ฉ ๊ฐ๋ฅ.
(๋ชจ๋ ๋ฆฌ์์ค๊ฐ ๋ฐ๋์ `AutoCloseable` ๋๋ `Closeable` ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํด์ผ ํ๋ค.)
- ๋ธ๋ก ๋ด๋ถ์์ ์ ์ธ๋์ง ์์ ์์์ ์๋์ผ๋ก ๋ซํ์ง ์์.
- ๋ํ ๋ฆฌ์์ค๋ฅผ ๋ซ๋ ์ค์ ๋ฐ์ํ๋ ์์ธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก "์ต์ ๋ ์์ธ(suppressed exception)"๋ก ์ฒ๋ฆฌ๋๋ค.
์ด๊ฑด `getSuppressed()` ๋ฉ์๋๋ก ํ์ธํ ์ ์๋ค.
์์
์์ 1. ํ์ผ์ฝ๊ธฐ
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TryWithResourcesExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line); // ํ์ผ ๋ด์ฉ ์ถ๋ ฅ
}
} catch (IOException e) {
e.printStackTrace(); // ์์ธ ์ฒ๋ฆฌ
}
}
}
์ ์ฝ๋๋ ํ์ผ์ ์ฝ๊ณ ๋์ BufferedReader๊ฐ ์๋์ผ๋ก ๋ซํ.
์์ 2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DatabaseExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb";
String user = "username";
String password = "password";
try (
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM my_table");
) {
while (rs.next()) {
System.out.println(rs.getString("column_name")); // ๊ฒฐ๊ณผ ์ถ๋ ฅ
}
} catch (Exception e) {
e.printStackTrace(); // ์์ธ ์ฒ๋ฆฌ
}
}
}
์ค์ํ ๋ถ๋ถ๋ง ๋ผ์ด์ ๋ณด๋ฉด
try (
Connection connection = DriverManager.getConnection(url, user, password);
PreparedStatement statement = connection.prepareStatement(query);
ResultSet resultSet = statement.executeQuery()
) {
while (resultSet.next()) {
System.out.println("ID: " + resultSet.getInt("id"));
}
} catch (SQLException e) {
e.printStackTrace();
} // try ๋ธ๋ก ์ข
๋ฃ ์ connection, statement, resultSet์ด ์๋์ผ๋ก ๋ซํ
Connection, Statement, ResultSet ๊ฐ์ฒด๋ค์ด ์๋์ผ๋ก ๋ซํ.
์์ 3: ์ฌ์ฉ์ ์ ์ ๋ฆฌ์์ค
AutoCloseable์ ๊ตฌํํ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ try-with-resources์ ํจ๊ป ์ฌ์ฉํ ์ ์๋ค.
class MyResource implements AutoCloseable {
@Override
public void close() {
System.out.println("๋ฆฌ์์ค๊ฐ ๋ซํ์ต๋๋ค!");
}
}
public class Main {
public static void main(String[] args) {
try (MyResource resource = new MyResource()) {
System.out.println("๋ฆฌ์์ค๋ฅผ ์ฌ์ฉ ์ค์
๋๋ค.");
}
}
}
์ด๋ ๊ฒ ์ฐ๋ฉด close() ๋ฉ์๋๊ฐ ์๋์ผ๋ก ํธ์ถ๋์ด "๋ฆฌ์์ค๊ฐ ๋ซํ์ต๋๋ค!"๊ฐ ์ถ๋ ฅ๋๋ค.
'ํ๋ก๊ทธ๋๋ฐ์ธ์ด > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ

์๋ฐ JDBC๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๊ฒฐํ ๋ ์ฌ์ฉํ ์์์ connection.close()
์ฒ๋ผ close()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๋ซ์ ์ฃผ์ด์ผ ํ๋ค. ํ์ง๋ง try with resources ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๋ฉด ๋ณ๋๋ก ์์ํด์ ๋ฅผ ์ ํด ์ค๋ ๋๋ค. try ๊ตฌ๋ฌธ์์ ์์์ ์ ์ธํ๋ฉด ์๋์ผ๋ก ํด์ ๋๊ธฐ ๋๋ฌธ์ด๋ค.
์ด์ฒ๋ผ try-with-resources๋ ๋ฆฌ์์ค ๊ด๋ฆฌ๊ฐ ์ค์ํ ํ๋ก๊ทธ๋จ์์ ์ค๋ฅ๋ฅผ ๋ฐฉ์งํ๊ณ ์ฝ๋ ํ์ง์ ๋์ด๋ ๋งค์ฐ ์ ์ฉํ ๊ธฐ๋ฅ์ด๋ค. ํ์ผ ์
์ถ๋ ฅ, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ, ๋คํธ์ํฌ ์์ผ ๋ฑ ๋ค์ํ ์ํฉ์์ ํ์ฉ ๊ฐ๋ฅํ๋ค.
๊ธฐ๋ณธ์ฌ์ฉ๋ฒ
์ฝ๋์์ ๋ณผ ์ ์๋ฏ์ด try-with-resources์ ๊ธฐ๋ณธ ํ์์ try๋ฌธ ์์์ ์์์ ์ ์ธํ๋ ๊ฒ์ด๋ค.
try (๋ฆฌ์์ค_ํ์
๋ฆฌ์์ค_์ด๋ฆ = new ๋ฆฌ์์ค_ํ์
()) { // ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋ } catch (์์ธ_ํ์
e) { // ์์ธ ์ฒ๋ฆฌ ์ฝ๋ }
try (ResourceType resource = new ResourceType()) { // ์์์ ์ฌ์ฉํ ์์
์ํ } catch (Exception e) { e.printStackTrace(); } // try ๋ธ๋ก์ด ๋๋๋ฉด resource.close()๊ฐ ์๋์ผ๋ก ํธ์ถ๋จ
์ฌ๋ฌ ๋ฆฌ์์ค๋ฅผ ํ ๋ฒ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
try ๋ฌธ ์์์ ์ฌ๋ฌ ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ ๋๋ ์ธ๋ฏธ์ฝ๋ก (;)์ผ๋ก ๊ตฌ๋ถํ์ฌ ์ ์ธํ๋ค.
try ( BufferedReader br1 = new BufferedReader(new FileReader("file1.txt")); BufferedReader br2 = new BufferedReader(new FileReader("file2.txt")) ) { // ๋ ํ์ผ์ ์ฒ๋ฆฌํ๋ ์ฝ๋ } catch (IOException e) { e.printStackTrace(); }
try-with-resources ์์ด ์์ฑํ ์ฝ๋
์์์ ์๋์ผ๋ก ๋ซ์์ผ ํ๋ฉฐ, ์์ธ๊ฐ ๋ฐ์ํด๋ finally ๋ธ๋ก์์ ์์์ ๋ซ๋ ์ฒ๋ฆฌ๋ฅผ ํด์ผ ํ๋ค.
์ด๋ ๊ฒ ํ๋์ฉ ๋ซ์์ฃผ๋ ๊ฑธ ์๋ ์์ ๊ด๋ฆฌ (try-catch-finally)๋ผ๊ณ ํ๋ค. ์ด๋ฐ์์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ์ฝ๋๊ฐ ๋ ๊ธธ์ด์ง๊ณ ๋ณต์กํด์ง๋ค. ๋ ์ฌ์ฉํ ์์์ ํ๋์ฉ ์๋์ผ๋ก ๋ซ์์ผ ํ๊ธฐ ๋๋ฌธ์ ์ค์๋ก ์ธํด ์์ ๋์๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ์๋ค.
๊ทธ๋์ ์ด๋ฐ ๋ฐฉ์์ try-with-resources๋ฅผ ์ฌ์ฉํ ์ ์๋ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class ManualResourceManagementExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/exampledb"; String user = "username"; String password = "password"; String query = "SELECT * FROM students"; Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; try { connection = DriverManager.getConnection(url, user, password); statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); while (resultSet.next()) { System.out.println("Student ID: " + resultSet.getInt("id")); System.out.println("Name: " + resultSet.getString("name")); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (resultSet != null) resultSet.close(); if (statement != null) statement.close(); if (connection != null) connection.close(); } catch (Exception e) { e.printStackTrace(); } } } }
try-with-resources ์ฌ์ฉ ์ฝ๋
try-with-resources๋ try๋ฌธ ์์์ ์์์ ์ ์ธํ๊ณ ์ด๊ธฐํํ๋ฉด, try๋ธ๋ก์ด ๋๋ ํ ์์์ ์๋์ผ๋ก ๋ซ์์ฃผ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ์ด๊ฑธ๋ก ์์ ๋์๋ฅผ ๋ฐฉ์งํ๊ณ , ์ฝ๋ ๊ฐ๋
์ฑ์ ๋์ผ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ ์์ธ ์ฒ๋ฆฌํ ๋ ์ฝ๋ ๋ธ๋ญ์ด ๋ณต์กํด์ง์ง ์์์ ์ฝ๋๊ฐ ๋จ์ํด์ง๋ค.
- ์๋์ผ๋ก ๋ซ์ ์ ์๋ ์์ (AutoCloseable ์ธํฐํ์ด์ค ๊ตฌํ์ฒด)๋ง try-with-resources ์์ ์์ฑํ ์ ์๋ค.
์: Connection, PreparedStatement, ResultSet ๊ฐ์ JDBC ํด๋์ค๋ค.
try ๋ธ๋ก์์ ์ฌ์ฉํ ์์์ ๋ธ๋ก์ด ์ข
๋ฃ๋๋ฉด ์๋์ผ๋ก close() ๋ฉ์๋๊ฐ ํธ์ถ๋์ด ์ข
๋ฃ๋๋ ๋ฐฉ์์ด๋ค.
์ฌ๋ฌ ์์์ ๋์์ ์ ์ธํ ์๋ ์๊ณ , ์์์ ์ ์ธ๋ ์์๋๋ก ๋ซํ๋ค.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class TryWithResourcesExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/exampledb"; String user = "username"; String password = "password"; String query = "SELECT * FROM students"; try ( Connection connection = DriverManager.getConnection(url, user, password); PreparedStatement statement = connection.prepareStatement(query); ResultSet resultSet = statement.executeQuery() ) { while (resultSet.next()) { System.out.println("Student ID: " + resultSet.getInt("id")); System.out.println("Name: " + resultSet.getString("name")); } } catch (Exception e) { e.printStackTrace(); } } }
์ฅ์
- ์ฝ๋ ๊ฐ๊ฒฐ์ฑ: try-catch-finally๋ก ์๋์ผ๋ก ๋ซ๋ ์ฝ๋๋ฅผ ์์ฑํ ํ์ ์์.
- ์์ ๋์ ๋ฐฉ์ง: ์์ธ๊ฐ ๋ฐ์ํ๋๋ผ๋ ์์์ด ๋ฐ๋์ ๋ซํ.
- ๋ค์ค ์์ ์ฒ๋ฆฌ ๊ฐ๋ฅ: ์ฌ๋ฌ ์์์ ํ ๋ฒ์ ์ ์ธ ๊ฐ๋ฅํ๋ฉฐ, ์์๋๋ก ๋ซํ.
์ฃผ์์ฌํญ
- Java 7
์ด์์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ.
- AutoCloseable
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ง ์ฌ์ฉ ๊ฐ๋ฅ.
(๋ชจ๋ ๋ฆฌ์์ค๊ฐ ๋ฐ๋์ AutoCloseable
๋๋ Closeable
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํด์ผ ํ๋ค.)
- ๋ธ๋ก ๋ด๋ถ์์ ์ ์ธ๋์ง ์์ ์์์ ์๋์ผ๋ก ๋ซํ์ง ์์.
- ๋ํ ๋ฆฌ์์ค๋ฅผ ๋ซ๋ ์ค์ ๋ฐ์ํ๋ ์์ธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก "์ต์ ๋ ์์ธ(suppressed exception)"๋ก ์ฒ๋ฆฌ๋๋ค.
์ด๊ฑด getSuppressed()
๋ฉ์๋๋ก ํ์ธํ ์ ์๋ค.
์์
์์ 1. ํ์ผ์ฝ๊ธฐ
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class TryWithResourcesExample { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); // ํ์ผ ๋ด์ฉ ์ถ๋ ฅ } } catch (IOException e) { e.printStackTrace(); // ์์ธ ์ฒ๋ฆฌ } } }
์ ์ฝ๋๋ ํ์ผ์ ์ฝ๊ณ ๋์ BufferedReader๊ฐ ์๋์ผ๋ก ๋ซํ.
์์ 2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DatabaseExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mydb"; String user = "username"; String password = "password"; try ( Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM my_table"); ) { while (rs.next()) { System.out.println(rs.getString("column_name")); // ๊ฒฐ๊ณผ ์ถ๋ ฅ } } catch (Exception e) { e.printStackTrace(); // ์์ธ ์ฒ๋ฆฌ } } }
์ค์ํ ๋ถ๋ถ๋ง ๋ผ์ด์ ๋ณด๋ฉด
try ( Connection connection = DriverManager.getConnection(url, user, password); PreparedStatement statement = connection.prepareStatement(query); ResultSet resultSet = statement.executeQuery() ) { while (resultSet.next()) { System.out.println("ID: " + resultSet.getInt("id")); } } catch (SQLException e) { e.printStackTrace(); } // try ๋ธ๋ก ์ข
๋ฃ ์ connection, statement, resultSet์ด ์๋์ผ๋ก ๋ซํ
Connection, Statement, ResultSet ๊ฐ์ฒด๋ค์ด ์๋์ผ๋ก ๋ซํ.
์์ 3: ์ฌ์ฉ์ ์ ์ ๋ฆฌ์์ค
AutoCloseable์ ๊ตฌํํ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ try-with-resources์ ํจ๊ป ์ฌ์ฉํ ์ ์๋ค.
class MyResource implements AutoCloseable { @Override public void close() { System.out.println("๋ฆฌ์์ค๊ฐ ๋ซํ์ต๋๋ค!"); } } public class Main { public static void main(String[] args) { try (MyResource resource = new MyResource()) { System.out.println("๋ฆฌ์์ค๋ฅผ ์ฌ์ฉ ์ค์
๋๋ค."); } } }
์ด๋ ๊ฒ ์ฐ๋ฉด close() ๋ฉ์๋๊ฐ ์๋์ผ๋ก ํธ์ถ๋์ด "๋ฆฌ์์ค๊ฐ ๋ซํ์ต๋๋ค!"๊ฐ ์ถ๋ ฅ๋๋ค.