💻 개인공부 💻/Java

[JAVA] JDBC 연동 - 3. JAVA와 DATABASE 연동 예제 및 소스코드

공대생 배기웅 2020. 3. 11. 14:07
반응형

JAVA와 DATABASE 연동하기 마지막 편 입니다. 첫 번째 글에서는 필요한 프로그램 설치에 대해서, 두 번째 글에서는 데이터를 입력하는 방법에 대하여 다루어 보았습니다. 세 번째 글에서는 입력한 데이터를 JDBC를 이용하여 데이터베이스와 연동시켜 보겠습니다.

 

1. eclipse실행

 

2. 아래의 소스코드를 eclipse에 입력합니다.

Server Host: localhost

Port: 5432

Database: bbs

User name: postgres

password: *****

칼럼: text, content

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
 
public class PostgreSQLRunner{
    public static void main(String [] args){
        try{
            Class.forName("org.postgresql.Driver");
            Connection con=DriverManager.getConnection("jdbc:postgresql://localhost:5432//bss""postgres""*****");
            PreparedStatement stmt=con.prepareStatement("select*from bbs.text");
            ResultSet Rs=stmt.executeQuery();
            while(Rs.next()){
                System.out.println(Rs.getString(1)+Rs.getString(2));
            }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

3. 이렇게 하고 Run을 하면 콘솔창에 데이터베이스가 출력됩니다.

728x90
반응형