๊ทธ๋ฆผ ์ถ์ฒ
1. DriverManager๋ฅผ ํตํด์ ์ปจ๋ฅ์
๊ฐ์ฒด๋ฅผ ๋ฐ์์ด
2. Connection์ ํตํด์ Statement๋ฅผ ๊ฐ์ ธ์ด
3. Statement๋ฅผ ํตํด์ ์ฟผ๋ฆฌ๋ฅผ ์คํํด์ ResultSet์ ๊ฐ์ ธ์ค๊ฑฐ๋ update๋ฅผ ์คํ
4. ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฐ๋์ ์ปจ๋ฅ์
์ ์ข
๋ฃ
public class JDBCCrudExample {
// JDBC ์ฐ๊ฒฐ ์ ๋ณด
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/sampledb";
private static final String JDBC_USERNAME = "username";
private static final String JDBC_PASSWORD = "password";
public static void main(String[] args) {
// JDBC ๋๋ผ์ด๋ฒ ๋ก๋
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
// ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ
try (Connection connection = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD)) {
// ๋ฐ์ดํฐ ์ฝ์
UUID id = UUID.randomUUID();
String name = "์ ๋ช
ํ";
int age = 25;
insertData(connection, id, name, age);
// ๋ฐ์ดํฐ ๊ฒ์
System.out.println("All Data:");
retrieveData(connection);
// ๋จ์ผ ํ ๊ฒ์
System.out.println("Single Row Data:");
UUID searchId = UUID.fromString("your-id");
UserData userData = retrieveDataById(connection, 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");
}
// ๋ฐ์ดํฐ ๊ฐฑ์
String newName = "๊น๋ช
ํ";
int newAge = 30;
updateData(connection, id, newName, newAge);
// ๋ฐ์ดํฐ ์ญ์
deleteData(connection, id);
// ์ต์ข
๋ฐ์ดํฐ ๊ฒ์
System.out.println("Updated Data:");
retrieveData(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
// ๋ฐ์ดํฐ ์ฝ์
private static void insertData(Connection connection, UUID id, String name, int age) throws SQLException {
String sql = "INSERT INTO users (id, name, age) VALUES (?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setBytes(1, toBytes(id));
statement.setString(2, name);
statement.setInt(3, age);
statement.executeUpdate();
System.out.println("Data inserted successfully.");
}
}
// ๋ฐ์ดํฐ ๊ฒ์
private static void retrieveData(Connection connection) throws SQLException {
String sql = "SELECT * FROM users";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
UUID id = toUUID(resultSet.getBytes("id"));
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
}
}
// ๋จ์ผ ํ ๊ฒ์
private static UserData retrieveDataById(Connection connection, UUID id) throws SQLException {
String sql = "SELECT * FROM users WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setBytes(1, toBytes(id));
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
UUID userId = toUUID(resultSet.getBytes("id"));
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
return new UserData(userId, name, age);
}
}
}
return null;
}
// ๋ฐ์ดํฐ ๊ฐฑ์
private static void updateData(Connection connection, UUID id, String name, int age) throws SQLException {
String sql = "UPDATE users SET name=?, age=? WHERE id=?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, name);
statement.setInt(2, age);
statement.setBytes(3, toBytes(id));
statement.executeUpdate();
System.out.println("Data updated successfully.");
}
}
// ๋ฐ์ดํฐ ์ญ์
private static void deleteData(Connection connection, UUID id) throws SQLException {
String sql = "DELETE FROM users WHERE id=?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setBytes(1, toBytes(id));
statement.executeUpdate();
System.out.println("Data deleted successfully.");
}
}
// byte[]์ UUID๋ก ๋ณํ
private static UUID toUUID(byte[] bytes) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
return new UUID(byteBuffer.getLong(), byteBuffer.getLong());
}
// UUID๋ฅผ byte[]๋ก ๋ณํ
private static byte[] toBytes(UUID uuid) {
ByteBuffer byteBuffer = ByteBuffer.allocate(16);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
return byteBuffer.array();
}
}