인텔리제이 jsp mariadb연동

오세훈·2023년 8월 2일
0

jsp

목록 보기
7/10
post-thumbnail

인텔리제이 jsp 마리아db 연동

maven repository 로 가서 mariadb java client 선택

최신 버전 선택 후 maven 복사

project Structure > Libraris > + > maven을 눌러 복사 한 것을 여기에 붙혀 넣어서 맞는 버전 선택

repository에서는 3.1.4가 제일 최신이라 3.1.4 선택
snapshot 버전은 안하는 걸 추천

repository로 돌아가서 jar 다운

lib 폴더에 해당 jar를 저장

인텔리제이는 처음에 보면 없으므로 생성해서 넣으면 됨

db 연동 테스트 코드

BDC interface

package dao;

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

public interface DBC {
    public Connection connect();
    public void close(PreparedStatement stmt, Connection conn);
    public void close(ResultSet rs, PreparedStatement stmt, Connection conn);
}

MariaDBCon class

package dao;

import java.sql.*;

public class MariaDBCon implements DBC{
    final String DRIVER = "org.mariadb.jdbc.Driver"; // 클래스 이름
    final String DNS = "localhost"; // DNS
    final String PORT = "3306"; // 포트 번호
    final String NAME = "edu"; // DB이름
    final String USER = "root"; // user 이름
    final String PASS = "1234"; // 비밀번호

    //        final String URL = "프로토콜:디비종류//호스트주소:포트번호/데이터베이스 이름";
    final String URL = "jdbc:mariadb://"+DNS+":"+PORT+"/"+NAME;
    Connection conn = null;

    @Override
    public Connection connect() {
        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASS);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    @Override
    public void close(PreparedStatement stmt, Connection conn) {
        if(stmt!=null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void close(ResultSet rs, PreparedStatement stmt, Connection conn) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(stmt!=null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


}

Member class

package dto;

public class Member {
    private String id;
    private String pw;
    private String name;
    private String regdate;
    private int point;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPw() {
        return pw;
    }

    public void setPw(String pw) {
        this.pw = pw;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRegdate() {
        return regdate;
    }

    public void setRegdate(String regdate) {
        this.regdate = regdate;
    }

    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }

    @Override
    public String toString() {
        return "Member{" +
                "id='" + id + '\'' +
                ", pw='" + pw + '\'' +
                ", name='" + name + '\'' +
                ", regdate='" + regdate + '\'' +
                ", point=" + point +
                '}';
    }
}

DaoTest1 class

package test;

import dao.DBC;
import dao.MariaDBCon;
import dto.Member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 여러개의 정보 조회
// selectMember()
public class DaoTest1 {
    static Connection conn = null;
    static PreparedStatement stmt = null;
    static ResultSet rs = null;

    public static void main(String[] args) {
        DBC dbc = new MariaDBCon();
        conn = dbc.connect();

        String sql = "select * from member";

        if(conn!=null) {
            System.out.println("DB 연결 성공");
        }
        try {
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            List<Member> memlist = new ArrayList<>(); // Member 클래스 값을 저장할 List 선언
            while(rs.next()) {
                Member mem = new Member(); // Member 클래스의 값을 저장하기 위해 선언
                mem.setId(rs.getString("id"));
                mem.setPw(rs.getString("pw"));
                mem.setName(rs.getString("name"));
                mem.setRegdate(rs.getString("regdate"));
                mem.setPoint(rs.getInt("point"));
                memlist.add(mem);
            }
            for(Member m : memlist) {
                System.out.println(m.toString());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            dbc.close(rs, stmt, conn);
        }
    }
}

인텔리제이에서 context.xml 만드는 법

profile
자바 풀 스택 주니어 개발자

1개의 댓글

comment-user-thumbnail
2023년 8월 2일

좋은 정보 얻어갑니다, 감사합니다.

답글 달기

관련 채용 정보