repository에서는 3.1.4가 제일 최신이라 3.1.4 선택
snapshot 버전은 안하는 걸 추천
인텔리제이는 처음에 보면 없으므로 생성해서 넣으면 됨
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);
}
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);
}
}
}
}
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 +
'}';
}
}
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);
}
}
}
좋은 정보 얻어갑니다, 감사합니다.