2012-10-26 9 views
0

PostgreSQL 서버가 Java 코드를 통해 실행 중인지 여부를 확인할 수 있습니까? 네트워크에서 관련 솔루션을 찾지 못했습니다. 모든 제안은 정말 감사드립니다. 미리 감사드립니다.java PostgreSQL 서버가 실행 중인지 확인

+0

** 실행 **을 정의하십시오. PostgreSQL 포트가 특정 호스트에서 사용 가능한지 테스트하는 것으로 충분합니까? 로컬 컴퓨터에서'postmaster' 존재를 확인하는 것으로 충분합니까? –

+1

네, 괜찮습니다. 특정 호스트에서 게시물을 사용할 수 있는지 확인. – Mojoy

답변

3

조금 재미 있지만, 여기에, 사용자 이름, 암호를 작동하며 서버가 충분 경우 연결 확인 출력 콘솔을

try { 
     Class.forName("org.postgresql.Driver"); 

     Connection connection = DriverManager.getConnection(
        "jdbc:postgresql://127.0.0.1:5432/testdb", "username", 
        "password"); 

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

     } 


    } catch (Exception e) { 

     System.out.println("Connection Failed! Check output console"); 
     e.printStackTrace(); 
     return; 

    } 
0

을하지 못했습니다 얻을 것이다 실행되고 있지 않은 경우 URL이 정확해야합니다 당신이 HOST + 당신의 PostgreSQL 서버의 포트 가용성을 확인하기 위해, 당신은 할 수 있습니다 :

public static boolean psqlAvailable(String host, int port) { 
    boolean ret = false; 
    try { 
     Socket s = new Socket(host, port); 
     ret = true; 
     s.close(); 
    } catch (Exception e) { 
     // 
    } 
    return ret; 
} 

그것은하지만, 속도가 느릴 수 있습니다. 사용 방법 :

// assuming these are IP+PORT configuration on your PostgreSQL server. 
boolean psql = psqlAvailable("192.168.16.1", 5432); 
관련 문제