JDBC 테스트 소스

POSTGRESQL 2015. 6. 24. 15:07

postgreSQL JDBC 접속테스트 소스입니다.

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class JDBCExample {
 
 public static void main(String[] argv) {
  Connection conn = null;
  Statement  st   = null;
  ResultSet  rs   = null;

// connection 속성 설정
  String     url      = "jdbc:postgresql://localhost/postgres";
  String     user     = "postgres";
  String     password = "postgres";
  String    query    = "select version()";

  System.out.println("-------- PostgreSQL JDBC Connection Testing ------------");
  
  try {
// postgresql 드라이버 클래스를 JDBC드라이버 메니저에 로드
   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 conn = null;
 
  try {
// conncetion establish 하기
   conn = DriverManager.getConnection(url, user, password);
 
  } catch (SQLException e) {
 
   System.out.println("Connection Failed! Check output console");
   e.printStackTrace();
   return;
 
  }
 
  if (conn != null) {
   System.out.println("Connection established!");
  } else {
   System.out.println("Failed to make conn!");
  }

// 실행할 쿼리 statement 입력
  try { 
   st = conn.createStatement();
   rs = st.executeQuery(query);
   while (rs.next()) {
    String ver = rs.getString("version");
    System.out.println("Your version : " + ver);
   }
  } 
  catch (SQLException e ) {
   System.out.println("query execution failed...");
   e.printStackTrace();
                        return;
  } 
  
// statement / connection close 하기
  try {
   if (st   != null) { st.close(); }
   if (conn != null) {conn.close(); }
  }
  catch (SQLException e ) {
                        System.out.println("failed to close statement or connection");
                        e.printStackTrace();
                        return;
                }
 } //main end
}// JDBCExample class end