๐ํ๋ก๊ทธ๋๋จธ์ค ๋ฐฑ์๋ ๋ฐ๋ธ์ฝ์ค 4๊ธฐ ๊ต์ก๊ณผ์ ์ ๋ฃ๊ณ ์ ๋ฆฌํ ๊ธ์
๋๋ค.๐
DataSource
- ๋ฐ์ดํฐ ์์ค๋ฅผ ์ด์ฉํด์ ์ปค๋ฅ์
ํ์ ์๋ ์ปค๋ฅ์
์ ๊ฐ์ ธ์ ์ฌ์ฉํ๊ณ , ๋ฐ๋ฉํจ
- ๋ฐ์ดํฐ ์์ค๊ฐ ์ปค๋ฅ์
ํ์ ๊ด๋ฆฌ
DataBase Connection Pool(DBCP)
๊ทธ๋ฆผ ์ถ์ฒ
- ์ปค๋ฅ์
ํ์ ์ฌ๋ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ(์ปค๋ฅ์
)์ ๋ฏธ๋ฆฌ ์ค์ ํ๊ณ ์ฌ์ฌ์ฉํ์ฌ ์ค๋ฒํค๋๋ฅผ ์ค์
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์
์ ์ฒ๋ฆฌํ ํ์๋ ์ฐ๊ฒฐ์ ๋ซ์ง ์๊ณ ํ์ ๋ฐํํ์ฌ ์ด๋ฅผ ์ฌ์ฌ์ฉ
HikariCP
- Java ์ธ์ด๋ฅผ ์ํ ์ปค๋ฅ์
ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ๋ณ๋ ์ค์ ์์ด ์คํ๋ง ๋ถํธ JDBC API์ ํฌํจ
JdbcTemplate
- ๊ธฐ์กด JDBC API๋ฅผ ๋จ์ํํ๊ณ ๋ณต์กํ ๋ฐ๋ณต ์ฝ๋๋ฅผ ์ ๊ฑฐ
- ์์ธ ์ฒ๋ฆฌ์ ์ปค๋ฅ์
๋ฆฌ์์ค ๊ด๋ฆฌ๋ฅผ ์๋์ผ๋ก ์ฒ๋ฆฌ
- DataSource๋ฅผ ์ด์ฉํด์ ์ปค๋ฅ์
์ ์ฌ์ฉ
์์ ์ฝ๋
public class JDBCTemplateCrudExample {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/sampledb");
dataSource.setUsername("username");
dataSource.setPassword("password");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
UUID id = UUID.randomUUID();
String name = "์ ๋ช
ํ";
int age = 25;
insertData(jdbcTemplate, id, name, age);
UUID searchId = UUID.fromString("your-id");
UserData userData = retrieveDataById(jdbcTemplate, searchId);
if (userData != null) {
System.out.println("Data Found:");
System.out.println("ID: " + userData.getId() + ", Name: " + userData.getName() + ", Age: " + userData.getAge());
} else {
System.out.println("Data Not Found");
}
System.out.println("All Data:");
retrieveData(jdbcTemplate);
String newName = "๊น๋ช
ํ";
int newAge = 30;
updateData(jdbcTemplate, id, newName, newAge);
deleteData(jdbcTemplate, id);
System.out.println("Updated Data:");
retrieveData(jdbcTemplate);
}
private static void insertData(JdbcTemplate jdbcTemplate, UUID id, String name, int age) {
String sql = "INSERT INTO users (id, name, age) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, toBytes(id), name, age);
System.out.println("Data inserted successfully.");
}
private static UserData retrieveDataById(JdbcTemplate jdbcTemplate, UUID id) {
String sql = "SELECT * FROM users WHERE id = ?";
Object[] params = {toBytes(id)};
UserData userData = jdbcTemplate.queryForObject(sql, params, (ResultSet rs, int rowNum) -> {
UUID userId = toUUID(rs.getBytes("id"));
String name = rs.getString("name");
int age = rs.getInt("age");
return new UserData(userId, name, age);
});
return userData;
}
private static void retrieveData(JdbcTemplate jdbcTemplate) {
String sql = "SELECT * FROM users";
jdbcTemplate.query(sql, (ResultSet rs) -> {
while (rs.next()) {
UUID id = toUUID(rs.getBytes("id"));
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
});
}
private static void updateData(JdbcTemplate jdbcTemplate, UUID id, String name, int age) {
String sql = "UPDATE users SET name=?, age=? WHERE id=?";
jdbcTemplate.update(sql, name, age, toBytes(id));
System.out.println("Data updated successfully.");
}
private static void deleteData(JdbcTemplate jdbcTemplate, UUID id) {
String sql = "DELETE FROM users WHERE id=?";
jdbcTemplate.update(sql, toBytes(id));
System.out.println("Data deleted successfully.");
}
private static UUID toUUID(byte[] bytes) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
return new UUID(byteBuffer.getLong(), byteBuffer.getLong());
}
private static byte[] toBytes(UUID uuid) {
ByteBuffer byteBuffer = ByteBuffer.allocate(16);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
return byteBuffer.array();
}
}