반응형

JDBC

DB를 연결하기 위해서는 JDBC 드라이버가 필요하다.

 

(1) mysql jdbc connector 다운로드 (jar파일 다운로드 됨)

링크: https://mariadb.com/kb/en/about-mariadb-connector-j/

 

About MariaDB Connector/J

LGPL-licensed MariaDB client library for Java applications.

mariadb.com

 

(2) 프로젝트 내에 해당 jar파일을 옮긴다.

 

(3) 라이브러리를 추가

file > Project Structure > Project Settings > Libraries > +버튼 클릭 > java > 위에서 다운받은 jar파일 선택 

 

(4) 자바-DB 연결

필요 정보 5개  IP / Port / Instance / User_id / Password 정보가 있어야 한다.

 

 

main  내에 필요한 데이터들을 정의한다.

package com.example.wifi_detect_web;

public class DBtest {

    public static void main(String[] args) {
        String url = "jdbc:mariadb://localhost:3306/wifi-project";
        String dbUserId ="root";
        String dbPassword = "1234";

    }
}

테이블 생성 후 데이터 저장

create table log_history
(
	id int NOT NULL AUTO_INCREMENT,
	x DECIMAL(10,7) ,
	y DECIMAL(10,7) ,
	search_date datetime,
	PRIMARY KEY (id)
);

drop table log_history;

INSERT INTO log_history 
(x, y, search_date)
VALUES (37.5544069, 126.8998666, now());

INSERT INTO log_history 
(x, y, search_date)
VALUES (37.5544069, 126.8998666, now());

INSERT INTO log_history 
(x, y, search_date)
VALUES (37.5544069, 126.8998666, now());

select * from log_history ;

JAVA를 통한 JDBC 프로그램 순서

 

(1) JDBC 드라이버 로드

        // 1.JDBC 드라이버 로드
        try {
            Class.forName("org.mariadb.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

(2) 데이터베이스 커넥션 생성

(3) SQL을 위한 Statement 객체 생성 

(4) SQL문장 실행

 

(5) SQL 실행 결과 처리

 

(6) JDBC 객체들 연결 해제

 

 

 

 

 

반응형

+ Recent posts