MariaDB의 JDBC 연결

MySQL에서 파생된 MariaDB를 프로젝트에 사용하고 있는데요. 이 MariaDB를 Java에서 연결해 필요한 데이터를 조회하기 위해 JDBC를 사용하는 코드를 정리해 둡니다.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MainEntry {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement pstmt = null;   
        ResultSet rs = null;

        try {
            Class.forName("org.mariadb.jdbc.Driver");
			
            con = DriverManager.getConnection(
                "jdbc:mariadb://100.100.100.7:3306/dbname",
                "userId",
                "password");
						
            pstmt = con.prepareStatement("select * from his_bus_voltage");
			
            rs = pstmt.executeQuery();
			
            while(rs.next()) {
                //.
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null) {
                    rs.close(); // 선택 사항
                }
				
                if(pstmt != null) {
                    pstmt.close(); // 선택사항이지만 호출 추천
                }
			
                if(con != null) {
                    con.close(); // 필수 사항
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

MariaDB의 JDBC 라이브러리는 공식 사이트인 https://downloads.mariadb.org/connector-java/에서 다운로드받았으며, 이 글을 작성할 때는 mariadb-java-client-2.0.3.jar를 사용하였습니다.

CentOS 7에서 Postgresql 9.6.2, Postgis 2.3.2-1 설치(인터넷 환경)

인터넷이 되는 환경에서 PostgreSQL(9.6.2)와 PostGIS(2.3.2)를 설치하는 절차입니다.

PostgreSQL 9.6.2 설치

1. PostgreSQL과 PostGIS 설치를 위한 yum 저장소 업데이트

rpm -Uvh https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

2. PostgreSQL 설치

yum install postgresql96-server postgresql96

3. 데이터베이스 저장소 생성

./postgresql96-setup initdb

4. PostgreSQL 서비스 실행

systemctl enable postgresql-9.6
systemctl enable postgresql-9.6.service
systemctl start postgresql-9.6.service

5. 방화벽에서 port 5432번 개방

firewall-cmd --zone=public --add-port=5432/tcp

6. 외부에서 DBMS 접근 허용 설정

vi postgresql.conf

아래처럼 편집

listen_addresses = "*"

7. postgres 사용자 암호 설정

su - postgres
psql
\password postgres
\q

8. 이전 사용자로 복귀
su - root

9. 외부 접속을 위한 보안설정 변경

vi pg_hba.conf

아래처럼 편집

local all all peer 문자열을 local all all md5 로 변경
host all all 127.0.0.1/32 ident 문자열을 host all all 0.0.0.0/0 md5 로 변경
host all all ::1/128 ident 문자열을 host all all ::1/128 md5 로 변경

10. PostgreSQL 서비스 재기동

systemctl stop postgresql-9.6.service
systemctl start postgresql-9.6.service

11. [2017-07-08 추가] UUID 확장 기능 설치를 위해 다음 명령이 필요함

systemctl stop postgresql-9.6.service
yum install postgresql96-contrib.x86_64
systemctl start postgresql-9.6.service

PostGIS 설치

yum install epel-release
yum install postgis2_96.x86_64
systemctl restart postgresql-9.6.service