목차
1. Insert
public class InsertExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb2?serverTimezone=Asia/Seoul";
String user = "root";
String password = "asd123";
// Connection 객체를 얻어서 insert 구문을 직접 만들어 보세요.
// mydb2 사용, employee 테이블에 값을 넣는 코드를 작성하세요
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String query = "INSERT INTO employee VALUES (?, ?, ?, ?, now())";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, 8);
preparedStatement.setString(2, "강감찬");
preparedStatement.setString(3, "마케팅부");
preparedStatement.setString(4, "3400000.00");
int rowCount = preparedStatement.executeUpdate();
System.out.println("rowCount : " + rowCount);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2. Select
public class SelectExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb2?serverTimezone=Asia/Seoul";
String user = "root";
String password = "asd123";
// Connection 객체를 얻어서 select 구문을 직접 만들어 보세요.
// mydb2 사용, employee 테이블의 값을 조회하는 코드를 작성하세요
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String query = "SELECT * FROM employee";
preparedStatement = connection.prepareStatement(query);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Employee employee = new Employee(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getString(5));
System.out.println(employee);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3. Update
public class UpdateExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb2?serverTimezone=Asia/Seoul";
String user = "root";
String password = "asd123";
// Connection 객체를 얻어서 update 구문을 직접 만들어 보세요.
// mydb2 사용, employee 테이블에 값을 변경하는 코드를 작성하세요
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String query = "UPDATE employee SET department = ? WHERE name = ?";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, "인사부");
preparedStatement.setString(2, "강감찬");
int rowCount = preparedStatement.executeUpdate();
System.out.println("rowCount : " + rowCount);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4. Delete
public class DeleteExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb2?serverTimezone=Asia/Seoul";
String user = "root";
String password = "asd123";
// Connection 객체를 얻어서 Delete 구문을 직접 만들어 보세요.
// mydb2 사용, employee 테이블에 값을 제거하는 코드를 작성하세요
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String query = "DELETE FROM employee WHERE name = ?";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, "강감찬");
int rowCount = preparedStatement.executeUpdate();
System.out.println("rowCount : " + rowCount);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
'Java > 자료구조' 카테고리의 다른 글
JDBC를 활용한 CRUD 와 SOLID 원칙 - 7 (1) | 2024.06.14 |
---|---|
JDBC 트랜잭션 관리와 배치 처리 - 6 (0) | 2024.06.12 |
JDBC 기본 사용법 - 4 (0) | 2024.06.11 |
JDBC 설치 및 설정 - 3 (0) | 2024.06.11 |
JDBC 구성 요소(아키텍처) - 2 (0) | 2024.06.10 |