검색결과 리스트
글
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
'POSTGRESQL' 카테고리의 다른 글
| DDL 구문으로 인한 테이블 읽기/쓰기 lock 발생현상과 회피요령 (0) | 2018.11.29 |
|---|---|
| pgbadger 로그파일로부터 SQL 정보 수집하기 (0) | 2015.06.24 |
| pg_trgm 을 이용한 전후위 like 검색 (0) | 2015.04.21 |
| prepare statements 와 casting (0) | 2015.04.21 |
| md5를 이용한 password hash value 구하기 (0) | 2014.11.26 |