1. PostgreSQL 설치
2. Java 연동(JDBC를 위한) 커넥터 설치
1. PostgreSQL 설치하기
PostgreSQL
The world's most advanced open source database.
www.postgresql.org
2. Java 연동(JDBC를 위한) 커넥터 설치
https://jdbc.postgresql.org/download.html
PostgreSQL JDBC Download
Download About Binary JAR file downloads of the JDBC driver are available here and the current version with Maven Repository. Because Java is platform neutral, it is a simple process of just downloading the appropriate JAR file and dropping it into your cl
jdbc.postgresql.org
소스코드는 블로그에 있는 코드를 그대로 애용했다...
System.out.println(person.getName());
System.out.println(person.getAge());
String podb = "jdbc:postgresql://localhost:5432/test";
String url = podb;
String id = "postgres";
String pw = "a123";
PreparedStatement psmt = null; //쓰기용
Connection con = null; //DB연결용
ResultSet rs = null; //읽기용
//String sql = "INSERT INTO test VALUES(?,?)";
String sql = "INSERT INTO user_info VALUES(?,?)";
try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection(url, id, pw);
psmt = con.prepareStatement(sql);
psmt.setString(1, person.getName());
psmt.setInt(2, person.getAge());
int result = psmt.executeUpdate();
if (result == 1) {
out.println("<p>정상적으로 입력 되었습니다.</p>");
} else {
out.println("<p>정상적으로 입력되지 못했습니다.</p>");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
con.close();
psmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
select * from user_info;
코드 적고 실행하면, 출력 결과가 나온다. (이미지는 추후 추가 예정)
출처 : https://blog.naver.com/ttkdl12345 "감사합니다:)"
'웹 full stack 교육 > 이론' 카테고리의 다른 글
[Spring] 사용 단계 (0) | 2021.09.23 |
---|---|
[Git] Pull & merge (0) | 2021.09.17 |
[Spring] 폴더 복제하여 사용하기 (0) | 2021.09.17 |
[Spring] Controller 사용하기 (0) | 2021.09.16 |
[Spring] 시작하기 (0) | 2021.09.16 |