728x90
1. PostgreSQL 설치
2. Java 연동(JDBC를 위한) 커넥터 설치

1. PostgreSQL 설치하기

https://www.postgresql.org/

 

PostgreSQL

The world's most advanced open source database.

www.postgresql.org

Download 클릭
Windows 클릭
Download the installer 클릭
Download 클릭
계속 Next를 누른다.
나는 패스워드 1로 설정했다. 까먹지 않기 위해!!
기본 포트는 5432이다.
한국어 선택
계속 Next해서 오다가, 마지막에 체크 박스 해제하고 Finsh 클릭

 

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

PostgreSQL JDBC 4.2 Driver, 42.2.23 클릭
다운받은 .jar 파일을 C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib 디렉토리에 넣어준다.

소스코드는 블로그에 있는 코드를 그대로 애용했다...

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();
			}
		}

데이터베이스안에 테이블에서 우클릭 후, Query Tool을 클릭한다.

select * from user_info;

코드 적고 실행하면, 출력 결과가 나온다. (이미지는 추후 추가 예정)

 

출처 : https://blog.naver.com/ttkdl12345 "감사합니다:)"

728x90

'웹 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

+ Recent posts