Create
package ch03;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 구문을 직접 만들어 보세요.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String query = "insert into employee values( ?, ?, ?, ?, now())";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(0, 0);
preparedStatement.setString(0, query);
preparedStatement.setString(0, query);
preparedStatement.setString(0, query);
int rowCount = preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// mydb2 사용, employee 테이블에 값을 넣는 코드를 작성하세요.
}
}
Read
package ch03;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
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 connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Scanner sc = new Scanner(System.in);
connection = DriverManager.getConnection(url, user, password);
String query = "Select * from employee where name = ?";
PreparedStatement preparedStatement = connection.prepareStatement(query);
// preparedStatement.setString(1, input);
//
// resultSet = preparedStatement.executeQuery();
String input = sc.next();
preparedStatement.setString(1, input);
resultSet = preparedStatement.executeQuery();
while (true) {
while (resultSet.next()) {
if (input.equals(resultSet.getString(input))) {
System.out.println("생존번호 : " + resultSet.getInt("id") + " || " + "이름 : "
+ resultSet.getString("name") + " || " + "부서 : " + resultSet.getString("department")
+ " || " + "급여 : " + resultSet.getInt("salary"));
}
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Update
package ch03;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String query = "update employee set name = ? where name = ?";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, "오효정");
preparedStatement.setString(2, "천상천하");
int upCount = preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Connection 객체를 얻어서 수정 구문을 직접 만들어 보세요.
// mydb2 사용, employee 테이블에 값을 넣는 코드를 작성하세요.
}
}
Delete
package ch03;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String query = "delete from employee where department = ?";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, "노래");
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Connection 객체를 얻어서 삭제 구문을 직접 만들어 보세요.
// mydb2 사용, employee 테이블에 값을 넣는 코드를 작성하세요.
}
}
'자료구조(Data Structure) > JDBC' 카테고리의 다른 글
JDBC를 활용한 CRUD 와 SOLID 원칙 - 7 (0) | 2024.06.14 |
---|---|
JDBC 트랜잭션 관리와 배치 처리 - 6 (0) | 2024.06.13 |
JDBC 기본 사용법 - 4 (0) | 2024.06.12 |
JDBC 설치 및 설정 - 3 (0) | 2024.06.12 |
JDBC 구성 요소(아키텍처) - 2 (0) | 2024.06.10 |