일단 jar파일 다운로드
나는 업무상 9.4.1208 버전이 필요해서 아래 링크에서 다운받았다.

Binary file: postgresql-9.4.1208.jre7.jar 다운로드


커넥션 샘플소스.

db명 : postgresDB
uid : postgresU
pwd : postgresP

[postgres@tech9 ~]$ cat a1.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class a1 {

        public static void main(String[] argv) {
                System.out.println("-------- PostgreSQL "
                                + "JDBC Connection Testing ------------");

                try {
                        Class.forName("org.postgresql.Driver");
                } catch (ClassNotFoundException e) {
                        System.out.println("Where is your PostgreSQL JDBC Driver? "
                                        + "Include in your library path!");
                        e.printStackTrace();
                        return;
                }

                System.out.println("PostgreSQL JDBC Driver Registered!");

                Connection connection = null;

                try {
                        connection = DriverManager.getConnection(
                                        "jdbc:postgresql://127.0.0.1:5432/postgresDB", "postgresU",
                                        "postgresP");
                } catch (SQLException e) {
                        System.out.println("Connection Failed! Check output console");
                        e.printStackTrace();
                        return;
                }

                if (connection != null) {
                        System.out.println("You made it, take control your database now!");
                } else {
                        System.out.println("Failed to make connection!");
                }
        }

}



작성한 샘플소스로 커넥션 해보쟈

javac 파일명 => 이렇게해서 컴파일을 먼저 하고 (클래스 파일 생성됨)
java 확장자 제외한 파일명 (.java붙이면 안됨)  해서 돌리면 됨.

이때 클래스명이랑 실제파일명이랑 같아야하고 숫자로 시작되면 잘 안되는 버그?가 종종 벌어지니 안전하게 ㄱㄱ

{postgres@tech9 ~]$ export CLASSPATH=$HOME/postgresql-9.4.1208.jre7.jar:$CLASSPATH

[postgres@tech9 ~]$ javac a1.java
javac a1.java
[postgres@tech9 ~]$ java a1
-------- PostgreSQL JDBC Connection Testing ------------
PostgreSQL JDBC Driver Registered!
You made it, take control your database now!





+ Recent posts