[JPA] 관계형 데이터베이스와 자바
관계형 데이터베이스와 자바
(관계형) 데이터베이스와 자바의 연결 고리
- 크지 않다. 굳이 자바가 아니라 다른 언어로도 접근 가능
- JDBC드라이버를 통해 접근
무엇이 문제?
- SQL을 실행하는 비용이 비쌈
- SQL이 데이터베이스마다 다르다.
- 스키마를 바꿨더니 코드가 너무 많이 바뀜
- 반복적인 코드가 많음
- 지금 당장 필요없더라도 다 읽어와야 하나?
JDBC예제
의존성추가
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
public class Application {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/springdata";
String user = "juho";
String pass = "pass";
try(Connection connection = DriverManager.getConnection(url, user, pass)) {
System.out.println("Connection created : " + connection);
// String sql = "CREATE TABLE ACCOUNT (id int, username varchar(255), password varchar(255));";
String sql = "INSERT INTO ACCOUNT VALUES(1, 'juho', 'pass');";
try(PreparedStatement ps = connection.prepareStatement(sql)) {
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}