JAVA/SPRINGBOOT
9月 - JDBC 을 활용한 DB관리 미니프로젝트 연습
인천쓰
2024. 1. 11. 20:25
반응형
9월에 진행한 Java와 DB(오라클)로 진행한 초미니 프로젝트 중 하나입니다 .
JDBC는 DAO VO 로 이루어진 DB의 CRUD를 관리해주는 백엔드입니다 .
오라클 연결부입니다 .
이떄는 오라클로 연습할떄라 SCOTT 계정을 사용하였습니다 .
try chatch구문으로 예외처리를하며, 사용하여 연결후
conn / statement /resultset 을 닫는 메서드를 생성하였습니다 .
public class Common {
final static String ORACLE_URL = "jdbc:oracle:thin:@localhost:1521:xe";
final static String ORACLE_ID = "SCOTT";
final static String ORACLE_PW = "TIGER";
final static String ORACLE_DRV = "oracle.jdbc.driver.OracleDriver"; // 오라클이랑 연결하는것. !
public static Connection getConnection(){
Connection conn =null;
try {
Class.forName(ORACLE_DRV); // 드라이버 로드
// 연결 얻기
conn = DriverManager.getConnection(ORACLE_URL, ORACLE_ID, ORACLE_PW);
}catch (Exception e){
System.out.println("xxxxxx");
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn) {
try {
if(conn != null && !conn.isClosed()) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if (stmt != null && !stmt.isClosed()) {
stmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(ResultSet rset) {
try {
if(rset != null && !rset.isClosed()) {
rset.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
연결메서드 생성후 VO에서 DB에 들어갈 생성자, getter, setter 를 걸어주어 사용하해주었습니다 .
public class ADUserVo {
private String company;
private String banner;
private Date start_AD;
private Date end_AD;
private int AD_Views;
private int view_Price;
private int company_maony;
public ADUserVo(String banner) {
this.banner = banner;
}
public ADUserVo(String company, String banner, Date start_AD, Date end_AD, int AD_Views, int view_Price, int company_maony) {
this.company = company;
this.banner = banner;
this.start_AD = start_AD;
this.end_AD = end_AD;
this.AD_Views = AD_Views;
this.view_Price = view_Price;
this.company_maony = company_maony;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getBanner() {
return banner;
}
public void setBanner(String banner) {
this.banner = banner;
}
public Date getStart_AD() {
return start_AD;
}
public void setStart_AD(Date start_AD) {
this.start_AD = start_AD;
}
public Date getEnd_AD() {
return end_AD;
}
public void setEnd_AD(Date end_AD) {
this.end_AD = end_AD;
}
public int getAD_Views() {
return AD_Views;
}
public void setAD_Views(int AD_Views) {
this.AD_Views = AD_Views;
}
public int getView_Price() {
return view_Price;
}
public void setView_Price(int view_Price) {
this.view_Price = view_Price;
}
public int getCompany_maony() {
return company_maony;
}
public void setCompany_maony(int company_maony) {
this.company_maony = company_maony;
}
}
Dao문은 메소드를 출력메소드와 저장할 메소드를 나누어 만들어 main에 붙여주었습니다 하나예시를 들어주자면
//광고리스트 출력
public List<ADUserVo> AduserSelect() {
List<ADUserVo> list = new ArrayList<>();
try {
conn = Common.getConnection();
stmt = conn.createStatement();
String sql = "SELECT * FROM ADUSER";
rs = stmt.executeQuery(sql);
//rs.값이 있다면 반복(DB 가져온 값이 있으면 행만큼 반복)
while (rs.next()) {
String company = rs.getString("COMPANY");
String banner = rs.getString("BANNER");
Date start_AD = rs.getDate("START_AD");
Date end_AD = rs.getDate("END_AD");
int AD_Views = rs.getInt("AD_VIEWS");
int view_Price = rs.getInt("VIEW_PRICE");
int company_maony = rs.getInt("PRICE");
list.add(new ADUserVo(company, banner, start_AD, end_AD, AD_Views, view_Price, company_maony));
}
Common.close(rs);
Common.close(stmt);
Common.close(conn);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
return list;
}
public void AduserPrint(List<ADUserVo> list) {
System.out.println("=".repeat(10)+"광고 리스트"+"=".repeat(10));
for (ADUserVo e : list) {
System.out.print("" + e.getCompany() + " ");
System.out.print("" + e.getBanner() + " ");
System.out.print("" + e.getStart_AD() + " ");
System.out.print("" + e.getEnd_AD() + " ");
System.out.print("" + e.getAD_Views() + " ");
System.out.print("" + e.getView_Price() + " ");
System.out.println("" + e.getCompany_maony() + " ");
}
이런식으로 사용하여 리스트 출력을 할수있었습니다 .
이번 초미니 프로젝트는 오라클DB에 총 5개 테이블 사용하였으며, 간단하게 콘솔로 만들수있었습니다 .
(예시 콘솔창)
반응형